Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Commit c20cb95

Browse files
committed
Use Panic for associated type rather than ! for clarity
1 parent ade4b1e commit c20cb95

File tree

8 files changed

+100
-90
lines changed

8 files changed

+100
-90
lines changed

src/alloc/abort.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ use core::ptr::NonNull;
2424
#[derive(Copy, Clone, Debug, Default)]
2525
pub struct AbortAlloc<Alloc>(pub Alloc);
2626

27+
/// A synonym used to indicate the impossible case will be a panic instead.
28+
///
29+
/// `!` in rust really does mean "never" / "impossible", but nothing in the type
30+
/// system tracks panicking.
31+
pub type Panic = !;
32+
2733
impl<A: BuildAllocRef> BuildAllocRef for AbortAlloc<A> {
2834
type Ref = AbortAlloc<A::Ref>;
2935

@@ -49,7 +55,7 @@ impl<A: DeallocRef> DeallocRef for AbortAlloc<A> {
4955
}
5056

5157
impl<A: AllocRef> AllocRef for AbortAlloc<A> {
52-
type Error = !;
58+
type Error = Panic;
5359

5460
fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
5561
self.0

src/alloc/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod abort;
22
mod layout;
33

44
pub use self::{
5-
abort::AbortAlloc,
5+
abort::{AbortAlloc, Panic},
66
layout::{LayoutErr, NonZeroLayout},
77
};
88
pub use core::alloc::GlobalAlloc;

src/boxed.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
//! [`NonZeroLayout::for_value(&*value)`]: crate::alloc::NonZeroLayout::for_value
8080
8181
use crate::{
82-
alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout},
82+
alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout, Panic},
8383
clone::CloneIn,
8484
collections::CollectionAllocErr,
8585
raw_vec::RawVec,
@@ -188,7 +188,7 @@ impl<T, A: AllocRef> Box<T, A> {
188188
#[inline(always)]
189189
pub fn new_in(x: T, a: A) -> Self
190190
where
191-
A: AllocRef<Error = !>,
191+
A: AllocRef<Error = Panic>,
192192
{
193193
let Ok(b) = Self::try_new_in(x, a);
194194
b
@@ -245,7 +245,7 @@ impl<T, A: AllocRef> Box<T, A> {
245245
#[inline(always)]
246246
pub fn new_uninit_in(a: A) -> Box<mem::MaybeUninit<T>, A>
247247
where
248-
A: AllocRef<Error = !>,
248+
A: AllocRef<Error = Panic>,
249249
{
250250
let Ok(b) = Self::try_new_uninit_in(a);
251251
b
@@ -286,7 +286,7 @@ impl<T, A: AllocRef> Box<T, A> {
286286
#[inline(always)]
287287
pub fn pin_in(x: T, a: A) -> Pin<Self>
288288
where
289-
A: AllocRef<Error = !>,
289+
A: AllocRef<Error = Panic>,
290290
{
291291
let Ok(b) = Self::try_pin_in(x, a);
292292
b
@@ -331,7 +331,7 @@ impl<T> Box<[T]> {
331331
}
332332

333333
#[allow(clippy::use_self)]
334-
impl<T, A: AllocRef<Error = !>> Box<[T], A> {
334+
impl<T, A: AllocRef<Error = Panic>> Box<[T], A> {
335335
/// Construct a new boxed slice with uninitialized contents with the spoecified allocator.
336336
///
337337
/// # Example
@@ -775,7 +775,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: DeallocRef> Drop for Box<T, A> {
775775
impl<T, A> Default for Box<T, A>
776776
where
777777
T: Default,
778-
A: Default + AllocRef<Error = !>,
778+
A: Default + AllocRef<Error = Panic>,
779779
{
780780
#[must_use]
781781
fn default() -> Self {
@@ -784,9 +784,9 @@ where
784784
}
785785

786786
#[allow(clippy::use_self)]
787-
impl<T, A: AllocRef<Error = !>> Default for Box<[T], A>
787+
impl<T, A: AllocRef<Error = Panic>> Default for Box<[T], A>
788788
where
789-
A: Default + AllocRef<Error = !>,
789+
A: Default + AllocRef<Error = Panic>,
790790
{
791791
#[must_use]
792792
fn default() -> Self {
@@ -801,19 +801,19 @@ unsafe fn from_boxed_utf8_unchecked<A: DeallocRef>(v: Box<[u8], A>) -> Box<str,
801801
}
802802

803803
#[allow(clippy::use_self)]
804-
impl<A: AllocRef<Error = !>> Default for Box<str, A>
804+
impl<A: AllocRef<Error = Panic>> Default for Box<str, A>
805805
where
806-
A: Default + AllocRef<Error = !>,
806+
A: Default + AllocRef<Error = Panic>,
807807
{
808808
#[must_use]
809809
fn default() -> Self {
810810
unsafe { from_boxed_utf8_unchecked(Box::default()) }
811811
}
812812
}
813813

814-
impl<T: Clone, A: AllocRef<Error = !>> Clone for Box<T, A>
814+
impl<T: Clone, A: AllocRef<Error = Panic>> Clone for Box<T, A>
815815
where
816-
A: AllocRef<Error = !>,
816+
A: AllocRef<Error = Panic>,
817817
A::BuildAlloc: Clone,
818818
{
819819
/// Returns a new box with a `clone()` of this box's contents.
@@ -876,7 +876,7 @@ impl<T: Clone, A: AllocRef, B: AllocRef> CloneIn<B> for Box<T, A> {
876876

877877
fn clone_in(&self, a: B) -> Self::Cloned
878878
where
879-
B: AllocRef<Error = !>,
879+
B: AllocRef<Error = Panic>,
880880
{
881881
Box::new_in(self.as_ref().clone(), a)
882882
}
@@ -978,9 +978,9 @@ impl<T: ?Sized + Hasher, A: DeallocRef> Hasher for Box<T, A> {
978978
}
979979
}
980980

981-
impl<T, A: AllocRef<Error = !>> From<T> for Box<T, A>
981+
impl<T, A: AllocRef<Error = Panic>> From<T> for Box<T, A>
982982
where
983-
A: Default + AllocRef<Error = !>,
983+
A: Default + AllocRef<Error = Panic>,
984984
{
985985
/// Converts a generic type `T` into a `Box<T>`
986986
///
@@ -1014,7 +1014,7 @@ impl<T: ?Sized, A: DeallocRef> From<Box<T, A>> for Pin<Box<T, A>> {
10141014
#[allow(clippy::use_self)]
10151015
impl<T: Copy, A> From<&[T]> for Box<[T], A>
10161016
where
1017-
A: Default + AllocRef<Error = !>,
1017+
A: Default + AllocRef<Error = Panic>,
10181018
{
10191019
/// Converts a `&[T]` into a `Box<[T], B>`
10201020
///
@@ -1043,7 +1043,7 @@ where
10431043
#[allow(clippy::use_self)]
10441044
impl<A> From<&str> for Box<str, A>
10451045
where
1046-
A: Default + AllocRef<Error = !>,
1046+
A: Default + AllocRef<Error = Panic>,
10471047
{
10481048
/// Converts a `&str` into a `Box<str>`
10491049
///
@@ -1306,7 +1306,7 @@ impl_dispatch_from_dyn!(AbortAlloc<std::alloc::System>);
13061306
#[allow(clippy::items_after_statements)]
13071307
impl<T: Clone, A: Clone> Clone for Box<[T], A>
13081308
where
1309-
A: AllocRef<Error = !>,
1309+
A: AllocRef<Error = Panic>,
13101310
A::BuildAlloc: Clone,
13111311
{
13121312
fn clone(&self) -> Self {

src/clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::alloc::AllocRef;
1+
use crate::alloc::{AllocRef, Panic};
22

33
pub trait CloneIn<A: AllocRef>: Sized {
44
type Cloned;
55

66
fn clone_in(&self, a: A) -> Self::Cloned
77
where
8-
A: AllocRef<Error = !>;
8+
A: AllocRef<Error = Panic>;
99

1010
fn try_clone_in(&self, a: A) -> Result<Self::Cloned, A::Error>;
1111
}

src/iter.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{alloc::AllocRef, collections::CollectionAllocErr};
1+
use crate::{
2+
alloc::{AllocRef, Panic},
3+
collections::CollectionAllocErr,
4+
};
25

36
/// Extend a collection "fallibly" with the contents of an iterator.
47
pub trait TryExtend<A> {
@@ -27,7 +30,7 @@ pub trait TryExtend<A> {
2730
pub trait FromIteratorIn<T, A: AllocRef> {
2831
fn from_iter_in<I: IntoIterator<Item = T>>(iter: I, allocator: A) -> Self
2932
where
30-
A: AllocRef<Error = !>;
33+
A: AllocRef<Error = Panic>;
3134

3235
fn try_from_iter_in<I: IntoIterator<Item = T>>(
3336
iter: I,
@@ -42,7 +45,7 @@ pub trait IteratorExt: Iterator + Sized {
4245
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
4346
fn collect_in<T: FromIteratorIn<Self::Item, A>, A: AllocRef>(self, allocator: A) -> T
4447
where
45-
A: AllocRef<Error = !>,
48+
A: AllocRef<Error = Panic>,
4649
{
4750
FromIteratorIn::from_iter_in(self, allocator)
4851
}

src/raw_vec.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
DeallocRef,
88
Global,
99
NonZeroLayout,
10+
Panic,
1011
ReallocRef,
1112
},
1213
boxed::Box,
@@ -162,7 +163,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
162163
/// * on 32-bit platforms if the requested capacity exceeds `isize::MAX` bytes.
163164
pub fn with_capacity_in(capacity: usize, a: A) -> Self
164165
where
165-
A: AllocRef<Error = !>,
166+
A: AllocRef<Error = Panic>,
166167
{
167168
match Self::try_with_capacity_in(capacity, a) {
168169
Ok(vec) => vec,
@@ -198,7 +199,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
198199
/// * on 32-bit platforms if the requested capacity exceeds `isize::MAX` bytes.
199200
pub fn with_capacity_zeroed_in(capacity: usize, a: A) -> Self
200201
where
201-
A: AllocRef<Error = !>,
202+
A: AllocRef<Error = Panic>,
202203
{
203204
match Self::try_with_capacity_zeroed_in(capacity, a) {
204205
Ok(vec) => vec,
@@ -419,7 +420,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
419420
/// ```
420421
pub fn double(&mut self)
421422
where
422-
A: ReallocRef<Error = !>,
423+
A: ReallocRef<Error = Panic>,
423424
{
424425
match self.try_double() {
425426
Ok(_) => (),
@@ -590,7 +591,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
590591
/// ```
591592
pub fn reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize)
592593
where
593-
A: ReallocRef<Error = !>,
594+
A: ReallocRef<Error = Panic>,
594595
{
595596
match self.try_reserve(used_capacity, needed_extra_capacity) {
596597
Ok(vec) => vec,
@@ -634,7 +635,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
634635
/// * on 32-bit platforms if the requested capacity exceeds `isize::MAX` bytes.
635636
pub fn reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize)
636637
where
637-
A: ReallocRef<Error = !>,
638+
A: ReallocRef<Error = Panic>,
638639
{
639640
match self.try_reserve_exact(used_capacity, needed_extra_capacity) {
640641
Ok(_) => (),
@@ -736,7 +737,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
736737
/// Panics if the given amount is *larger* than the current capacity.
737738
pub fn shrink_to_fit(&mut self, amount: usize)
738739
where
739-
A: ReallocRef<Error = !>,
740+
A: ReallocRef<Error = Panic>,
740741
{
741742
match self.try_shrink_to_fit(amount) {
742743
Ok(_) => (),

0 commit comments

Comments
 (0)