Skip to content

Commit b2d35e1

Browse files
committed
Implement From<[T; N]> for Rc<[T]> and Arc<[T]>
1 parent 31395ec commit b2d35e1

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
@@ -2406,6 +2406,27 @@ impl<T> From<T> for Rc<T> {
24062406
}
24072407
}
24082408

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