Skip to content

Commit 46da927

Browse files
committed
Auto merge of rust-lang#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.
2 parents aeaa5c3 + b2d35e1 commit 46da927

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

library/alloc/src/rc.rs

+21
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,27 @@ impl<T> From<T> for Rc<T> {
24082408
}
24092409
}
24102410

2411+
#[cfg(not(no_global_oom_handling))]
2412+
#[stable(feature = "shared_from_array", since = "CURRENT_RUSTC_VERSION")]
2413+
impl<T, const N: usize> From<[T; N]> for Rc<[T]> {
2414+
/// Converts a [`[T; N]`](prim@array) into an `Rc<[T]>`.
2415+
///
2416+
/// The conversion moves the array into a newly allocated `Rc`.
2417+
///
2418+
/// # Example
2419+
///
2420+
/// ```
2421+
/// # use std::rc::Rc;
2422+
/// let original: [i32; 3] = [1, 2, 3];
2423+
/// let shared: Rc<[i32]> = Rc::from(original);
2424+
/// assert_eq!(&[1, 2, 3], &shared[..]);
2425+
/// ```
2426+
#[inline]
2427+
fn from(v: [T; N]) -> Rc<[T]> {
2428+
Rc::<[T; N]>::from(v)
2429+
}
2430+
}
2431+
24112432
#[cfg(not(no_global_oom_handling))]
24122433
#[stable(feature = "shared_from_slice", since = "1.21.0")]
24132434
impl<T: Clone> From<&[T]> for Rc<[T]> {

library/alloc/src/sync.rs

+21
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,27 @@ impl<T> From<T> for Arc<T> {
32693269
}
32703270
}
32713271

3272+
#[cfg(not(no_global_oom_handling))]
3273+
#[stable(feature = "shared_from_array", since = "CURRENT_RUSTC_VERSION")]
3274+
impl<T, const N: usize> From<[T; N]> for Arc<[T]> {
3275+
/// Converts a [`[T; N]`](prim@array) into an `Arc<[T]>`.
3276+
///
3277+
/// The conversion moves the array into a newly allocated `Arc`.
3278+
///
3279+
/// # Example
3280+
///
3281+
/// ```
3282+
/// # use std::sync::Arc;
3283+
/// let original: [i32; 3] = [1, 2, 3];
3284+
/// let shared: Arc<[i32]> = Arc::from(original);
3285+
/// assert_eq!(&[1, 2, 3], &shared[..]);
3286+
/// ```
3287+
#[inline]
3288+
fn from(v: [T; N]) -> Arc<[T]> {
3289+
Arc::<[T; N]>::from(v)
3290+
}
3291+
}
3292+
32723293
#[cfg(not(no_global_oom_handling))]
32733294
#[stable(feature = "shared_from_slice", since = "1.21.0")]
32743295
impl<T: Clone> From<&[T]> for Arc<[T]> {

0 commit comments

Comments
 (0)