about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-28 06:16:01 +0000
committerbors <bors@rust-lang.org>2023-09-28 06:16:01 +0000
commit46da927abb389f646d9b0c500e5c386aec61ecf9 (patch)
tree59fbab9314234ee71523fb89ad4eacd76cab65b1
parentaeaa5c30e5c9041264a2e8314b68ad84c2dc3169 (diff)
parentb2d35e1f4bc2aa6f2b9745ea515d5d69298b2e52 (diff)
downloadrust-46da927abb389f646d9b0c500e5c386aec61ecf9.tar.gz
rust-46da927abb389f646d9b0c500e5c386aec61ecf9.zip
Auto merge of #114041 - nvzqz:nvzqz/shared_from_array, r=dtolnay
Implement `From<[T; N]>` for `Rc<[T]>` and `Arc<[T]>`

Given that `Box<[T]>` already has this conversion, the shared counterparts should also have it.
-rw-r--r--library/alloc/src/rc.rs21
-rw-r--r--library/alloc/src/sync.rs21
2 files changed, 42 insertions, 0 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs
index 0fb06b16655..8dbaca223aa 100644
--- a/library/alloc/src/rc.rs
+++ b/library/alloc/src/rc.rs
@@ -2409,6 +2409,27 @@ impl<T> From<T> for Rc<T> {
 }
 
 #[cfg(not(no_global_oom_handling))]
+#[stable(feature = "shared_from_array", since = "CURRENT_RUSTC_VERSION")]
+impl<T, const N: usize> From<[T; N]> for Rc<[T]> {
+    /// Converts a [`[T; N]`](prim@array) into an `Rc<[T]>`.
+    ///
+    /// The conversion moves the array into a newly allocated `Rc`.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// # use std::rc::Rc;
+    /// let original: [i32; 3] = [1, 2, 3];
+    /// let shared: Rc<[i32]> = Rc::from(original);
+    /// assert_eq!(&[1, 2, 3], &shared[..]);
+    /// ```
+    #[inline]
+    fn from(v: [T; N]) -> Rc<[T]> {
+        Rc::<[T; N]>::from(v)
+    }
+}
+
+#[cfg(not(no_global_oom_handling))]
 #[stable(feature = "shared_from_slice", since = "1.21.0")]
 impl<T: Clone> From<&[T]> for Rc<[T]> {
     /// Allocate a reference-counted slice and fill it by cloning `v`'s items.
diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs
index c53e9a5dd7a..9df971af49c 100644
--- a/library/alloc/src/sync.rs
+++ b/library/alloc/src/sync.rs
@@ -3270,6 +3270,27 @@ impl<T> From<T> for Arc<T> {
 }
 
 #[cfg(not(no_global_oom_handling))]
+#[stable(feature = "shared_from_array", since = "CURRENT_RUSTC_VERSION")]
+impl<T, const N: usize> From<[T; N]> for Arc<[T]> {
+    /// Converts a [`[T; N]`](prim@array) into an `Arc<[T]>`.
+    ///
+    /// The conversion moves the array into a newly allocated `Arc`.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// # use std::sync::Arc;
+    /// let original: [i32; 3] = [1, 2, 3];
+    /// let shared: Arc<[i32]> = Arc::from(original);
+    /// assert_eq!(&[1, 2, 3], &shared[..]);
+    /// ```
+    #[inline]
+    fn from(v: [T; N]) -> Arc<[T]> {
+        Arc::<[T; N]>::from(v)
+    }
+}
+
+#[cfg(not(no_global_oom_handling))]
 #[stable(feature = "shared_from_slice", since = "1.21.0")]
 impl<T: Clone> From<&[T]> for Arc<[T]> {
     /// Allocate a reference-counted slice and fill it by cloning `v`'s items.