Skip to content

Commit 8528f66

Browse files
committed
parameterize Allocator::Result on error type
1 parent 16af5c8 commit 8528f66

File tree

6 files changed

+97
-50
lines changed

6 files changed

+97
-50
lines changed

library/alloc/src/boxed.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ use core::task::{Context, Poll};
169169
use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
170170
#[cfg(not(no_global_oom_handling))]
171171
use crate::borrow::Cow;
172+
use crate::collections::TryReserveError;
172173
use crate::falloc::{AllocError, Allocator, Global, Layout};
173174
use crate::raw_vec::RawVec;
174175
#[cfg(not(no_global_oom_handling))]
@@ -737,7 +738,10 @@ impl<T, A: Allocator> Box<[T], A> {
737738
#[unstable(feature = "allocator_api", issue = "32838")]
738739
// #[unstable(feature = "new_uninit", issue = "63291")]
739740
#[must_use]
740-
pub fn new_uninit_slice_in(len: usize, alloc: A) -> A::Result<Box<[mem::MaybeUninit<T>], A>> {
741+
pub fn new_uninit_slice_in(
742+
len: usize,
743+
alloc: A,
744+
) -> A::Result<Box<[mem::MaybeUninit<T>], A>, TryReserveError> {
741745
unsafe { A::map_result(RawVec::with_capacity_in(len, alloc).map(|r| r.into_box(len))) }
742746
}
743747

@@ -764,7 +768,10 @@ impl<T, A: Allocator> Box<[T], A> {
764768
#[unstable(feature = "allocator_api", issue = "32838")]
765769
// #[unstable(feature = "new_uninit", issue = "63291")]
766770
#[must_use]
767-
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> A::Result<Box<[mem::MaybeUninit<T>], A>> {
771+
pub fn new_zeroed_slice_in(
772+
len: usize,
773+
alloc: A,
774+
) -> A::Result<Box<[mem::MaybeUninit<T>], A>, TryReserveError> {
768775
unsafe {
769776
A::map_result(RawVec::with_capacity_zeroed_in(len, alloc).map(|r| r.into_box(len)))
770777
}
@@ -2008,7 +2015,7 @@ impl<I> FromIterator<I> for Box<[I]> {
20082015
}
20092016

20102017
#[stable(feature = "box_slice_clone", since = "1.3.0")]
2011-
impl<T: Clone, A: Allocator<Result<Self> = Self> + Clone> Clone for Box<[T], A> {
2018+
impl<T: Clone, A: Allocator<Result<Self, TryReserveError> = Self> + Clone> Clone for Box<[T], A> {
20122019
fn clone(&self) -> Self {
20132020
let alloc = Box::allocator(self).clone();
20142021
self.to_vec_in(alloc).into_boxed_slice()

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub struct VecDeque<
108108
#[stable(feature = "rust1", since = "1.0.0")]
109109
impl<T: Clone, A> Clone for VecDeque<T, A>
110110
where
111-
A: Allocator<Result<Self> = Self> + Clone,
111+
A: Allocator<Result<Self, TryReserveError> = Self> + Clone,
112112
{
113113
fn clone(&self) -> Self {
114114
let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone());
@@ -594,7 +594,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
594594
/// let deque: VecDeque<u32> = VecDeque::with_capacity(10);
595595
/// ```
596596
#[unstable(feature = "allocator_api", issue = "32838")]
597-
pub fn with_capacity_in(capacity: usize, alloc: A) -> A::Result<VecDeque<T, A>> {
597+
pub fn with_capacity_in(
598+
capacity: usize,
599+
alloc: A,
600+
) -> A::Result<VecDeque<T, A>, TryReserveError> {
598601
A::map_result(Self::try_with_capacity_in(capacity, alloc))
599602
}
600603

@@ -758,7 +761,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
758761
///
759762
/// [`reserve`]: VecDeque::reserve
760763
#[stable(feature = "rust1", since = "1.0.0")]
761-
pub fn reserve_exact(&mut self, additional: usize) -> A::Result<()> {
764+
pub fn reserve_exact(&mut self, additional: usize) -> A::Result<(), TryReserveError> {
762765
A::map_result(self.try_reserve_exact(additional))
763766
}
764767

@@ -779,7 +782,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
779782
/// assert!(buf.capacity() >= 11);
780783
/// ```
781784
#[stable(feature = "rust1", since = "1.0.0")]
782-
pub fn reserve(&mut self, additional: usize) -> A::Result<()> {
785+
pub fn reserve(&mut self, additional: usize) -> A::Result<(), TryReserveError> {
783786
A::map_result(self.try_reserve(additional))
784787
}
785788

@@ -925,7 +928,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
925928
/// assert!(buf.capacity() >= 4);
926929
/// ```
927930
#[stable(feature = "shrink_to", since = "1.56.0")]
928-
pub fn shrink_to(&mut self, min_capacity: usize) -> A::Result<()> {
931+
pub fn shrink_to(&mut self, min_capacity: usize) -> A::Result<(), TryReserveError> {
929932
A::map_result((|| {
930933
// Substitute for try block
931934
let target_cap = min_capacity.max(self.len);
@@ -1622,7 +1625,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
16221625
/// assert_eq!(d.front(), Some(&2));
16231626
/// ```
16241627
#[stable(feature = "rust1", since = "1.0.0")]
1625-
pub fn push_front(&mut self, value: T) -> A::Result<()> {
1628+
pub fn push_front(&mut self, value: T) -> A::Result<(), TryReserveError> {
16261629
A::map_result((|| {
16271630
// Substitute for try block
16281631
if self.is_full() {
@@ -1653,7 +1656,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
16531656
/// assert_eq!(3, *buf.back().unwrap());
16541657
/// ```
16551658
#[stable(feature = "rust1", since = "1.0.0")]
1656-
pub fn push_back(&mut self, value: T) -> A::Result<()> {
1659+
pub fn push_back(&mut self, value: T) -> A::Result<(), TryReserveError> {
16571660
A::map_result((|| {
16581661
// Substsitute for try block
16591662
if self.is_full() {
@@ -1767,7 +1770,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
17671770
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']);
17681771
/// ```
17691772
#[stable(feature = "deque_extras_15", since = "1.5.0")]
1770-
pub fn insert(&mut self, index: usize, value: T) -> A::Result<()> {
1773+
pub fn insert(&mut self, index: usize, value: T) -> A::Result<(), TryReserveError> {
17711774
A::map_result((|| {
17721775
// Substitute for try block
17731776
assert!(index <= self.len(), "index out of bounds");
@@ -1874,7 +1877,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
18741877
#[inline]
18751878
#[must_use = "use `.truncate()` if you don't need the other half"]
18761879
#[stable(feature = "split_off", since = "1.4.0")]
1877-
pub fn split_off(&mut self, at: usize) -> A::Result<Self>
1880+
pub fn split_off(&mut self, at: usize) -> A::Result<Self, TryReserveError>
18781881
where
18791882
A: Clone,
18801883
{

library/alloc/src/falloc.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::error::Error;
2+
13
pub use crate::alloc::{AllocError, Global, GlobalAlloc, Layout, LayoutError};
24

35
/// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of
@@ -59,10 +61,14 @@ pub use crate::alloc::{AllocError, Global, GlobalAlloc, Layout, LayoutError};
5961
#[unstable(feature = "allocator_api", issue = "32838")]
6062
pub unsafe trait Allocator: crate::alloc::Allocator {
6163
#[must_use] // Doesn't actually work
62-
type Result<T>;
64+
type Result<T, E>
65+
where
66+
E: Error + IntoLayout;
6367

6468
#[must_use]
65-
fn map_result<T>(result: Result<T, TryReserveError>) -> Self::Result<T>;
69+
fn map_result<T, E>(result: Result<T, E>) -> Self::Result<T, E>
70+
where
71+
E: Error + IntoLayout;
6672
}
6773

6874
#[cfg(not(no_global_oom_handling))]
@@ -78,18 +84,34 @@ pub(crate) fn capacity_overflow() -> ! {
7884
panic!("capacity overflow");
7985
}
8086

87+
#[unstable(feature = "allocator_api", issue = "32838")]
88+
pub trait IntoLayout {
89+
#[cfg(not(no_global_oom_handling))]
90+
fn into_layout(self) -> Layout;
91+
}
92+
93+
#[unstable(feature = "allocator_api", issue = "32838")]
94+
impl IntoLayout for TryReserveError {
95+
#[cfg(not(no_global_oom_handling))]
96+
fn into_layout(self) -> Layout {
97+
match self.kind() {
98+
TryReserveErrorKind::CapacityOverflow => capacity_overflow(),
99+
TryReserveErrorKind::AllocError { layout, .. } => layout,
100+
}
101+
}
102+
}
103+
81104
#[unstable(feature = "allocator_api", issue = "32838")]
82105
#[cfg(not(no_global_oom_handling))]
83106
unsafe impl<X: crate::alloc::Allocator> Allocator for X {
84-
type Result<T> = T;
107+
type Result<T, E> = T
108+
where
109+
E: Error + IntoLayout;
85110

86-
fn map_result<T>(result: Result<T, TryReserveError>) -> Self::Result<T> {
87-
match result {
88-
Err(error) => match error.kind() {
89-
TryReserveErrorKind::CapacityOverflow => capacity_overflow(),
90-
TryReserveErrorKind::AllocError { layout, .. } => handle_alloc_error(layout),
91-
},
92-
Ok(x) => x,
93-
}
111+
fn map_result<T, E>(result: Result<T, E>) -> Self::Result<T, E>
112+
where
113+
E: Error + IntoLayout,
114+
{
115+
result.unwrap_or_else(|error| handle_alloc_error(error.into_layout()))
94116
}
95117
}

library/alloc/src/raw_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
77
use core::ptr::{self, NonNull, Unique};
88
use core::slice;
99

10+
use crate::alloc::{Allocator, Global, Layout};
1011
use crate::boxed::Box;
1112
use crate::collections::TryReserveError;
1213
use crate::collections::TryReserveErrorKind::*;
13-
use crate::falloc::{Allocator, Global, Layout};
1414

1515
#[cfg(test)]
1616
mod tests;

library/alloc/src/vec/mod.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<T, A: Allocator> Vec<T, A> {
673673
/// ```
674674
#[inline]
675675
#[unstable(feature = "allocator_api", issue = "32838")]
676-
pub fn with_capacity_in(capacity: usize, alloc: A) -> A::Result<Self> {
676+
pub fn with_capacity_in(capacity: usize, alloc: A) -> A::Result<Self, TryReserveError> {
677677
A::map_result(Self::try_with_capacity_in(capacity, alloc))
678678
}
679679

@@ -908,7 +908,7 @@ impl<T, A: Allocator> Vec<T, A> {
908908
/// assert!(vec.capacity() >= 11);
909909
/// ```
910910
#[stable(feature = "rust1", since = "1.0.0")]
911-
pub fn reserve(&mut self, additional: usize) -> A::Result<()> {
911+
pub fn reserve(&mut self, additional: usize) -> A::Result<(), TryReserveError> {
912912
A::map_result(self.try_reserve(additional))
913913
}
914914

@@ -937,7 +937,7 @@ impl<T, A: Allocator> Vec<T, A> {
937937
/// assert!(vec.capacity() >= 11);
938938
/// ```
939939
#[stable(feature = "rust1", since = "1.0.0")]
940-
pub fn reserve_exact(&mut self, additional: usize) -> A::Result<()> {
940+
pub fn reserve_exact(&mut self, additional: usize) -> A::Result<(), TryReserveError> {
941941
A::map_result(self.try_reserve_exact(additional))
942942
}
943943

@@ -1047,7 +1047,7 @@ impl<T, A: Allocator> Vec<T, A> {
10471047
/// assert!(vec.capacity() >= 3);
10481048
/// ```
10491049
#[stable(feature = "rust1", since = "1.0.0")]
1050-
pub fn shrink_to_fit(&mut self) -> A::Result<()> {
1050+
pub fn shrink_to_fit(&mut self) -> A::Result<(), TryReserveError> {
10511051
A::map_result(self.try_shrink_to_fit())
10521052
}
10531053

@@ -1070,7 +1070,7 @@ impl<T, A: Allocator> Vec<T, A> {
10701070
/// assert!(vec.capacity() >= 3);
10711071
/// ```
10721072
#[stable(feature = "shrink_to", since = "1.56.0")]
1073-
pub fn shrink_to(&mut self, min_capacity: usize) -> A::Result<()> {
1073+
pub fn shrink_to(&mut self, min_capacity: usize) -> A::Result<(), TryReserveError> {
10741074
A::map_result(if self.capacity() > min_capacity {
10751075
self.buf.shrink_to(cmp::max(self.len, min_capacity))
10761076
} else {
@@ -1104,7 +1104,7 @@ impl<T, A: Allocator> Vec<T, A> {
11041104
/// assert_eq!(slice.into_vec().capacity(), 3);
11051105
/// ```
11061106
#[stable(feature = "rust1", since = "1.0.0")]
1107-
pub fn into_boxed_slice(mut self) -> A::Result<Box<[T], A>> {
1107+
pub fn into_boxed_slice(mut self) -> A::Result<Box<[T], A>, TryReserveError> {
11081108
A::map_result((|| {
11091109
// Substitute for try block
11101110
self.try_shrink_to_fit()?;
@@ -1442,7 +1442,7 @@ impl<T, A: Allocator> Vec<T, A> {
14421442
/// assert_eq!(vec, [1, 4, 2, 3, 5]);
14431443
/// ```
14441444
#[stable(feature = "rust1", since = "1.0.0")]
1445-
pub fn insert(&mut self, index: usize, element: T) -> A::Result<()> {
1445+
pub fn insert(&mut self, index: usize, element: T) -> A::Result<(), TryReserveError> {
14461446
A::map_result((|| {
14471447
// Substitute for try block
14481448
#[cold]
@@ -1835,7 +1835,7 @@ impl<T, A: Allocator> Vec<T, A> {
18351835
/// ```
18361836
#[inline]
18371837
#[stable(feature = "rust1", since = "1.0.0")]
1838-
pub fn push(&mut self, value: T) -> A::Result<()> {
1838+
pub fn push(&mut self, value: T) -> A::Result<(), TryReserveError> {
18391839
A::map_result((|| {
18401840
// Substitute for try block
18411841
// This will panic or abort if we would allocate > isize::MAX bytes
@@ -1943,7 +1943,7 @@ impl<T, A: Allocator> Vec<T, A> {
19431943
/// ```
19441944
#[inline]
19451945
#[stable(feature = "append", since = "1.4.0")]
1946-
pub fn append(&mut self, other: &mut Self) -> A::Result<()> {
1946+
pub fn append(&mut self, other: &mut Self) -> A::Result<(), TryReserveError> {
19471947
A::map_result((|| {
19481948
// Substitute for try block
19491949
unsafe {
@@ -2111,7 +2111,7 @@ impl<T, A: Allocator> Vec<T, A> {
21112111
#[inline]
21122112
#[must_use = "use `.truncate()` if you don't need the other half"]
21132113
#[stable(feature = "split_off", since = "1.4.0")]
2114-
pub fn split_off(&mut self, at: usize) -> A::Result<Self>
2114+
pub fn split_off(&mut self, at: usize) -> A::Result<Self, TryReserveError>
21152115
where
21162116
A: Clone,
21172117
{
@@ -2176,7 +2176,7 @@ impl<T, A: Allocator> Vec<T, A> {
21762176
/// assert_eq!(vec, [2, 4, 8, 16]);
21772177
/// ```
21782178
#[stable(feature = "vec_resize_with", since = "1.33.0")]
2179-
pub fn resize_with<F>(&mut self, new_len: usize, f: F) -> A::Result<()>
2179+
pub fn resize_with<F>(&mut self, new_len: usize, f: F) -> A::Result<(), TryReserveError>
21802180
where
21812181
F: FnMut() -> T,
21822182
{
@@ -2380,7 +2380,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
23802380
/// assert_eq!(vec, [1, 2]);
23812381
/// ```
23822382
#[stable(feature = "vec_resize", since = "1.5.0")]
2383-
pub fn resize(&mut self, new_len: usize, value: T) -> A::Result<()> {
2383+
pub fn resize(&mut self, new_len: usize, value: T) -> A::Result<(), TryReserveError> {
23842384
A::map_result((|| {
23852385
// Substitute for try block
23862386
let len = self.len();
@@ -2416,7 +2416,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
24162416
/// [`extend`]: Vec::extend
24172417
#[cfg(not(no_global_oom_handling))]
24182418
#[stable(feature = "vec_extend_from_slice", since = "1.6.0")]
2419-
pub fn extend_from_slice(&mut self, other: &[T]) -> A::Result<()> {
2419+
pub fn extend_from_slice(&mut self, other: &[T]) -> A::Result<(), TryReserveError> {
24202420
A::map_result(self.spec_extend(other.iter()))
24212421
}
24222422

@@ -2443,7 +2443,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
24432443
/// ```
24442444
#[cfg(not(no_global_oom_handling))]
24452445
#[stable(feature = "vec_extend_from_within", since = "1.53.0")]
2446-
pub fn extend_from_within<R>(&mut self, src: R) -> A::Result<()>
2446+
pub fn extend_from_within<R>(&mut self, src: R) -> A::Result<(), TryReserveError>
24472447
where
24482448
R: RangeBounds<usize>,
24492449
{
@@ -2570,13 +2570,20 @@ impl<T: PartialEq, A: Allocator> Vec<T, A> {
25702570

25712571
#[doc(hidden)]
25722572
#[stable(feature = "rust1", since = "1.0.0")]
2573-
pub fn from_elem<T: Clone>(elem: T, n: usize) -> <Global as Allocator>::Result<Vec<T>> {
2573+
pub fn from_elem<T: Clone>(
2574+
elem: T,
2575+
n: usize,
2576+
) -> <Global as Allocator>::Result<Vec<T>, TryReserveError> {
25742577
<Global as Allocator>::map_result(<T as SpecFromElem>::from_elem(elem, n, Global))
25752578
}
25762579

25772580
#[doc(hidden)]
25782581
#[unstable(feature = "allocator_api", issue = "32838")]
2579-
pub fn from_elem_in<T: Clone, A: Allocator>(elem: T, n: usize, alloc: A) -> A::Result<Vec<T, A>> {
2582+
pub fn from_elem_in<T: Clone, A: Allocator>(
2583+
elem: T,
2584+
n: usize,
2585+
alloc: A,
2586+
) -> A::Result<Vec<T, A>, TryReserveError> {
25802587
A::map_result(<T as SpecFromElem>::from_elem(elem, n, alloc))
25812588
}
25822589

@@ -2801,7 +2808,7 @@ impl<'a, T, A: Allocator> IntoIterator for &'a mut Vec<T, A> {
28012808
}
28022809

28032810
#[stable(feature = "rust1", since = "1.0.0")]
2804-
impl<T, A: Allocator<Result<()> = ()>> Extend<T> for Vec<T, A> {
2811+
impl<T, A: Allocator<Result<(), TryReserveError> = ()>> Extend<T> for Vec<T, A> {
28052812
#[inline]
28062813
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
28072814
A::map_result(<Self as SpecExtend<T, I::IntoIter>>::spec_extend(self, iter.into_iter()))
@@ -2928,7 +2935,7 @@ impl<T, A: Allocator> Vec<T, A> {
29282935
where
29292936
R: RangeBounds<usize>,
29302937
I: IntoIterator<Item = T>,
2931-
A: Allocator<Result<()> = ()>,
2938+
A: Allocator<Result<(), TryReserveError> = ()>,
29322939
{
29332940
Splice { drain: self.drain(range), replace_with: replace_with.into_iter() }
29342941
}
@@ -3000,7 +3007,9 @@ impl<T, A: Allocator> Vec<T, A> {
30003007
///
30013008
/// [`copy_from_slice`]: slice::copy_from_slice
30023009
#[stable(feature = "extend_ref", since = "1.2.0")]
3003-
impl<'a, T: Copy + 'a, A: Allocator<Result<()> = ()> + 'a> Extend<&'a T> for Vec<T, A> {
3010+
impl<'a, T: Copy + 'a, A: Allocator<Result<(), TryReserveError> = ()> + 'a> Extend<&'a T>
3011+
for Vec<T, A>
3012+
{
30043013
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
30053014
A::map_result(self.spec_extend(iter.into_iter()))
30063015
}
@@ -3203,7 +3212,7 @@ impl<T, A: Allocator> From<Box<[T], A>> for Vec<T, A> {
32033212
#[stable(feature = "box_from_vec", since = "1.20.0")]
32043213
impl<T, A> From<Vec<T, A>> for Box<[T], A>
32053214
where
3206-
A: Allocator<Result<Self> = Self>,
3215+
A: Allocator<Result<Self, TryReserveError> = Self>,
32073216
{
32083217
/// Convert a vector into a boxed slice.
32093218
///

0 commit comments

Comments
 (0)