From 2f475ea5004fd2fa8ade9018aee64e4fcdb37cc1 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Sun, 13 Mar 2022 19:57:58 +0530 Subject: [PATCH 1/2] Impl AsRef for QByteArray --- qttypes/src/qtcore/qbytearray.rs | 18 ++++++++++++++++++ qttypes/src/qtcore/qstring.rs | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/qttypes/src/qtcore/qbytearray.rs b/qttypes/src/qtcore/qbytearray.rs index 64d930de..ad7f460a 100644 --- a/qttypes/src/qtcore/qbytearray.rs +++ b/qttypes/src/qtcore/qbytearray.rs @@ -1,5 +1,6 @@ use crate::internal_prelude::*; use crate::QString; +use std::ffi::CStr; use std::fmt::Display; use std::os::raw::c_char; use std::str::Utf8Error; @@ -31,6 +32,7 @@ impl QByteArray { std::str::from_utf8(self.to_slice()) } } + impl<'a> From<&'a [u8]> for QByteArray { /// Constructs a `QByteArray` from a slice. (Copy the slice.) fn from(s: &'a [u8]) -> QByteArray { @@ -41,18 +43,32 @@ impl<'a> From<&'a [u8]> for QByteArray { }) } } + impl<'a> From<&'a str> for QByteArray { /// Constructs a `QByteArray` from a `&str`. (Copy the string.) fn from(s: &'a str) -> QByteArray { s.as_bytes().into() } } + impl From for QByteArray { /// Constructs a `QByteArray` from a `String`. (Copy the string.) fn from(s: String) -> QByteArray { QByteArray::from(&*s) } } + +impl AsRef for QByteArray { + fn as_ref(&self) -> &CStr { + unsafe { + let s = cpp!([self as "const QByteArray*"] -> *const c_char as "const char*" { + return self->constData(); + }); + &CStr::from_ptr(s) + } + } +} + impl From for QByteArray { /// Converts a `QString` to a `QByteArray` fn from(s: QString) -> QByteArray { @@ -61,6 +77,7 @@ impl From for QByteArray { }) } } + impl Display for QByteArray { /// Prints the contents of the `QByteArray` if it contains UTF-8, do nothing otherwise. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { @@ -72,6 +89,7 @@ impl Display for QByteArray { } } } + impl std::fmt::Debug for QByteArray { /// Prints the contents of the `QByteArray` if it contains UTF-8, nothing otherwise fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { diff --git a/qttypes/src/qtcore/qstring.rs b/qttypes/src/qtcore/qstring.rs index 169966db..c60b20b6 100644 --- a/qttypes/src/qtcore/qstring.rs +++ b/qttypes/src/qtcore/qstring.rs @@ -407,4 +407,13 @@ mod tests { let str3 = QString::from("abcef") + QString::from("gefg"); assert_eq!(str1, str3); } + + #[test] + fn cstring() { + use std::ffi::CString; + + let s = "abc"; + let qstr = QString::from(s); + // assert_eq!(CString::new(qstr), CString::new(s)); + } } From 3cf054fceab9b6b4344e76b24b80bdde2b9f89df Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 15 Mar 2022 01:03:02 +0530 Subject: [PATCH 2/2] Impl From for Vec --- qttypes/src/qtcore/qstring.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/qttypes/src/qtcore/qstring.rs b/qttypes/src/qtcore/qstring.rs index c60b20b6..4ce319cd 100644 --- a/qttypes/src/qtcore/qstring.rs +++ b/qttypes/src/qtcore/qstring.rs @@ -317,6 +317,14 @@ impl TryFrom for i16 { } } +impl From for Vec { + fn from(s: QString) -> Self { + std::char::decode_utf16(s.to_slice().into_iter().cloned()) + .map(|x| (x.expect("QString contains invalid character") as u8)) + .collect() + } +} + impl Add for QString { type Output = QString; @@ -414,6 +422,6 @@ mod tests { let s = "abc"; let qstr = QString::from(s); - // assert_eq!(CString::new(qstr), CString::new(s)); + assert_eq!(CString::new(qstr), CString::new(s)); } }