From a4d09b1a191f315fe4fb7c3eb1721f0478b29309 Mon Sep 17 00:00:00 2001 From: William Tremblay Date: Fri, 4 Oct 2024 13:17:55 -0400 Subject: [PATCH 1/2] remove 'static requirement on WStr endianness --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 212fa58..37b3363 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -160,8 +160,8 @@ pub struct WString { #[derive(Debug, Eq, PartialEq, Hash)] #[repr(transparent)] -pub struct WStr { - _endian: PhantomData<&'static E>, +pub struct WStr { + _endian: PhantomData E>, raw: [u8], } @@ -170,9 +170,9 @@ pub struct WStr { /// The slice must contain valid UTF-16, otherwise this may panic or cause undefined /// behaviour. #[derive(Debug)] -pub struct WStrChars<'a, E: 'static + ByteOrder> { +pub struct WStrChars<'a, E: ByteOrder> { chunks: ChunksExact<'a, u8>, - _endian: PhantomData<&'static E>, + _endian: PhantomData E>, } /// Iterator yielding `(index, char)` tuples from a UTF-16 little-endian encoded byte slice. @@ -180,7 +180,7 @@ pub struct WStrChars<'a, E: 'static + ByteOrder> { /// The slice must contain valid UTF-16, otherwise this may panic or cause undefined /// behaviour. #[derive(Debug)] -pub struct WStrCharIndices<'a, E: 'static + ByteOrder> { +pub struct WStrCharIndices<'a, E: ByteOrder> { chars: WStrChars<'a, E>, index: usize, } From 48f5af9be03440f6c0c466848e07d853432c1438 Mon Sep 17 00:00:00 2001 From: William Tremblay Date: Fri, 4 Oct 2024 13:29:51 -0400 Subject: [PATCH 2/2] add impls of Borrow and ToOwned for Cow support --- src/lib.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 37b3363..91eecbd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ clippy::all )] -use std::marker::PhantomData; +use std::{borrow::Borrow, marker::PhantomData}; use std::slice::ChunksExact; use byteorder::ByteOrder; @@ -130,9 +130,9 @@ pub struct Utf16Error { /// assert_eq!(s0, s1); /// ``` #[derive(Debug, Eq, PartialEq, Hash)] -pub struct WString { +pub struct WString { buf: Vec, - _endian: PhantomData<&'static E>, + _endian: PhantomData E>, } /// A UTF-16 [`str`]-like type with little- or big-endian byte order. @@ -184,3 +184,19 @@ pub struct WStrCharIndices<'a, E: ByteOrder> { chars: WStrChars<'a, E>, index: usize, } + +impl Borrow> for WString { + fn borrow(&self) -> &WStr { + return self.as_wstr() + } +} + +impl ToOwned for WStr { + type Owned = WString; + + fn to_owned(&self) -> Self::Owned { + let mut wstr = WString::with_capacity(self.len()); + wstr.insert_wstr(0, self); + wstr + } +} \ No newline at end of file