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

Commit ade4b1e

Browse files
committed
Merge branch 'infallibility-2' into infallibility
2 parents cd6d5bc + df1c75a commit ade4b1e

File tree

7 files changed

+97
-63
lines changed

7 files changed

+97
-63
lines changed

src/alloc/abort.rs

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,46 @@
1-
//! An allocator adapter that blows up by calling `handle_alloc_error` on all errors.
2-
//!
3-
//! On one hand, concrete allocator implementations should always be written
4-
//! without panicking on user error and OOM to give users maximum
5-
//! flexibility. On the other hand, code that depends on allocation succeeding
6-
//! should depend on `Alloc<Err=!>` to avoid repetitively handling errors from
7-
//! which it cannot recover.
8-
//!
9-
//! This adapter bridges the gap, effectively allowing `Alloc<Err=!>` to be
10-
//! implemented by any allocator.
1+
#![allow(clippy::use_self)]
112

12-
use core::{ptr::NonNull, usize};
3+
use crate::alloc::{
4+
handle_alloc_error,
5+
AllocRef,
6+
BuildAllocRef,
7+
DeallocRef,
8+
NonZeroLayout,
9+
NonZeroUsize,
10+
ReallocRef,
11+
};
12+
use core::ptr::NonNull;
1313

14-
use crate::alloc::*;
15-
16-
/// An allocator adapter that blows up by calling `handle_alloc_error` on all errors.
14+
/// An allocator adapter that blows up by calling `handle_alloc_error` on OOM.
15+
///
16+
/// On one hand, concrete allocator implementations should always be written
17+
/// without panicking on user error and OOM to give users maximum
18+
/// flexibility. On the other hand, code that depends on allocation succeeding
19+
/// should depend on `Alloc<Err=!>` to avoid repetitively handling errors from
20+
/// which it cannot recover.
1721
///
18-
/// See the [module-level documentation](../../std/abort_adapter/index.html) for more.
22+
/// This adapter bridges the gap, effectively allowing `Alloc<Err=!>` to be
23+
/// implemented by any allocator.
1924
#[derive(Copy, Clone, Debug, Default)]
2025
pub struct AbortAlloc<Alloc>(pub Alloc);
2126

