From 65b8ada2811917723113e11e279bf281a889582d Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Tue, 20 Feb 2024 20:10:06 -0600 Subject: [PATCH] String trait refactoring (#2863) --- crates/libs/core/src/param.rs | 12 ++++++++++++ crates/libs/core/src/runtime_type.rs | 4 ++++ crates/libs/core/src/strings/bstr.rs | 10 ---------- crates/libs/core/src/strings/hstring.rs | 14 -------------- crates/libs/core/src/strings/mod.rs | 3 +-- crates/libs/core/src/strings/pcstr.rs | 17 ----------------- crates/libs/core/src/strings/pcwstr.rs | 4 ---- crates/libs/core/src/strings/pstr.rs | 4 ---- crates/libs/core/src/strings/pwstr.rs | 4 ---- crates/libs/core/src/type.rs | 24 ++++++++++++++++++++++++ crates/tests/core/tests/pcstr.rs | 8 ++++++++ 11 files changed, 49 insertions(+), 55 deletions(-) diff --git a/crates/libs/core/src/param.rs b/crates/libs/core/src/param.rs index 8dca3e059c..cd16d473f2 100644 --- a/crates/libs/core/src/param.rs +++ b/crates/libs/core/src/param.rs @@ -79,3 +79,15 @@ where Param::Owned(std::mem::transmute_copy(&self)) } } + +impl IntoParam for &BSTR { + unsafe fn into_param(self) -> Param { + Param::Owned(PCWSTR(self.as_ptr())) + } +} + +impl IntoParam for &HSTRING { + unsafe fn into_param(self) -> Param { + Param::Owned(PCWSTR(self.as_ptr())) + } +} diff --git a/crates/libs/core/src/runtime_type.rs b/crates/libs/core/src/runtime_type.rs index c85a3889bc..560b5d14d1 100644 --- a/crates/libs/core/src/runtime_type.rs +++ b/crates/libs/core/src/runtime_type.rs @@ -28,3 +28,7 @@ primitives! { (f32, b"f4"), (f64, b"f8") } + +impl RuntimeType for HSTRING { + const SIGNATURE: crate::imp::ConstBuffer = crate::imp::ConstBuffer::from_slice(b"string"); +} diff --git a/crates/libs/core/src/strings/bstr.rs b/crates/libs/core/src/strings/bstr.rs index 91197ebd12..70da39c6f3 100644 --- a/crates/libs/core/src/strings/bstr.rs +++ b/crates/libs/core/src/strings/bstr.rs @@ -111,12 +111,6 @@ impl TryFrom for String { } } -impl IntoParam for &BSTR { - unsafe fn into_param(self) -> Param { - Param::Owned(PCWSTR(self.as_ptr())) - } -} - impl Default for BSTR { fn default() -> Self { Self(std::ptr::null_mut()) @@ -168,7 +162,3 @@ impl Drop for BSTR { } } } - -impl TypeKind for BSTR { - type TypeKind = ValueType; -} diff --git a/crates/libs/core/src/strings/hstring.rs b/crates/libs/core/src/strings/hstring.rs index 3082189353..7ef4732122 100644 --- a/crates/libs/core/src/strings/hstring.rs +++ b/crates/libs/core/src/strings/hstring.rs @@ -87,14 +87,6 @@ impl HSTRING { } } -impl RuntimeType for HSTRING { - const SIGNATURE: crate::imp::ConstBuffer = crate::imp::ConstBuffer::from_slice(b"string"); -} - -impl TypeKind for HSTRING { - type TypeKind = ValueType; -} - impl Default for HSTRING { fn default() -> Self { Self::new() @@ -397,12 +389,6 @@ impl From for std::ffi::OsString { } } -impl IntoParam for &HSTRING { - unsafe fn into_param(self) -> Param { - Param::Owned(PCWSTR(self.as_ptr())) - } -} - const REFERENCE_FLAG: u32 = 1; #[repr(C)] diff --git a/crates/libs/core/src/strings/mod.rs b/crates/libs/core/src/strings/mod.rs index 94e7bfd992..632aa2ea42 100644 --- a/crates/libs/core/src/strings/mod.rs +++ b/crates/libs/core/src/strings/mod.rs @@ -25,8 +25,7 @@ extern "C" { } /// An internal helper for decoding an iterator of chars and displaying them -#[doc(hidden)] -pub struct Decode(pub F); +struct Decode(pub F); impl std::fmt::Display for Decode where diff --git a/crates/libs/core/src/strings/pcstr.rs b/crates/libs/core/src/strings/pcstr.rs index d4da2c3319..0d69cb6a90 100644 --- a/crates/libs/core/src/strings/pcstr.rs +++ b/crates/libs/core/src/strings/pcstr.rs @@ -54,20 +54,3 @@ impl PCSTR { Decode(move || decode_utf8(self.as_bytes())) } } - -impl TypeKind for PCSTR { - type TypeKind = CopyType; -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn can_display() { - // 💖 followed by an invalid byte sequence and then an incomplete one - let s = [240, 159, 146, 150, 255, 240, 159, 0]; - let s = PCSTR::from_raw(s.as_ptr()); - assert_eq!("💖�", format!("{}", unsafe { s.display() })); - } -} diff --git a/crates/libs/core/src/strings/pcwstr.rs b/crates/libs/core/src/strings/pcwstr.rs index 03b8cdd928..4ae16fdbc7 100644 --- a/crates/libs/core/src/strings/pcwstr.rs +++ b/crates/libs/core/src/strings/pcwstr.rs @@ -63,7 +63,3 @@ impl PCWSTR { Decode(move || std::char::decode_utf16(self.as_wide().iter().cloned())) } } - -impl TypeKind for PCWSTR { - type TypeKind = CopyType; -} diff --git a/crates/libs/core/src/strings/pstr.rs b/crates/libs/core/src/strings/pstr.rs index 634b7ac036..aaa4a3b7e0 100644 --- a/crates/libs/core/src/strings/pstr.rs +++ b/crates/libs/core/src/strings/pstr.rs @@ -54,7 +54,3 @@ impl PSTR { Decode(move || decode_utf8(self.as_bytes())) } } - -impl TypeKind for PSTR { - type TypeKind = CopyType; -} diff --git a/crates/libs/core/src/strings/pwstr.rs b/crates/libs/core/src/strings/pwstr.rs index 8ed6e7b1da..69b8c70d2d 100644 --- a/crates/libs/core/src/strings/pwstr.rs +++ b/crates/libs/core/src/strings/pwstr.rs @@ -63,7 +63,3 @@ impl PWSTR { Decode(move || std::char::decode_utf16(self.as_wide().iter().cloned())) } } - -impl TypeKind for PWSTR { - type TypeKind = CopyType; -} diff --git a/crates/libs/core/src/type.rs b/crates/libs/core/src/type.rs index 3b4884962c..2379a5e8ea 100644 --- a/crates/libs/core/src/type.rs +++ b/crates/libs/core/src/type.rs @@ -98,3 +98,27 @@ primitives!(bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize); #[doc(hidden)] pub type AbiType = >::Abi; + +impl TypeKind for PWSTR { + type TypeKind = CopyType; +} + +impl TypeKind for PSTR { + type TypeKind = CopyType; +} + +impl TypeKind for PCWSTR { + type TypeKind = CopyType; +} + +impl TypeKind for PCSTR { + type TypeKind = CopyType; +} + +impl TypeKind for HSTRING { + type TypeKind = ValueType; +} + +impl TypeKind for BSTR { + type TypeKind = ValueType; +} diff --git a/crates/tests/core/tests/pcstr.rs b/crates/tests/core/tests/pcstr.rs index 0f4e109fc0..b51528651f 100644 --- a/crates/tests/core/tests/pcstr.rs +++ b/crates/tests/core/tests/pcstr.rs @@ -20,3 +20,11 @@ fn test() -> Result<()> { Ok(()) } + +#[test] +fn can_display() { + // 💖 followed by an invalid byte sequence and then an incomplete one + let s = [240, 159, 146, 150, 255, 240, 159, 0]; + let s = PCSTR::from_raw(s.as_ptr()); + assert_eq!("💖�", format!("{}", unsafe { s.display() })); +}