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

Commit

Permalink
Use Panic for associated type rather than ! for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Nov 30, 2019
1 parent ade4b1e commit 39869d7
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 90 deletions.
8 changes: 7 additions & 1 deletion src/alloc/abort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ use core::ptr::NonNull;
#[derive(Copy, Clone, Debug, Default)]
pub struct AbortAlloc<Alloc>(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<A: BuildAllocRef> BuildAllocRef for AbortAlloc<A> {
type Ref = AbortAlloc<A::Ref>;

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

impl<A: AllocRef> AllocRef for AbortAlloc<A> {
type Error = !;
type Error = Panic;

fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
self.0
Expand Down
2 changes: 1 addition & 1 deletion src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod abort;
mod layout;

pub use self::{
abort::AbortAlloc,
abort::{AbortAlloc, Panic},
layout::{LayoutErr, NonZeroLayout},
};
pub use core::alloc::GlobalAlloc;
Expand Down
36 changes: 18 additions & 18 deletions src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<T, A: AllocRef> Box<T, A> {
#[inline(always)]
pub fn new_in(x: T, a: A) -> Self
where
A: AllocRef<Error = !>,
A: AllocRef<Error = Panic>,
{
let Ok(b) = Self::try_new_in(x, a);
b
Expand Down Expand Up @@ -245,7 +245,7 @@ impl<T, A: AllocRef> Box<T, A> {
#[inline(always)]
pub fn new_uninit_in(a: A) -> Box<mem::MaybeUninit<T>, A>
where
A: AllocRef<Error = !>,
A: AllocRef<Error = Panic>,
{
let Ok(b) = Self::try_new_uninit_in(a);
b
Expand Down Expand Up @@ -286,7 +286,7 @@ impl<T, A: AllocRef> Box<T, A> {
#[inline(always)]
pub fn pin_in(x: T, a: A) -> Pin<Self>
where
A: AllocRef<Error = !>,
A: AllocRef<Error = Panic>,
{
let Ok(b) = Self::try_pin_in(x, a);
b
Expand Down Expand Up @@ -331,7 +331,7 @@ impl<T> Box<[T]> {
}

#[allow(clippy::use_self)]
impl<T, A: AllocRef<Error = !>> Box<[T], A> {
impl<T, A: AllocRef<Error = Panic>> Box<[T], A> {
/// Construct a new boxed slice with uninitialized contents with the spoecified allocator.
///
/// # Example
Expand Down Expand Up @@ -775,7 +775,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: DeallocRef> Drop for Box<T, A> {
impl<T, A> Default for Box<T, A>
where
T: Default,
A: Default + AllocRef<Error = !>,
A: Default + AllocRef<Error = Panic>,
{
#[must_use]
fn default() -> Self {
Expand All @@ -784,9 +784,9 @@ where
}

#[allow(clippy::use_self)]
impl<T, A: AllocRef<Error = !>> Default for Box<[T], A>
impl<T, A: AllocRef<Error = Panic>> Default for Box<[T], A>
where
A: Default + AllocRef<Error = !>,
A: Default + AllocRef<Error = Panic>,
{
#[must_use]
fn default() -> Self {
Expand All @@ -801,19 +801,19 @@ unsafe fn from_boxed_utf8_unchecked<A: DeallocRef>(v: Box<[u8], A>) -> Box<str,
}

#[allow(clippy::use_self)]
impl<A: AllocRef<Error = !>> Default for Box<str, A>
impl<A: AllocRef<Error = Panic>> Default for Box<str, A>
where
A: Default + AllocRef<Error = !>,
A: Default + AllocRef<Error = Panic>,
{
#[must_use]
fn default() -> Self {
unsafe { from_boxed_utf8_unchecked(Box::default()) }
}
}

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

fn clone_in(&self, a: B) -> Self::Cloned
where
B: AllocRef<Error = !>,
B: AllocRef<Error = Panic>,
{
Box::new_in(self.as_ref().clone(), a)
}
Expand Down Expand Up @@ -978,9 +978,9 @@ impl<T: ?Sized + Hasher, A: DeallocRef> Hasher for Box<T, A> {
}
}

impl<T, A: AllocRef<Error = !>> From<T> for Box<T, A>
impl<T, A: AllocRef<Error = Panic>> From<T> for Box<T, A>
where
A: Default + AllocRef<Error = !>,
A: Default + AllocRef<Error = Panic>,
{
/// Converts a generic type `T` into a `Box<T>`
///
Expand Down Expand Up @@ -1014,7 +1014,7 @@ impl<T: ?Sized, A: DeallocRef> From<Box<T, A>> for Pin<Box<T, A>> {
#[allow(clippy::use_self)]
impl<T: Copy, A> From<&[T]> for Box<[T], A>
where
A: Default + AllocRef<Error = !>,
A: Default + AllocRef<Error = Panic>,
{
/// Converts a `&[T]` into a `Box<[T], B>`
///
Expand Down Expand Up @@ -1043,7 +1043,7 @@ where
#[allow(clippy::use_self)]
impl<A> From<&str> for Box<str, A>
where
A: Default + AllocRef<Error = !>,
A: Default + AllocRef<Error = Panic>,
{
/// Converts a `&str` into a `Box<str>`
///
Expand Down Expand Up @@ -1306,7 +1306,7 @@ impl_dispatch_from_dyn!(AbortAlloc<std::alloc::System>);
#[allow(clippy::items_after_statements)]
impl<T: Clone, A: Clone> Clone for Box<[T], A>
where
A: AllocRef<Error = !>,
A: AllocRef<Error = Panic>,
A::BuildAlloc: Clone,
{
fn clone(&self) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions src/clone.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::alloc::AllocRef;
use crate::alloc::{AllocRef, Panic};

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

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

fn try_clone_in(&self, a: A) -> Result<Self::Cloned, A::Error>;
}
6 changes: 3 additions & 3 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -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<A> {
Expand Down Expand Up @@ -27,7 +27,7 @@ pub trait TryExtend<A> {
pub trait FromIteratorIn<T, A: AllocRef> {
fn from_iter_in<I: IntoIterator<Item = T>>(iter: I, allocator: A) -> Self
where
A: AllocRef<Error = !>;
A: AllocRef<Error = Panic>;

fn try_from_iter_in<I: IntoIterator<Item = T>>(
iter: I,
Expand All @@ -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<T: FromIteratorIn<Self::Item, A>, A: AllocRef>(self, allocator: A) -> T
where
A: AllocRef<Error = !>,
A: AllocRef<Error = Panic>,
{
FromIteratorIn::from_iter_in(self, allocator)
}
Expand Down
13 changes: 7 additions & 6 deletions src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
Global,
NonZeroLayout,
ReallocRef,
Panic,
},
boxed::Box,
collections::CollectionAllocErr,
Expand Down Expand Up @@ -162,7 +163,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
/// * 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<Error = !>,
A: AllocRef<Error = Panic>,
{
match Self::try_with_capacity_in(capacity, a) {
Ok(vec) => vec,
Expand Down Expand Up @@ -198,7 +199,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
/// * 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<Error = !>,
A: AllocRef<Error = Panic>,
{
match Self::try_with_capacity_zeroed_in(capacity, a) {
Ok(vec) => vec,
Expand Down Expand Up @@ -419,7 +420,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
/// ```
pub fn double(&mut self)
where
A: ReallocRef<Error = !>,
A: ReallocRef<Error = Panic>,
{
match self.try_double() {
Ok(_) => (),
Expand Down Expand Up @@ -590,7 +591,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
/// ```
pub fn reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize)
where
A: ReallocRef<Error = !>,
A: ReallocRef<Error = Panic>,
{
match self.try_reserve(used_capacity, needed_extra_capacity) {
Ok(vec) => vec,
Expand Down Expand Up @@ -634,7 +635,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
/// * 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<Error = !>,
A: ReallocRef<Error = Panic>,
{
match self.try_reserve_exact(used_capacity, needed_extra_capacity) {
Ok(_) => (),
Expand Down Expand Up @@ -736,7 +737,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
/// Panics if the given amount is *larger* than the current capacity.
pub fn shrink_to_fit(&mut self, amount: usize)
where
A: ReallocRef<Error = !>,
A: ReallocRef<Error = Panic>,
{
match self.try_shrink_to_fit(amount) {
Ok(_) => (),
Expand Down
Loading

0 comments on commit 39869d7

Please sign in to comment.