From 594be707c43f626538ff11b22c35b858c3323cf2 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Fri, 15 Apr 2022 14:34:54 +0200 Subject: Implement str to [u8] conversion for refcounted containers --- library/alloc/src/sync.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'library/alloc/src/sync.rs') diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index a19999cd725..a8ed31b776b 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -2556,6 +2556,25 @@ where } } +#[stable(feature = "shared_from_str", since = "1.61.0")] +impl From> for Arc<[u8]> { + /// Converts an atomically reference-counted string slice into a byte slice. + /// + /// # Example + /// + /// ``` + /// # use std::sync::Arc; + /// let string: Arc = Arc::from("eggplant"); + /// let bytes: Arc<[u8]> = Arc::from(string); + /// assert_eq!("eggplant".as_bytes(), bytes.as_ref()); + /// ``` + #[inline] + fn from(rc: Arc) -> Self { + // SAFETY: `str` has the same layout as `[u8]`. + unsafe { Arc::from_raw(Arc::into_raw(rc) as *const [u8]) } + } +} + #[stable(feature = "boxed_slice_try_from", since = "1.43.0")] impl TryFrom> for Arc<[T; N]> { type Error = Arc<[T]>; -- cgit 1.4.1-3-g733a5 From 100006bec9262a3774bfeb34a7ac5867b422b896 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 30 Apr 2022 23:40:35 -0700 Subject: Bump shared_from_str to Rust 1.62.0 --- library/alloc/src/rc.rs | 2 +- library/alloc/src/sync.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'library/alloc/src/sync.rs') diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 488b74d388e..edc6883573e 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1956,7 +1956,7 @@ where } } -#[stable(feature = "shared_from_str", since = "1.61.0")] +#[stable(feature = "shared_from_str", since = "1.62.0")] impl From> for Rc<[u8]> { /// Converts a reference-counted string slice into a byte slice. /// diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index a8ed31b776b..1e2caddcacb 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -2556,7 +2556,7 @@ where } } -#[stable(feature = "shared_from_str", since = "1.61.0")] +#[stable(feature = "shared_from_str", since = "1.62.0")] impl From> for Arc<[u8]> { /// Converts an atomically reference-counted string slice into a byte slice. /// -- cgit 1.4.1-3-g733a5