Skip to content

Commit

Permalink
String trait refactoring (#2863)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Feb 21, 2024
1 parent 075c3c1 commit 65b8ada
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 55 deletions.
12 changes: 12 additions & 0 deletions crates/libs/core/src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ where
Param::Owned(std::mem::transmute_copy(&self))
}
}

impl IntoParam<PCWSTR> for &BSTR {
unsafe fn into_param(self) -> Param<PCWSTR> {
Param::Owned(PCWSTR(self.as_ptr()))
}
}

impl IntoParam<PCWSTR> for &HSTRING {
unsafe fn into_param(self) -> Param<PCWSTR> {
Param::Owned(PCWSTR(self.as_ptr()))
}
}
4 changes: 4 additions & 0 deletions crates/libs/core/src/runtime_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
10 changes: 0 additions & 10 deletions crates/libs/core/src/strings/bstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ impl TryFrom<BSTR> for String {
}
}

impl IntoParam<PCWSTR> for &BSTR {
unsafe fn into_param(self) -> Param<PCWSTR> {
Param::Owned(PCWSTR(self.as_ptr()))
}
}

impl Default for BSTR {
fn default() -> Self {
Self(std::ptr::null_mut())
Expand Down Expand Up @@ -168,7 +162,3 @@ impl Drop for BSTR {
}
}
}

impl TypeKind for BSTR {
type TypeKind = ValueType;
}
14 changes: 0 additions & 14 deletions crates/libs/core/src/strings/hstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -397,12 +389,6 @@ impl From<HSTRING> for std::ffi::OsString {
}
}

impl IntoParam<PCWSTR> for &HSTRING {
unsafe fn into_param(self) -> Param<PCWSTR> {
Param::Owned(PCWSTR(self.as_ptr()))
}
}

const REFERENCE_FLAG: u32 = 1;

#[repr(C)]
Expand Down
3 changes: 1 addition & 2 deletions crates/libs/core/src/strings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ extern "C" {
}

/// An internal helper for decoding an iterator of chars and displaying them
#[doc(hidden)]
pub struct Decode<F>(pub F);
struct Decode<F>(pub F);

impl<F, R, E> std::fmt::Display for Decode<F>
where
Expand Down
17 changes: 0 additions & 17 deletions crates/libs/core/src/strings/pcstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }));
}
}
4 changes: 0 additions & 4 deletions crates/libs/core/src/strings/pcwstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,3 @@ impl PCWSTR {
Decode(move || std::char::decode_utf16(self.as_wide().iter().cloned()))
}
}

impl TypeKind for PCWSTR {
type TypeKind = CopyType;
}
4 changes: 0 additions & 4 deletions crates/libs/core/src/strings/pstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,3 @@ impl PSTR {
Decode(move || decode_utf8(self.as_bytes()))
}
}

impl TypeKind for PSTR {
type TypeKind = CopyType;
}
4 changes: 0 additions & 4 deletions crates/libs/core/src/strings/pwstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,3 @@ impl PWSTR {
Decode(move || std::char::decode_utf16(self.as_wide().iter().cloned()))
}
}

impl TypeKind for PWSTR {
type TypeKind = CopyType;
}
24 changes: 24 additions & 0 deletions crates/libs/core/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,27 @@ primitives!(bool, i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, usize, isize);

#[doc(hidden)]
pub type AbiType<T> = <T as Type<T>>::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;
}
8 changes: 8 additions & 0 deletions crates/tests/core/tests/pcstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }));
}

0 comments on commit 65b8ada

Please sign in to comment.