diff options
| author | bors <bors@rust-lang.org> | 2024-12-27 03:28:21 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-12-27 03:28:21 +0000 |
| commit | 207ed1b18538b3bc77177b162ca8a05223e5e797 (patch) | |
| tree | ed01c67c5cb97757acf928a81383043c23beb082 /library/alloc/src/sync.rs | |
| parent | 917bfa78478cbcc77406e5ea37b24c3eedefacf4 (diff) | |
| parent | c1447e34495fd72dc8d70cb7deea50c457686f69 (diff) | |
| download | rust-207ed1b18538b3bc77177b162ca8a05223e5e797.tar.gz rust-207ed1b18538b3bc77177b162ca8a05223e5e797.zip | |
Auto merge of #134812 - jhpratt:rollup-a9klvez, r=jhpratt
Rollup of 8 pull requests Successful merges: - #131522 ([macro_metavar_expr_concat] Fix #128346) - #134379 (Add `into_array` conversion destructors for `Box`, `Rc`, and `Arc`.) - #134644 (Document collection `From` and `FromIterator` impls that drop duplicate keys.) - #134649 (Fix forgetting to save statx availability on success) - #134728 (Use scoped threads in `std::sync::Barrier` examples) - #134782 (Update Code Example for `Iterator::rposition`) - #134789 (unwinding: bump version to fix naked_asm on Xous) - #134791 (docs: inline `std::ffi::c_str` types to `std::ffi`) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'library/alloc/src/sync.rs')
| -rw-r--r-- | library/alloc/src/sync.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 9be0b3e3e88..b34a6d3f660 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -1203,6 +1203,26 @@ impl<T> Arc<[T]> { )) } } + + /// Converts the reference-counted slice into a reference-counted array. + /// + /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type. + /// + /// If `N` is not exactly equal to the length of `self`, then this method returns `None`. + #[unstable(feature = "slice_as_array", issue = "133508")] + #[inline] + #[must_use] + pub fn into_array<const N: usize>(self) -> Option<Arc<[T; N]>> { + if self.len() == N { + let ptr = Self::into_raw(self) as *const [T; N]; + + // SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length. + let me = unsafe { Arc::from_raw(ptr) }; + Some(me) + } else { + None + } + } } impl<T, A: Allocator> Arc<[T], A> { |
