Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<[T; N]> for Rc<[T]> and Arc<[T]> #114041

Merged
merged 1 commit into from Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions library/alloc/src/rc.rs
Expand Up @@ -2406,6 +2406,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;
chenyukang marked this conversation as resolved.
Show resolved Hide resolved
/// 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]> {
Expand Down
21 changes: 21 additions & 0 deletions library/alloc/src/sync.rs
Expand Up @@ -3269,6 +3269,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;
chenyukang marked this conversation as resolved.
Show resolved Hide resolved
/// 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]> {
Expand Down