22-
impl<B: BuildAllocRef> BuildAllocRef for AbortAlloc<B> {
23-
type Ref = AbortAlloc<B::Ref>;
27+
impl<A: BuildAllocRef> BuildAllocRef for AbortAlloc<A> {
28+
type Ref = AbortAlloc<A::Ref>;
2429

2530
unsafe fn build_alloc_ref(
2631
&mut self,
2732
ptr: NonNull<u8>,
2833
layout: Option<NonZeroLayout>,
2934
) -> Self::Ref {
30-
AbortAlloc(self.0.build_alloc_ref(ptr, layout))
35+
Self(self.0.build_alloc_ref(ptr, layout))
3136
}
3237
}
3338

3439
impl<A: DeallocRef> DeallocRef for AbortAlloc<A> {
3540
type BuildAlloc = AbortAlloc<A::BuildAlloc>;
3641

3742
fn get_build_alloc(&mut self) -> Self::BuildAlloc {
38-
AbortAlloc(self.0.get_build_alloc())
43+
Self(self.0.get_build_alloc())
3944
}
4045

4146
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: NonZeroLayout) {
@@ -49,13 +54,13 @@ impl<A: AllocRef> AllocRef for AbortAlloc<A> {
4954
fn alloc(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
5055
self.0
5156
.alloc(layout)
52-
.or_else(|_| handle_alloc_error(layout.into()))
57+
.map_err(|_| handle_alloc_error(layout.into()))
5358
}
5459

5560
fn alloc_zeroed(&mut self, layout: NonZeroLayout) -> Result<NonNull<u8>, Self::Error> {
5661
self.0
5762
.alloc_zeroed(layout)
58-
.or_else(|_| handle_alloc_error(layout.into()))
63+
.map_err(|_| handle_alloc_error(layout.into()))
5964
}
6065

6166
fn usable_size(&self, layout: NonZeroLayout) -> (usize, usize) {
@@ -90,6 +95,6 @@ impl<A: ReallocRef> ReallocRef for AbortAlloc<A> {
9095
) -> Result<NonNull<u8>, Self::Error> {
9196
self.0
9297
.realloc(ptr, old_layout, new_layout)
93-
.or_else(|_| handle_alloc_error(new_layout.into()))
98+
.map_err(|_| handle_alloc_error(new_layout.into()))
9499
}
95100
}

src/alloc/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
mod abort;
12
mod layout;
23

3-
pub use self::layout::{LayoutErr, NonZeroLayout};
4+
pub use self::{
5+
abort::AbortAlloc,
6+
layout::{LayoutErr, NonZeroLayout},
7+
};
48
pub use core::alloc::GlobalAlloc;
59
use core::{
610
cmp,
@@ -13,9 +17,6 @@ pub use liballoc::alloc::{handle_alloc_error, Layout};
1317
#[cfg(feature = "std")]
1418
use std::alloc::System;
1519

16-
mod abort;
17-
pub use self::abort::AbortAlloc;
18-
1920
/// Allocate memory with the global allocator.
2021
///
2122
/// This function forwards calls to the [`GlobalAlloc::alloc`] method

src/boxed.rs

+23-11
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::{AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout, AbortAlloc},
82+
alloc::{AbortAlloc, AllocRef, BuildAllocRef, DeallocRef, Global, NonZeroLayout},
8383
clone::CloneIn,
8484
collections::CollectionAllocErr,
8585
raw_vec::RawVec,
@@ -176,10 +176,13 @@ impl<T, A: AllocRef> Box<T, A> {
176176
/// # Example
177177
///
178178
/// ```
179-
/// use alloc_wg::{alloc::Global, boxed::Box};
179+
/// use alloc_wg::{
180+
/// alloc::{AbortAlloc, Global},
181+
/// boxed::Box,
182+
/// };
180183
///
181184
/// # #[allow(unused_variables)]
182-
/// let five = Box::new_in(5, Global);
185+
/// let five = Box::new_in(5, AbortAlloc(Global));
183186
/// ```
184187
#[allow(clippy::inline_always)]
185188
#[inline(always)]
@@ -222,9 +225,12 @@ impl<T, A: AllocRef> Box<T, A> {
222225
/// # Example
223226
///
224227
/// ```
225-
/// use alloc_wg::{alloc::Global, boxed::Box};
228+
/// use alloc_wg::{
229+
/// alloc::{AbortAlloc, Global},
230+
/// boxed::Box,
231+
/// };
226232
///
227-
/// let mut five = Box::<u32, _>::new_uninit_in(Global);
233+
/// let mut five = Box::<u32, _>::new_uninit_in(AbortAlloc(Global));
228234
///
229235
/// let five = unsafe {
230236
/// // Deferred initialization:
@@ -331,9 +337,12 @@ impl<T, A: AllocRef<Error = !>> Box<[T], A> {
331337
/// # Example
332338
///
333339
/// ```
334-
/// use alloc_wg::{alloc::Global, boxed::Box};
340+
/// use alloc_wg::{
341+
/// alloc::{AbortAlloc, Global},
342+
/// boxed::Box,
343+
/// };
335344
///
336-
/// let mut values = Box::<[u32], _>::new_uninit_slice_in(3, Global);
345+
/// let mut values = Box::<[u32], AbortAlloc<Global>>::new_uninit_slice_in(3, AbortAlloc(Global));
337346
///
338347
/// let values = unsafe {
339348
/// // Deferred initialization:
@@ -777,7 +786,7 @@ where
777786
#[allow(clippy::use_self)]
778787
impl<T, A: AllocRef<Error = !>> Default for Box<[T], A>
779788
where
780-
A: Default + AllocRef,
789+
A: Default + AllocRef<Error = !>,
781790
{
782791
#[must_use]
783792
fn default() -> Self {
@@ -794,7 +803,7 @@ unsafe fn from_boxed_utf8_unchecked<A: DeallocRef>(v: Box<[u8], A>) -> Box<str,
794803
#[allow(clippy::use_self)]
795804
impl<A: AllocRef<Error = !>> Default for Box<str, A>
796805
where
797-
A: Default + AllocRef,
806+
A: Default + AllocRef<Error = !>,
798807
{
799808
#[must_use]
800809
fn default() -> Self {
@@ -804,7 +813,7 @@ where
804813

805814
impl<T: Clone, A: AllocRef<Error = !>> Clone for Box<T, A>
806815
where
807-
A: AllocRef,
816+
A: AllocRef<Error = !>,
808817
A::BuildAlloc: Clone,
809818
{
810819
/// Returns a new box with a `clone()` of this box's contents.
@@ -971,7 +980,7 @@ impl<T: ?Sized + Hasher, A: DeallocRef> Hasher for Box<T, A> {
971980

972981
impl<T, A: AllocRef<Error = !>> From<T> for Box<T, A>
973982
where
974-
A: Default + AllocRef,
983+
A: Default + AllocRef<Error = !>,
975984
{
976985
/// Converts a generic type `T` into a `Box<T>`
977986
///
@@ -1288,8 +1297,11 @@ macro_rules! impl_dispatch_from_dyn {
12881297
}
12891298

12901299
impl_dispatch_from_dyn!(Global);
1300+
impl_dispatch_from_dyn!(AbortAlloc<Global>);
12911301
#[cfg(feature = "std")]
12921302
impl_dispatch_from_dyn!(std::alloc::System);
1303+
#[cfg(feature = "std")]
1304+
impl_dispatch_from_dyn!(AbortAlloc<std::alloc::System>);
12931305

12941306
#[allow(clippy::items_after_statements)]
12951307
impl<T: Clone, A: Clone> Clone for Box<[T], A>

src/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub trait TryExtend<A> {
1919
/// message.try_extend(['d', 'e', 'f'].iter())?;
2020
///
2121
/// assert_eq!(vec!['a', 'b', 'c', 'd', 'e', 'f'], message);
22-
/// # Ok::<(), alloc_wg::collections::CollectionAllocErr<alloc_wg::alloc::Global>>(())
22+
/// # Ok::<(), alloc_wg::collections::CollectionAllocErr<alloc_wg::alloc::AbortAlloc<alloc_wg::alloc::Global>>>(())
2323
/// ```
2424
fn try_extend<T: IntoIterator<Item = A>>(&mut self, iter: T) -> Result<(), Self::Err>;
2525
}

src/raw_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::{
22
alloc::{
3+
AbortAlloc,
34
AllocRef,
45
BuildAllocRef,
56
CapacityOverflow,
67
DeallocRef,
78
Global,
89
NonZeroLayout,
9-
AbortAlloc,
1010
ReallocRef,
1111
},
1212
boxed::Box,
@@ -634,7 +634,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
634634
/// * on 32-bit platforms if the requested capacity exceeds `isize::MAX` bytes.
635635
pub fn reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize)
636636
where
637-
A: ReallocRef,
637+
A: ReallocRef<Error = !>,
638638
{
639639
match self.try_reserve_exact(used_capacity, needed_extra_capacity) {
640640
Ok(_) => (),
@@ -736,7 +736,7 @@ impl<T, A: DeallocRef> RawVec<T, A> {
736736
/// Panics if the given amount is *larger* than the current capacity.
737737
pub fn shrink_to_fit(&mut self, amount: usize)
738738
where
739-
A: ReallocRef,
739+
A: ReallocRef<Error = !>,
740740
{
741741
match self.try_shrink_to_fit(amount) {
742742
Ok(_) => (),

src/string.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
//! ```
5353
5454
use crate::{
55-
alloc::{AllocRef, DeallocRef, Global, AbortAlloc, ReallocRef},
55+
alloc::{AbortAlloc, AllocRef, DeallocRef, Global, ReallocRef},
5656
boxed::Box,
5757
collections::CollectionAllocErr,
5858
iter::TryExtend,
@@ -1069,7 +1069,7 @@ impl<A: DeallocRef> String<A> {
10691069
#[inline]
10701070
pub fn reserve_exact(&mut self, additional: usize)
10711071
where
1072-
A: ReallocRef,
1072+
A: ReallocRef<Error = !>,
10731073
{
10741074
self.vec.reserve_exact(additional)
10751075
}
@@ -1088,9 +1088,13 @@ impl<A: DeallocRef> String<A> {
10881088
/// # Examples
10891089
///
10901090
/// ```
1091-
/// use alloc_wg::{alloc::Global, collections::CollectionAllocErr, string::String};
1091+
/// use alloc_wg::{
1092+
/// alloc::{AbortAlloc, Global},
1093+
/// collections::CollectionAllocErr,
1094+
/// string::String,
1095+
/// };
10921096
///
1093-
/// fn process_data(data: &str) -> Result<String, CollectionAllocErr<Global>> {
1097+
/// fn process_data(data: &str) -> Result<String, CollectionAllocErr<AbortAlloc<Global>>> {
10941098
/// let mut output = String::new();
10951099
///
10961100
/// // Pre-reserve the memory, exiting if we can't
@@ -1127,9 +1131,13 @@ impl<A: DeallocRef> String<A> {
11271131
/// # Examples
11281132
///
11291133
/// ```
1130-
/// use alloc_wg::{alloc::Global, collections::CollectionAllocErr, string::String};
1134+
/// use alloc_wg::{
1135+
/// alloc::{AbortAlloc, Global},
1136+
/// collections::CollectionAllocErr,
1137+
/// string::String,
1138+
/// };
11311139
///
1132-
/// fn process_data(data: &str) -> Result<String, CollectionAllocErr<Global>> {
1140+
/// fn process_data(data: &str) -> Result<String, CollectionAllocErr<AbortAlloc<Global>>> {
11331141
/// let mut output = String::new();
11341142
///
11351143
/// // Pre-reserve the memory, exiting if we can't
@@ -1172,7 +1180,7 @@ impl<A: DeallocRef> String<A> {
11721180
#[inline]
11731181
pub fn shrink_to_fit(&mut self)
11741182
where
1175-
A: ReallocRef,
1183+
A: ReallocRef<Error = !>,
11761184
{
11771185
self.vec.shrink_to_fit()
11781186
}
@@ -1216,7 +1224,7 @@ impl<A: DeallocRef> String<A> {
12161224
#[inline]
12171225
pub fn shrink_to(&mut self, min_capacity: usize)
12181226
where
1219-
A: ReallocRef,
1227+
A: ReallocRef<Error = !>,
12201228
{
12211229
self.vec.shrink_to(min_capacity)
12221230
}
@@ -1598,7 +1606,7 @@ impl<A: DeallocRef> String<A> {
15981606
#[inline]
15991607
pub fn try_insert_str(&mut self, idx: usize, string: &str) -> Result<(), CollectionAllocErr<A>>
16001608
where
1601-
A: ReallocRef,
1609+
A: ReallocRef<Error = !>,
16021610
{
16031611
assert!(self.is_char_boundary(idx));
16041612

0 commit comments

Comments
 (0)