Skip to content

Commit d1c4b5e

Browse files
ahayzen-kdabBe-ing
authored andcommitted
cxx-qt-lib: add support for bytes crate with QByteArray
Related to #292
1 parent 9d0f36d commit d1c4b5e

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Support for further types: `QByteArray`, `QCoreApplication`, `QGuiApplication`, `QModelIndex`, `QPersistentModelIndex`, `QQmlApplicationEngine`, `QQmlEngine`, `QStringList`, `QVector2D`, `QVector3D`, `QVector4D`
2121
- Support for nesting objects in properties, invokables, and signals with `*mut T`
2222
- Allow for marking signals as existing in the base class
23-
- Support for conversions to types in third-party crates: `http`, `rgb`, `url`
23+
- Support for conversions to types in third-party crates: `bytes`, `http`, `rgb`, `url`
2424

2525
### Changed
2626

crates/cxx-qt-lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ links = "cxx-qt-lib"
1717

1818
[dependencies]
1919
cxx.workspace = true
20+
bytes = { version = "1.4", optional = true }
2021
http = { version = "0.2", optional = true }
2122
rgb = { version = "0.8", optional = true }
2223
url = { version = "2.3", optional = true }
@@ -28,6 +29,7 @@ qt-build-utils.workspace = true
2829

2930
[features]
3031
default = ["qt_gui", "qt_qml"]
32+
bytes = ["dep:bytes"]
3133
http = ["dep:http"]
3234
rgb = ["dep:rgb"]
3335
qt_gui = ["cxx-qt-lib-headers/qt_gui"]

crates/cxx-qt-lib/src/core/qbytearray.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ impl From<&QByteArray> for Vec<u8> {
176176
}
177177
}
178178

179+
#[cfg(feature = "bytes")]
180+
impl From<&bytes::Bytes> for QByteArray {
181+
/// Convert `bytes::Bytes` to a QByteArray. This makes a deep copy of the data.
182+
fn from(value: &bytes::Bytes) -> Self {
183+
Self::from(value.as_ref())
184+
}
185+
}
186+
187+
#[cfg(feature = "bytes")]
188+
impl From<&QByteArray> for bytes::Bytes {
189+
/// Convert QByteArray to a `bytes::Bytes`. This makes a deep copy of the data.
190+
fn from(value: &QByteArray) -> Self {
191+
Self::copy_from_slice(value.as_ref())
192+
}
193+
}
194+
179195
impl QByteArray {
180196
/// Inserts value at the end of the list.
181197
pub fn append(&mut self, ch: u8) {
@@ -199,12 +215,24 @@ impl QByteArray {
199215
ffi::qbytearray_fill(self, ch, size)
200216
}
201217

218+
/// Construct a QByteArray from a `bytes::Bytes` without a deep copy
219+
///
220+
/// # Safety
221+
///
222+
/// The caller must ensure that the original `bytes::Bytes` outlives the QByteArray
223+
/// and that the QByteArray is not modified
224+
#[cfg(feature = "bytes")]
225+
pub unsafe fn from_raw_bytes(bytes: &bytes::Bytes) -> Self {
226+
Self::from_raw_data(bytes.as_ref())
227+
}
228+
202229
/// Construct a QByteArray from a `&[u8]` without a deep copy
203230
///
204231
/// # Safety
205232
///
206233
/// The caller must ensure that the original slice outlives the QByteArray
207-
pub unsafe fn from_raw_data(bytes: &[u8]) -> QByteArray {
234+
/// and that the QByteArray is not modified
235+
pub unsafe fn from_raw_data(bytes: &[u8]) -> Self {
208236
ffi::qbytearray_from_raw_data(bytes)
209237
}
210238

@@ -251,3 +279,20 @@ unsafe impl ExternType for QByteArray {
251279
type Id = type_id!("QByteArray");
252280
type Kind = cxx::kind::Trivial;
253281
}
282+
283+
#[cfg(test)]
284+
mod tests {
285+
#[cfg(feature = "bytes")]
286+
use super::*;
287+
288+
#[cfg(feature = "bytes")]
289+
#[test]
290+
fn test_bytes() {
291+
let bytes = bytes::Bytes::from("KDAB");
292+
let qbytearray = QByteArray::from(&bytes);
293+
assert_eq!(bytes.as_ref(), qbytearray.as_ref());
294+
295+
let bytes_bytes = bytes::Bytes::from(&qbytearray);
296+
assert_eq!(bytes, bytes_bytes)
297+
}
298+
}

0 commit comments

Comments
 (0)