diff --git a/src/lib.rs b/src/lib.rs index 212fa58..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. @@ -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,23 @@ 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, } + +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