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 | |
| 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')
| -rw-r--r-- | library/alloc/src/boxed.rs | 20 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/map.rs | 9 | ||||
| -rw-r--r-- | library/alloc/src/collections/btree/set.rs | 5 | ||||
| -rw-r--r-- | library/alloc/src/rc.rs | 20 | ||||
| -rw-r--r-- | library/alloc/src/sync.rs | 20 |
5 files changed, 73 insertions, 1 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 23b85fbd4eb..ca3bd24a420 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -761,6 +761,26 @@ impl<T> Box<[T]> { }; unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) } } + + /// Converts the boxed slice into a boxed 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<Box<[T; N]>> { + if self.len() == N { + let ptr = Self::into_raw(self) as *mut [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 { Box::from_raw(ptr) }; + Some(me) + } else { + None + } + } } impl<T, A: Allocator> Box<[T], A> { diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index d1ce4e215ed..6d305386dbf 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -2289,6 +2289,10 @@ impl<K, V> FusedIterator for RangeMut<'_, K, V> {} #[stable(feature = "rust1", since = "1.0.0")] impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> { + /// Constructs a `BTreeMap<K, V>` from an iterator of key-value pairs. + /// + /// If the iterator produces any pairs with equal keys, + /// all but one of the corresponding values will be dropped. fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> BTreeMap<K, V> { let mut inputs: Vec<_> = iter.into_iter().collect(); @@ -2403,7 +2407,10 @@ where #[stable(feature = "std_collections_from_array", since = "1.56.0")] impl<K: Ord, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V> { - /// Converts a `[(K, V); N]` into a `BTreeMap<(K, V)>`. + /// Converts a `[(K, V); N]` into a `BTreeMap<K, V>`. + /// + /// If any entries in the array have equal keys, + /// all but one of the corresponding values will be dropped. /// /// ``` /// use std::collections::BTreeMap; diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 6f8c3b2d152..9660023d694 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -1491,6 +1491,11 @@ impl<T: Ord, A: Allocator + Clone> BTreeSet<T, A> { impl<T: Ord, const N: usize> From<[T; N]> for BTreeSet<T> { /// Converts a `[T; N]` into a `BTreeSet<T>`. /// + /// If the array contains any equal values, + /// all but one will be dropped. + /// + /// # Examples + /// /// ``` /// use std::collections::BTreeSet; /// diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index b7ec3af9818..e014404eff3 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -1084,6 +1084,26 @@ impl<T> Rc<[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<Rc<[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 { Rc::from_raw(ptr) }; + Some(me) + } else { + None + } + } } impl<T, A: Allocator> Rc<[T], A> { 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> { |
