diff options
| author | Clar Charr <clar@charr.xyz> | 2017-04-11 16:02:43 -0400 |
|---|---|---|
| committer | Clar Charr <clar@charr.xyz> | 2017-04-24 17:51:49 -0400 |
| commit | c66c6e96978cd81130587e9f4d1fa52dc1b183d6 (patch) | |
| tree | 98339df32166aa47788c639e3f74de8776f3367c /src/liballoc/boxed.rs | |
| parent | 968ae7babecfc6c62ef9699ff052d9ab00411848 (diff) | |
| download | rust-c66c6e96978cd81130587e9f4d1fa52dc1b183d6.tar.gz rust-c66c6e96978cd81130587e9f4d1fa52dc1b183d6.zip | |
More methods for str boxes.
Diffstat (limited to 'src/liballoc/boxed.rs')
| -rw-r--r-- | src/liballoc/boxed.rs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 43b0d72186a..b03e3bb7a4b 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -68,6 +68,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut}; use core::ops::{BoxPlace, Boxed, InPlace, Place, Placer}; use core::ptr::{self, Unique}; use core::convert::From; +use str::from_boxed_utf8_unchecked; /// A value that represents the heap. This is the default place that the `box` /// keyword allocates into when no place is supplied. @@ -320,8 +321,7 @@ impl<T> Default for Box<[T]> { #[stable(feature = "default_box_extra", since = "1.17.0")] impl Default for Box<str> { fn default() -> Box<str> { - let default: Box<[u8]> = Default::default(); - unsafe { mem::transmute(default) } + unsafe { from_boxed_utf8_unchecked(Default::default()) } } } @@ -366,7 +366,7 @@ impl Clone for Box<str> { let buf = RawVec::with_capacity(len); unsafe { ptr::copy_nonoverlapping(self.as_ptr(), buf.ptr(), len); - mem::transmute(buf.into_box()) // bytes to str ~magic + from_boxed_utf8_unchecked(buf.into_box()) } } } @@ -441,8 +441,16 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> { #[stable(feature = "box_from_slice", since = "1.17.0")] impl<'a> From<&'a str> for Box<str> { fn from(s: &'a str) -> Box<str> { - let boxed: Box<[u8]> = Box::from(s.as_bytes()); - unsafe { mem::transmute(boxed) } + unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) } + } +} + +#[stable(feature = "boxed_str_conv", since = "1.18.0")] +impl From<Box<str>> for Box<[u8]> { + fn from(s: Box<str>) -> Self { + unsafe { + mem::transmute(s) + } } } |
