diff --git a/src/alloc/abort.rs b/src/alloc/abort.rs index 21678d3..d7aed1b 100644 --- a/src/alloc/abort.rs +++ b/src/alloc/abort.rs @@ -24,6 +24,12 @@ use core::ptr::NonNull; #[derive(Copy, Clone, Debug, Default)] pub struct AbortAlloc(pub Alloc); +/// A synonym used to indicate the impossible case will be a panic instead. +/// +/// `!` in rust really does mean "never" / "impossible", but nothing in the type +/// system tracks panicking. +pub type Panic = !; + impl BuildAllocRef for AbortAlloc { type Ref = AbortAlloc; @@ -49,7 +55,7 @@ impl DeallocRef for AbortAlloc { } impl AllocRef for AbortAlloc { - type Error = !; + type Error = Panic; fn alloc(&mut self, layout: NonZeroLayout) -> Result, Self::Error> { self.0 diff --git a/src/alloc/mod.rs b/src/alloc/mod.rs index 5870301..5882151 100644 --- a/src/alloc/mod.rs +++ b/src/alloc/mod.rs @@ -2,7 +2,7 @@ mod abort; mod layout; pub use self::{ - abort::AbortAlloc, + abort::{AbortAlloc, Panic}, layout::{LayoutErr, NonZeroLayout}, }; pub use core::alloc::GlobalAlloc; diff --git a/src/boxed.rs b/src/boxed.rs index f2e9285..2a4ed93 100644 --- a/src/boxed.rs +++ b/src/boxed.rs @@ -79,7 +79,7 @@ //! [`NonZeroLayout::for_value(&*value)`]: crate::alloc::NonZeroLayout::for_value use crate::{ - alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout}, + alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout, Panic}, clone::CloneIn, collections::CollectionAllocErr, raw_vec::RawVec, @@ -188,7 +188,7 @@ impl Box { #[inline(always)] pub fn new_in(x: T, a: A) -> Self where - A: AllocRef, + A: AllocRef, { let Ok(b) = Self::try_new_in(x, a); b @@ -245,7 +245,7 @@ impl Box { #[inline(always)] pub fn new_uninit_in(a: A) -> Box, A> where - A: AllocRef, + A: AllocRef, { let Ok(b) = Self::try_new_uninit_in(a); b @@ -286,7 +286,7 @@ impl Box { #[inline(always)] pub fn pin_in(x: T, a: A) -> Pin where - A: AllocRef, + A: AllocRef, { let Ok(b) = Self::try_pin_in(x, a); b @@ -331,7 +331,7 @@ impl Box<[T]> { } #[allow(clippy::use_self)] -impl> Box<[T], A> { +impl> Box<[T], A> { /// Construct a new boxed slice with uninitialized contents with the spoecified allocator. /// /// # Example @@ -775,7 +775,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: DeallocRef> Drop for Box { impl Default for Box where T: Default, - A: Default + AllocRef, + A: Default + AllocRef, { #[must_use] fn default() -> Self { @@ -784,9 +784,9 @@ where } #[allow(clippy::use_self)] -impl> Default for Box<[T], A> +impl> Default for Box<[T], A> where - A: Default + AllocRef, + A: Default + AllocRef, { #[must_use] fn default() -> Self { @@ -801,9 +801,9 @@ unsafe fn from_boxed_utf8_unchecked(v: Box<[u8], A>) -> Box> Default for Box +impl> Default for Box where - A: Default + AllocRef, + A: Default + AllocRef, { #[must_use] fn default() -> Self { @@ -811,9 +811,9 @@ where } } -impl> Clone for Box +impl> Clone for Box where - A: AllocRef, + A: AllocRef, A::BuildAlloc: Clone, { /// Returns a new box with a `clone()` of this box's contents. @@ -876,7 +876,7 @@ impl CloneIn for Box { fn clone_in(&self, a: B) -> Self::Cloned where - B: AllocRef, + B: AllocRef, { Box::new_in(self.as_ref().clone(), a) } @@ -978,9 +978,9 @@ impl Hasher for Box { } } -impl> From for Box +impl> From for Box where - A: Default + AllocRef, + A: Default + AllocRef, { /// Converts a generic type `T` into a `Box` /// @@ -1014,7 +1014,7 @@ impl From> for Pin> { #[allow(clippy::use_self)] impl From<&[T]> for Box<[T], A> where - A: Default + AllocRef, + A: Default + AllocRef, { /// Converts a `&[T]` into a `Box<[T], B>` /// @@ -1043,7 +1043,7 @@ where #[allow(clippy::use_self)] impl From<&str> for Box where - A: Default + AllocRef, + A: Default + AllocRef, { /// Converts a `&str` into a `Box` /// @@ -1306,7 +1306,7 @@ impl_dispatch_from_dyn!(AbortAlloc); #[allow(clippy::items_after_statements)] impl Clone for Box<[T], A> where - A: AllocRef, + A: AllocRef, A::BuildAlloc: Clone, { fn clone(&self) -> Self { diff --git a/src/clone.rs b/src/clone.rs index 18b43d5..ee4a970 100644 --- a/src/clone.rs +++ b/src/clone.rs @@ -1,11 +1,11 @@ -use crate::alloc::AllocRef; +use crate::alloc::{AllocRef, Panic}; pub trait CloneIn: Sized { type Cloned; fn clone_in(&self, a: A) -> Self::Cloned where - A: AllocRef; + A: AllocRef; fn try_clone_in(&self, a: A) -> Result; } diff --git a/src/iter.rs b/src/iter.rs index d511f34..5d1421f 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -1,4 +1,4 @@ -use crate::{alloc::AllocRef, collections::CollectionAllocErr}; +use crate::{alloc::{AllocRef, Panic}, collections::CollectionAllocErr}; /// Extend a collection "fallibly" with the contents of an iterator. pub trait TryExtend { @@ -27,7 +27,7 @@ pub trait TryExtend { pub trait FromIteratorIn { fn from_iter_in>(iter: I, allocator: A) -> Self where - A: AllocRef; + A: AllocRef; fn try_from_iter_in>( iter: I, @@ -42,7 +42,7 @@ pub trait IteratorExt: Iterator + Sized { #[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"] fn collect_in, A: AllocRef>(self, allocator: A) -> T where - A: AllocRef, + A: AllocRef, { FromIteratorIn::from_iter_in(self, allocator) } diff --git a/src/raw_vec.rs b/src/raw_vec.rs index 1f41254..3ba7ab0 100644 --- a/src/raw_vec.rs +++ b/src/raw_vec.rs @@ -8,6 +8,7 @@ use crate::{ Global, NonZeroLayout, ReallocRef, + Panic, }, boxed::Box, collections::CollectionAllocErr, @@ -162,7 +163,7 @@ impl RawVec { /// * on 32-bit platforms if the requested capacity exceeds `isize::MAX` bytes. pub fn with_capacity_in(capacity: usize, a: A) -> Self where - A: AllocRef, + A: AllocRef, { match Self::try_with_capacity_in(capacity, a) { Ok(vec) => vec, @@ -198,7 +199,7 @@ impl RawVec { /// * on 32-bit platforms if the requested capacity exceeds `isize::MAX` bytes. pub fn with_capacity_zeroed_in(capacity: usize, a: A) -> Self where - A: AllocRef, + A: AllocRef, { match Self::try_with_capacity_zeroed_in(capacity, a) { Ok(vec) => vec, @@ -419,7 +420,7 @@ impl RawVec { /// ``` pub fn double(&mut self) where - A: ReallocRef, + A: ReallocRef, { match self.try_double() { Ok(_) => (), @@ -590,7 +591,7 @@ impl RawVec { /// ``` pub fn reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize) where - A: ReallocRef, + A: ReallocRef, { match self.try_reserve(used_capacity, needed_extra_capacity) { Ok(vec) => vec, @@ -634,7 +635,7 @@ impl RawVec { /// * on 32-bit platforms if the requested capacity exceeds `isize::MAX` bytes. pub fn reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize) where - A: ReallocRef, + A: ReallocRef, { match self.try_reserve_exact(used_capacity, needed_extra_capacity) { Ok(_) => (), @@ -736,7 +737,7 @@ impl RawVec { /// Panics if the given amount is *larger* than the current capacity. pub fn shrink_to_fit(&mut self, amount: usize) where - A: ReallocRef, + A: ReallocRef, { match self.try_shrink_to_fit(amount) { Ok(_) => (), diff --git a/src/string.rs b/src/string.rs index 71e01a4..ed0bca7 100644 --- a/src/string.rs +++ b/src/string.rs @@ -52,7 +52,7 @@ //! ``` use crate::{ - alloc::{AbortAlloc, AllocRef, DeallocRef, Global, ReallocRef}, + alloc::{AbortAlloc, AllocRef, DeallocRef, Global, ReallocRef, Panic}, boxed::Box, collections::CollectionAllocErr, iter::TryExtend, @@ -580,7 +580,7 @@ impl String { #[inline] pub fn with_capacity_in(capacity: usize, a: A) -> Self where - A: AllocRef, + A: AllocRef, { match Self::try_with_capacity_in(capacity, a) { Ok(vec) => vec, @@ -608,7 +608,7 @@ impl String { #[inline] pub fn from_str_in(s: &str, a: A) -> Self where - A: ReallocRef, + A: ReallocRef, { let mut v = Self::with_capacity_in(s.len(), a); v.push_str(s); @@ -708,7 +708,7 @@ impl String { /// Panics if allocation fails. pub fn from_utf8_lossy_in(v: &[u8], a: A) -> Self where - A: ReallocRef, + A: ReallocRef, { match Self::try_from_utf8_lossy_in(v, a) { Ok(s) => s, @@ -757,7 +757,7 @@ impl String { /// Like `from_utf16` but parameterized over the choice of allocator for the returned `String`. pub fn from_utf16_in(v: &[u16], a: A) -> Result where - A: ReallocRef, + A: ReallocRef, { // This isn't done via collect::>() for performance reasons. // FIXME: the function can be simplified again when #48994 is closed. @@ -932,7 +932,7 @@ impl String { #[inline] pub fn push_str(&mut self, string: &str) where - A: ReallocRef, + A: ReallocRef, { self.vec.extend_from_slice(string.as_bytes()) } @@ -1016,7 +1016,7 @@ impl String { #[inline] pub fn reserve(&mut self, additional: usize) where - A: ReallocRef, + A: ReallocRef, { self.vec.reserve(additional) } @@ -1069,7 +1069,7 @@ impl String { #[inline] pub fn reserve_exact(&mut self, additional: usize) where - A: ReallocRef, + A: ReallocRef, { self.vec.reserve_exact(additional) } @@ -1180,7 +1180,7 @@ impl String { #[inline] pub fn shrink_to_fit(&mut self) where - A: ReallocRef, + A: ReallocRef, { self.vec.shrink_to_fit() } @@ -1224,7 +1224,7 @@ impl String { #[inline] pub fn shrink_to(&mut self, min_capacity: usize) where - A: ReallocRef, + A: ReallocRef, { self.vec.shrink_to(min_capacity) } @@ -1263,7 +1263,7 @@ impl String { #[inline] pub fn push(&mut self, ch: char) where - A: ReallocRef, + A: ReallocRef, { match self.try_push(ch) { Ok(s) => s, @@ -1516,7 +1516,7 @@ impl String { #[inline] pub fn insert(&mut self, idx: usize, ch: char) where - A: ReallocRef, + A: ReallocRef, { match self.try_insert(idx, ch) { Ok(s) => s, @@ -1592,7 +1592,7 @@ impl String { #[inline] pub fn insert_str(&mut self, idx: usize, string: &str) where - A: ReallocRef, + A: ReallocRef, { match self.try_insert_str(idx, string) { Ok(s) => s, @@ -1606,7 +1606,7 @@ impl String { #[inline] pub fn try_insert_str(&mut self, idx: usize, string: &str) -> Result<(), CollectionAllocErr> where - A: ReallocRef, + A: ReallocRef, { assert!(self.is_char_boundary(idx)); @@ -1716,7 +1716,7 @@ impl String { #[inline] pub fn split_off(&mut self, at: usize) -> Self where - A: AllocRef, + A: AllocRef, { match self.try_split_off(at) { Ok(s) => s, @@ -1856,7 +1856,7 @@ impl String { pub fn replace_range(&mut self, range: R, replace_with: &str) where R: RangeBounds, - A: ReallocRef, + A: ReallocRef, { // Memory safety // @@ -1901,7 +1901,7 @@ impl String { #[inline] pub fn into_boxed_str(self) -> Box where - A: ReallocRef, + A: ReallocRef, { let slice = self.vec.into_boxed_slice(); unsafe { from_boxed_utf8_unchecked(slice) } @@ -2007,7 +2007,7 @@ impl fmt::Display for FromUtf16Error { } } -impl> Clone for String +impl> Clone for String where A: AllocRef, A::BuildAlloc: Clone, @@ -2033,7 +2033,7 @@ impl CloneIn for String { #[must_use = "Cloning is expected to be expensive"] fn clone_in(&self, a: B) -> Self::Cloned where - B: AllocRef, + B: AllocRef, { String { vec: self.vec.clone_in(a), @@ -2110,7 +2110,7 @@ impl<'a> FromIterator> for String { impl Extend for String where - A: ReallocRef, + A: ReallocRef, { fn extend>(&mut self, iter: I) { let iterator = iter.into_iter(); @@ -2134,7 +2134,7 @@ impl TryExtend for String { impl<'a, A> Extend<&'a char> for String where - A: ReallocRef, + A: ReallocRef, { fn extend>(&mut self, iter: I) { self.extend(iter.into_iter().cloned()); @@ -2151,7 +2151,7 @@ impl<'a, A: ReallocRef> TryExtend<&'a char> for String { impl<'a, A> Extend<&'a str> for String where - A: ReallocRef, + A: ReallocRef, { fn extend>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(s)); @@ -2168,7 +2168,7 @@ impl<'a, A: ReallocRef> TryExtend<&'a str> for String { impl Extend> for String where - A: ReallocRef, + A: ReallocRef, B: DeallocRef, { fn extend>>(&mut self, iter: I) { @@ -2188,7 +2188,7 @@ impl TryExtend> for String { #[cfg(feature = "std")] impl<'a, A> Extend> for String where - A: ReallocRef, + A: ReallocRef, { fn extend>>(&mut self, iter: I) { iter.into_iter().for_each(move |s| self.push_str(&s)); @@ -2324,7 +2324,7 @@ impl hash::Hash for String { /// ``` impl Add<&str> for String where - A: ReallocRef, + A: ReallocRef, { type Output = Self; @@ -2340,7 +2340,7 @@ where /// This has the same behavior as the [`push_str`][`String::push_str`] method. impl AddAssign<&str> for String where - A: ReallocRef, + A: ReallocRef, { #[inline] fn add_assign(&mut self, other: &str) { @@ -2516,7 +2516,7 @@ impl From> for String { impl From> for Box where - A: ReallocRef, + A: ReallocRef, { /// Converts the given `String` to a boxed `str` slice that is owned. /// diff --git a/src/vec.rs b/src/vec.rs index 0e35199..238e938 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -69,7 +69,7 @@ //! [`vec!`]: ../macro.vec.html use crate::{ - alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, ReallocRef}, + alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, ReallocRef, Panic}, boxed::Box, clone::CloneIn, collections::CollectionAllocErr, @@ -490,7 +490,7 @@ impl Vec { #[inline] pub fn with_capacity_in(capacity: usize, a: A) -> Self where - A: AllocRef, + A: AllocRef, { Self { buf: RawVec::with_capacity_in(capacity, a), @@ -612,7 +612,7 @@ impl Vec { /// Panics if the reallocation fails. pub fn reserve(&mut self, additional: usize) where - A: ReallocRef, + A: ReallocRef, { self.buf.reserve(self.len, additional); } @@ -645,7 +645,7 @@ impl Vec { /// Panics if the reallocation fails. pub fn reserve_exact(&mut self, additional: usize) where - A: ReallocRef, + A: ReallocRef, { self.buf.reserve_exact(self.len, additional); } @@ -759,7 +759,7 @@ impl Vec { /// Panics if the reallocation fails. pub fn shrink_to_fit(&mut self) where - A: ReallocRef, + A: ReallocRef, { if self.capacity() != self.len { self.buf.shrink_to_fit(self.len); @@ -805,7 +805,7 @@ impl Vec { /// * Panics if the reallocation fails. pub fn shrink_to(&mut self, min_capacity: usize) where - A: ReallocRef, + A: ReallocRef, { self.buf.shrink_to_fit(cmp::max(self.len, min_capacity)); } @@ -852,7 +852,7 @@ impl Vec { /// Panics if the reallocation fails. pub fn into_boxed_slice(self) -> Box<[T], A> where - A: ReallocRef, + A: ReallocRef, { match self.try_into_boxed_slice() { Ok(vec) => vec, @@ -1197,7 +1197,7 @@ impl Vec { /// ``` pub fn insert(&mut self, index: usize, element: T) where - A: ReallocRef, + A: ReallocRef, { match self.try_insert(index, element) { Ok(vec) => vec, @@ -1304,7 +1304,7 @@ impl Vec { pub fn retain(&mut self, mut f: F) where F: FnMut(&T) -> bool, - A: ReallocRef, + A: ReallocRef, { self.drain_filter(|x| !f(x)); } @@ -1387,7 +1387,7 @@ impl Vec { #[inline] pub fn push(&mut self, value: T) where - A: ReallocRef, + A: ReallocRef, { match self.try_push(value) { Ok(vec) => vec, @@ -1473,7 +1473,7 @@ impl Vec { #[inline] pub fn append(&mut self, other: &mut Self) where - A: ReallocRef, + A: ReallocRef, { match self.try_append(other) { Ok(vec) => vec, @@ -1659,7 +1659,7 @@ impl Vec { #[inline] pub fn split_off(&mut self, at: usize) -> Self where - A: AllocRef, + A: AllocRef, { match self.try_split_off(at) { Ok(vec) => vec, @@ -1730,7 +1730,7 @@ impl Vec { pub fn resize_with(&mut self, new_len: usize, f: F) where F: FnMut() -> T, - A: ReallocRef, + A: ReallocRef, { match self.try_resize_with(new_len, f) { Ok(vec) => vec, @@ -1785,7 +1785,7 @@ impl Vec { pub fn leak<'a>(vec: Self) -> &'a mut [T] where T: 'a, // Technically not needed, but kept to be explicit. - A: ReallocRef, + A: ReallocRef, { Box::leak(vec.into_boxed_slice()) } @@ -1834,7 +1834,7 @@ impl Vec { /// Panics if the reallocation fails. pub fn resize(&mut self, new_len: usize, value: T) where - A: ReallocRef, + A: ReallocRef, { match self.try_resize(new_len, value) { Ok(_) => (), @@ -1882,7 +1882,7 @@ impl Vec { /// Panics if the reallocation fails. pub fn extend_from_slice(&mut self, other: &[T]) where - A: ReallocRef, + A: ReallocRef, { self.spec_extend(other.iter().cloned()) } @@ -2051,7 +2051,7 @@ pub fn from_elem(elem: T, n: usize) -> Vec { #[doc(hidden)] pub fn from_elem_in(elem: T, n: usize, a: A) -> Vec where - A: ReallocRef, + A: ReallocRef, { match try_from_elem_in(elem, n, a) { Ok(vec) => vec, @@ -2199,7 +2199,7 @@ unsafe impl IsZero for Option> { // Common trait implementations for Vec //////////////////////////////////////////////////////////////////////////////// -impl> Clone for Vec +impl> Clone for Vec where A: AllocRef, A::BuildAlloc: Clone, @@ -2224,7 +2224,7 @@ impl CloneIn for Vec { fn clone_in(&self, a: B) -> Self::Cloned where - B: AllocRef, + B: AllocRef, { let Ok(v) = self.try_clone_in(a); v @@ -2300,7 +2300,7 @@ impl FromIteratorIn for Vec { #[must_use] fn from_iter_in>(iter: I, a: A) -> Self where - A: AllocRef, + A: AllocRef, { >::from_iter_in(iter.into_iter(), a) } @@ -2376,7 +2376,7 @@ impl<'a, T, A: DeallocRef> IntoIterator for &'a mut Vec { impl Extend for Vec where - A: ReallocRef, + A: ReallocRef, { #[inline] fn extend>(&mut self, iter: I) { @@ -2397,7 +2397,7 @@ trait SpecExtend: Sized { #[inline] fn from_iter_in(iter: I, a: A) -> Self where - A: AllocRef, + A: AllocRef, { match Self::try_from_iter_in(iter, a) { Ok(v) => v, @@ -2412,7 +2412,7 @@ trait SpecExtend: Sized { #[inline] fn spec_extend(&mut self, iter: I) where - A: AllocRef, + A: AllocRef, { match self.try_spec_extend(iter) { Ok(_) => (), @@ -2625,7 +2625,7 @@ impl Vec { #[inline] pub fn splice(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter, A> where - A: ReallocRef, + A: ReallocRef, R: RangeBounds, I: IntoIterator, { @@ -2684,7 +2684,7 @@ impl Vec { /// ``` pub fn drain_filter(&mut self, filter: F) -> DrainFilter<'_, T, F, A> where - A: ReallocRef, + A: ReallocRef, F: FnMut(&mut T) -> bool, { let old_len = self.len(); @@ -2713,7 +2713,7 @@ impl Vec { /// [`copy_from_slice`]: ../../std/primitive.slice.html#method.copy_from_slice impl<'a, T: 'a + Copy, A> Extend<&'a T> for Vec where - A: ReallocRef, + A: ReallocRef, { fn extend>(&mut self, iter: I) { self.extend(iter.into_iter().cloned()) @@ -3172,7 +3172,7 @@ impl FusedIterator for Drain<'_, T, A> {} #[derive(Debug)] pub struct Splice<'a, I: Iterator + 'a, A = AbortAlloc> where - A: ReallocRef, // Because Drop can allocate + A: ReallocRef, // Because Drop can allocate { drain: Drain<'a, I::Item, A>, replace_with: I, @@ -3180,7 +3180,7 @@ where impl Iterator for Splice<'_, I, A> where - A: ReallocRef, + A: ReallocRef, { type Item = I::Item; @@ -3195,18 +3195,18 @@ where impl DoubleEndedIterator for Splice<'_, I, A> where - A: ReallocRef, + A: ReallocRef, { fn next_back(&mut self) -> Option { self.drain.next_back() } } -impl ExactSizeIterator for Splice<'_, I, A> where A: ReallocRef {} +impl ExactSizeIterator for Splice<'_, I, A> where A: ReallocRef {} impl Drop for Splice<'_, I, A> where - A: ReallocRef, + A: ReallocRef, { fn drop(&mut self) { self.drain.by_ref().for_each(drop); @@ -3252,7 +3252,7 @@ where /// Private helper methods for `Splice::drop` impl Drain<'_, T, A> where - A: ReallocRef, + A: ReallocRef, { /// The range from `self.vec.len` to `self.tail_start` contains elements /// that have been moved out.