Skip to content

Commit c0e84e6

Browse files
committed
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>`.
1 parent 2f4e31c commit c0e84e6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

wstr/src/buf.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,7 @@ impl DerefMut for WString {
361361
impl<'a> From<&'a WStr> for WString {
362362
#[inline]
363363
fn from(s: &'a WStr) -> Self {
364-
let mut buf = Self::new();
365-
buf.push_str(s);
366-
buf
364+
s.to_owned()
367365
}
368366
}
369367

wstr/src/ptr.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use alloc::borrow::ToOwned;
12
use core::ops::Range;
23
use core::ptr::{slice_from_raw_parts, slice_from_raw_parts_mut};
34

4-
use super::Units;
5+
use super::{Units, WString};
56

67
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
78
compile_error!("WStr only supports 32-bits and 64-bits targets");
@@ -46,6 +47,16 @@ pub struct WStr {
4647
_repr: [()],
4748
}
4849

50+
impl ToOwned for WStr {
51+
type Owned = WString;
52+
53+
fn to_owned(&self) -> Self::Owned {
54+
let mut buf = WString::new();
55+
buf.push_str(self);
56+
buf
57+
}
58+
}
59+
4960
/// Convenience method to turn a `&T` into a `*mut T`.
5061
#[inline]
5162
pub fn ptr_mut<T: ?Sized>(t: &T) -> *mut T {

0 commit comments

Comments
 (0)