diff options
Diffstat (limited to 'third_party/rust/fallible_collections/src')
6 files changed, 87 insertions, 39 deletions
diff --git a/third_party/rust/fallible_collections/src/arc.rs b/third_party/rust/fallible_collections/src/arc.rs index 282b8e5555..26a6ec9407 100644 --- a/third_party/rust/fallible_collections/src/arc.rs +++ b/third_party/rust/fallible_collections/src/arc.rs @@ -1,15 +1,20 @@ //! Implement a Fallible Arc +#[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] use super::FallibleBox; use super::TryClone; - use crate::TryReserveError; + +#[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] use alloc::boxed::Box; use alloc::sync::Arc; /// trait to implement Fallible Arc -#[deprecated( - since = "0.3.1", - note = "⚠️️️this function is not completely fallible, it can panic !, see [issue](https://github.com/vcombey/fallible_collections/issues/13). help wanted" +#[cfg_attr( + any(not(feature = "unstable"), feature = "rust_1_57"), + deprecated( + since = "0.3.1", + note = "⚠️️️this function is not completely fallible, it can panic !, see [issue](https://github.com/vcombey/fallible_collections/issues/13). help wanted" + ) )] pub trait FallibleArc<T> { /// try creating a new Arc, returning a Result<Box<T>, @@ -22,10 +27,24 @@ pub trait FallibleArc<T> { #[allow(deprecated)] impl<T> FallibleArc<T> for Arc<T> { fn try_new(t: T) -> Result<Self, TryReserveError> { - // doesn't work as the inner variable of arc are also stocked in the box - - let b = <Box<T> as FallibleBox<T>>::try_new(t)?; - Ok(Arc::from(b)) + #[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] + { + // doesn't work as the inner variable of arc are also stocked in the box + let b = <Box<T> as FallibleBox<T>>::try_new(t)?; + Ok(Arc::from(b)) + } + #[cfg(all(feature = "unstable", not(feature = "rust_1_57")))] + { + use alloc::alloc::Layout; + use alloc::collections::TryReserveErrorKind; + Arc::try_new(t).map_err(|_e| { + TryReserveErrorKind::AllocError { + layout: Layout::new::<Arc<T>>(), // This is bullshit + non_exhaustive: (), + } + .into() + }) + } } } diff --git a/third_party/rust/fallible_collections/src/boxed.rs b/third_party/rust/fallible_collections/src/boxed.rs index 6040754716..6680c713f4 100644 --- a/third_party/rust/fallible_collections/src/boxed.rs +++ b/third_party/rust/fallible_collections/src/boxed.rs @@ -64,19 +64,22 @@ impl<T> Deref for TryBox<T> { } fn alloc(layout: Layout) -> Result<NonNull<u8>, TryReserveError> { - #[cfg(feature = "unstable")] // requires allocator_api + #[cfg(all(feature = "unstable", not(feature = "rust_1_57")))] // requires allocator_api { + use alloc::collections::TryReserveErrorKind; use core::alloc::Allocator; alloc::alloc::Global .allocate(layout) - .map_err(|_e| TryReserveError::AllocError { - layout, - #[cfg(not(feature = "rust_1_57"))] - non_exhaustive: (), + .map_err(|_e| { + TryReserveErrorKind::AllocError { + layout, + non_exhaustive: (), + } + .into() }) .map(|v| v.cast()) } - #[cfg(not(feature = "unstable"))] + #[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] { match layout.size() { 0 => { diff --git a/third_party/rust/fallible_collections/src/btree/node.rs b/third_party/rust/fallible_collections/src/btree/node.rs index 249aeb6598..bed459dcfe 100644 --- a/third_party/rust/fallible_collections/src/btree/node.rs +++ b/third_party/rust/fallible_collections/src/btree/node.rs @@ -670,8 +670,8 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> { let idx = self.len(); unsafe { - ptr::write(self.keys_mut().get_unchecked_mut(idx), key); - ptr::write(self.vals_mut().get_unchecked_mut(idx), val); + ptr::write(self.keys_mut().as_mut_ptr().add(idx), key); + ptr::write(self.vals_mut().as_mut_ptr().add(idx), val); (*self.as_leaf_mut()).len += 1; } @@ -703,11 +703,14 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> { let idx = self.len(); unsafe { - ptr::write(self.keys_mut().get_unchecked_mut(idx), key); - ptr::write(self.vals_mut().get_unchecked_mut(idx), val); + ptr::write(self.keys_mut().as_mut_ptr().add(idx), key); + ptr::write(self.vals_mut().as_mut_ptr().add(idx), val); self.as_internal_mut() .edges - .get_unchecked_mut(idx + 1) + .as_mut_ptr() + .add(idx + 1) + .as_mut() + .unwrap() .write(edge.node); (*self.as_leaf_mut()).len += 1; @@ -1002,7 +1005,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge (*self.node.as_leaf_mut()).len += 1; - self.node.vals_mut().get_unchecked_mut(self.idx) + self.node.vals_mut().as_mut_ptr().add(self.idx) } } @@ -1156,8 +1159,8 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType> let (keys, vals) = self.node.into_slices_mut(); unsafe { ( - keys.get_unchecked_mut(self.idx), - vals.get_unchecked_mut(self.idx), + keys.as_mut_ptr().add(self.idx).as_mut().unwrap(), + vals.as_mut_ptr().add(self.idx).as_mut().unwrap(), ) } } @@ -1168,8 +1171,8 @@ impl<'a, K, V, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker unsafe { let (keys, vals) = self.node.reborrow_mut().into_slices_mut(); ( - keys.get_unchecked_mut(self.idx), - vals.get_unchecked_mut(self.idx), + keys.as_mut_ptr().add(self.idx).as_mut().unwrap(), + vals.as_mut_ptr().add(self.idx).as_mut().unwrap(), ) } } @@ -1338,7 +1341,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker:: unsafe { ptr::write( - left_node.keys_mut().get_unchecked_mut(left_len), + left_node.keys_mut().as_mut_ptr().add(left_len), slice_remove(self.node.keys_mut(), self.idx), ); ptr::copy_nonoverlapping( @@ -1347,7 +1350,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker:: right_len, ); ptr::write( - left_node.vals_mut().get_unchecked_mut(left_len), + left_node.vals_mut().as_mut_ptr().add(left_len), slice_remove(self.node.vals_mut(), self.idx), ); ptr::copy_nonoverlapping( @@ -1662,7 +1665,7 @@ unsafe fn slice_insert<T>(slice: &mut [T], idx: usize, val: T) { slice.as_mut_ptr().add(idx + 1), slice.len() - idx, ); - ptr::write(slice.get_unchecked_mut(idx), val); + ptr::write(slice.as_mut_ptr().add(idx), val); } unsafe fn slice_remove<T>(slice: &mut [T], idx: usize) -> T { diff --git a/third_party/rust/fallible_collections/src/lib.rs b/third_party/rust/fallible_collections/src/lib.rs index 45dcd48d17..9f525d71b7 100644 --- a/third_party/rust/fallible_collections/src/lib.rs +++ b/third_party/rust/fallible_collections/src/lib.rs @@ -22,16 +22,22 @@ //! can't return a Result to indicate allocation failure. #![cfg_attr(not(test), no_std)] -#![cfg_attr(all(feature = "unstable", not(feature = "rust_1_57")), feature(try_reserve))] +#![cfg_attr(feature = "unstable", feature(try_reserve_kind))] #![cfg_attr(feature = "unstable", feature(min_specialization))] #![cfg_attr(feature = "unstable", feature(allocator_api))] #![cfg_attr(feature = "unstable", feature(dropck_eyepatch))] #![cfg_attr(feature = "unstable", feature(ptr_internals))] #![cfg_attr(feature = "unstable", feature(core_intrinsics))] -#![cfg_attr(all(feature = "unstable", not(feature = "rust_1_57")), feature(maybe_uninit_ref))] #![cfg_attr(feature = "unstable", feature(maybe_uninit_slice))] -#![cfg_attr(feature = "unstable", feature(maybe_uninit_extra))] #![cfg_attr(feature = "unstable", feature(maybe_uninit_uninit_array))] + +#[cfg(all(feature = "unstable", feature = "rust_1_57"))] +compile_error!( + "The use of the 'unstable' feature combined with the \ +'rust_1_57' feature, which is related to the partial stabilization \ +of the allocator API since rustc version 1.57, does not make sense!" +); + extern crate alloc; #[cfg(feature = "std")] extern crate std; @@ -47,18 +53,16 @@ pub mod arc; pub use arc::*; #[cfg(feature = "unstable")] pub mod btree; -#[cfg(not(feature = "unstable"))] +#[cfg(all(feature = "hashmap", not(feature = "unstable")))] pub mod hashmap; -#[cfg(not(feature = "unstable"))] +#[cfg(all(feature = "hashmap", not(feature = "unstable")))] pub use hashmap::*; #[macro_use] pub mod format; pub mod try_clone; -#[cfg(all(feature = "unstable", not(feature = "rust_1_57")))] -pub use alloc::collections::TryReserveError; -#[cfg(not(all(feature = "unstable", not(feature = "rust_1_57"))))] -pub use hashbrown::TryReserveError; +pub mod try_reserve_error; +pub use try_reserve_error::TryReserveError; #[cfg(feature = "std_io")] pub use vec::std_io::*; @@ -81,7 +85,7 @@ pub trait TryClone { } #[cfg(feature = "rust_1_57")] -fn make_try_reserve_error(len: usize, additional: usize, elem_size: usize, align: usize) -> hashbrown::TryReserveError { +fn make_try_reserve_error(len: usize, additional: usize, elem_size: usize, align: usize) -> TryReserveError { if let Some(size) = len.checked_add(additional).and_then(|l| l.checked_mul(elem_size)) { if let Ok(layout) = alloc::alloc::Layout::from_size_align(size, align) { return TryReserveError::AllocError { layout } diff --git a/third_party/rust/fallible_collections/src/try_reserve_error.rs b/third_party/rust/fallible_collections/src/try_reserve_error.rs new file mode 100644 index 0000000000..ec3a998f2e --- /dev/null +++ b/third_party/rust/fallible_collections/src/try_reserve_error.rs @@ -0,0 +1,19 @@ +#[cfg(all(feature = "unstable", not(feature = "rust_1_57")))] +pub use alloc::collections::TryReserveError; +#[cfg(all(feature = "hashmap", not(all(feature = "unstable", not(feature = "rust_1_57")))))] +pub use hashbrown::TryReserveError; + +/// The error type for `try_reserve` methods. +#[cfg(all(not(feature = "hashmap"), not(all(feature = "unstable", not(feature = "rust_1_57")))))] +#[derive(Clone, PartialEq, Eq, Debug)] +pub enum TryReserveError { + /// Error due to the computed capacity exceeding the collection's maximum + /// (usually `isize::MAX` bytes). + CapacityOverflow, + + /// The memory allocator returned an error + AllocError { + /// The layout of the allocation request that failed. + layout: alloc::alloc::Layout, + }, +}
\ No newline at end of file diff --git a/third_party/rust/fallible_collections/src/vec.rs b/third_party/rust/fallible_collections/src/vec.rs index 6197b5b5d6..d4ed4df74c 100644 --- a/third_party/rust/fallible_collections/src/vec.rs +++ b/third_party/rust/fallible_collections/src/vec.rs @@ -515,7 +515,7 @@ impl<T> FallibleVec<T> for Vec<T> { self.try_reserve(additional) } - #[cfg(not(feature = "rust_1_57"))] + #[cfg(all(not(feature = "unstable"), not(feature = "rust_1_57")))] { vec_try_reserve(self, additional) } @@ -624,7 +624,7 @@ impl<T> FallibleVec<T> for Vec<T> { let mut iterator = other.iter(); while let Some(element) = iterator.next() { unsafe { - core::ptr::write(self.get_unchecked_mut(len), element.try_clone()?); + core::ptr::write(self.as_mut_ptr().add(len), element.try_clone()?); // NB can't overflow since we would have had to alloc the address space len += 1; self.set_len(len); |