diff options
| author | The Miri Cronjob Bot <miri@cron.bot> | 2024-12-31 05:12:50 +0000 |
|---|---|---|
| committer | The Miri Cronjob Bot <miri@cron.bot> | 2024-12-31 05:12:50 +0000 |
| commit | e898da11d2149358e87f7c4f89dcc0654fcfac3b (patch) | |
| tree | 6f09bd54aead12fa9e0155cda3dcb7e0f46f97ae /library/alloc/src/sync.rs | |
| parent | 332fefbd3e686d0d76a43119e374997543338389 (diff) | |
| parent | 5079acc060b1c7225de95ee3cdd84b5719ff189c (diff) | |
| download | rust-e898da11d2149358e87f7c4f89dcc0654fcfac3b.tar.gz rust-e898da11d2149358e87f7c4f89dcc0654fcfac3b.zip | |
Merge from rustc
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> { |
