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