wstr: Implement `ToOwned` for `WStr`

Similarly to how `str` implements `ToOwned`, and `From<&str> for String`
forwards to `to_owned()`.

This will allow defining `Cow<WStr>`.
This commit is contained in:
relrelb 2022-07-03 20:44:27 +03:00 committed by relrelb
parent 2f4e31c6be
commit c0e84e646e
2 changed files with 13 additions and 4 deletions

View File

@ -361,9 +361,7 @@ impl DerefMut for WString {
impl<'a> From<&'a WStr> for WString {
#[inline]
fn from(s: &'a WStr) -> Self {
let mut buf = Self::new();
buf.push_str(s);
buf
s.to_owned()
}
}

View File

@ -1,7 +1,8 @@
use alloc::borrow::ToOwned;
use core::ops::Range;
use core::ptr::{slice_from_raw_parts, slice_from_raw_parts_mut};
use super::Units;
use super::{Units, WString};
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
compile_error!("WStr only supports 32-bits and 64-bits targets");
@ -46,6 +47,16 @@ pub struct WStr {
_repr: [()],
}
impl ToOwned for WStr {
type Owned = WString;
fn to_owned(&self) -> Self::Owned {
let mut buf = WString::new();
buf.push_str(self);
buf
}
}
/// Convenience method to turn a `&T` into a `*mut T`.
#[inline]
pub fn ptr_mut<T: ?Sized>(t: &T) -> *mut T {