Skip to content

Commit 68bd5f0

Browse files
committed
Add associated error type to allocators
We will use this for fallibility polymorphism
1 parent be8f389 commit 68bd5f0

File tree

4 files changed

+124
-89
lines changed

4 files changed

+124
-89
lines changed

src/liballoc/alloc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
120120
__rust_alloc_zeroed(layout.size(), layout.align())
121121
}
122122

123+
#[cfg(not(test))]
124+
#[unstable(feature = "allocator_api", issue = "32838")]
125+
impl AllocHelper for Global {
126+
type Err = AllocErr;
127+
}
128+
123129
#[cfg(not(test))]
124130
#[unstable(feature = "allocator_api", issue = "32838")]
125131
unsafe impl Alloc for Global {

src/liballoc/boxed.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut, Generator, GeneratorState};
6969
use core::ptr::{self, NonNull, Unique};
7070
use core::task::{Context, Poll};
7171

72-
use alloc::{Alloc, Global, Layout, handle_alloc_error};
72+
use alloc::{Alloc, AllocHelper, AllocErr, Global, Layout, handle_alloc_error};
7373
use raw_vec::RawVec;
7474
use str::from_boxed_utf8_unchecked;
7575

@@ -79,7 +79,7 @@ use str::from_boxed_utf8_unchecked;
7979
#[lang = "owned_box"]
8080
#[fundamental]
8181
#[stable(feature = "rust1", since = "1.0.0")]
82-
pub struct Box<T: ?Sized, A: Alloc = Global>(Unique<T>, A);
82+
pub struct Box<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr> = Global>(Unique<T>, A);
8383

8484
impl<T> Box<T> {
8585
/// Allocates memory on the heap and then places `x` into it.
@@ -98,7 +98,7 @@ impl<T> Box<T> {
9898
}
9999
}
100100

101-
impl<T, A: Alloc> Box<T, A> {
101+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> Box<T, A> {
102102
/// Allocates memory in the given allocator and then places `x` into it.
103103
///
104104
/// This doesn't actually allocate if `T` is zero-sized.
@@ -159,7 +159,7 @@ impl<T: ?Sized> Box<T> {
159159
}
160160
}
161161

162-
impl<T: ?Sized, A: Alloc> Box<T, A> {
162+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> Box<T, A> {
163163
/// Constructs a box from a raw pointer in the given allocator.
164164
///
165165
/// This is similar to the [`Box::from_raw`] function, but assumes
@@ -303,7 +303,7 @@ impl<T: ?Sized, A: Alloc> Box<T, A> {
303303
}
304304

305305
#[stable(feature = "rust1", since = "1.0.0")]
306-
unsafe impl<#[may_dangle] T: ?Sized, A: Alloc> Drop for Box<T, A> {
306+
unsafe impl<#[may_dangle] T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> Drop for Box<T, A> {
307307
fn drop(&mut self) {
308308
// FIXME: Do nothing, drop is currently performed by compiler.
309309
}
@@ -318,15 +318,15 @@ impl<T: Default> Default for Box<T> {
318318
}
319319

320320
#[unstable(feature = "allocator_api", issue = "32838")]
321-
impl<T: Default, A: Alloc + Default> Default for Box<T, A> {
321+
impl<T: Default, A: Alloc + AllocHelper<Err = AllocErr> + Default> Default for Box<T, A> {
322322
/// Creates a `Box<T, A>`, with the `Default` value for T.
323323
default fn default() -> Box<T, A> {
324324
Box::new_in(Default::default(), Default::default())
325325
}
326326
}
327327

328328
#[stable(feature = "rust1", since = "1.0.0")]
329-
impl<T, A: Alloc + Default> Default for Box<[T], A> {
329+
impl<T, A: Alloc + AllocHelper<Err = AllocErr> + Default> Default for Box<[T], A> {
330330
fn default() -> Box<[T], A> {
331331
Box::<[T; 0], A>::new_in([], A::default())
332332
}
@@ -374,7 +374,7 @@ impl<T: Clone> Clone for Box<T> {
374374
}
375375

376376
#[unstable(feature = "allocator_api", issue = "32838")]
377-
impl<T: Clone, A: Alloc + Clone> Clone for Box<T, A> {
377+
impl<T: Clone, A: Alloc + AllocHelper<Err = AllocErr> + Clone> Clone for Box<T, A> {
378378
#[inline]
379379
default fn clone(&self) -> Box<T, A> {
380380
Box::new_in((**self).clone(), self.1.clone())
@@ -399,7 +399,7 @@ impl Clone for Box<str> {
399399
}
400400

401401
#[stable(feature = "rust1", since = "1.0.0")]
402-
impl<T: ?Sized + PartialEq, A: Alloc> PartialEq for Box<T, A> {
402+
impl<T: ?Sized + PartialEq, A: Alloc + AllocHelper<Err = AllocErr>> PartialEq for Box<T, A> {
403403
#[inline]
404404
fn eq(&self, other: &Box<T, A>) -> bool {
405405
PartialEq::eq(&**self, &**other)
@@ -410,7 +410,7 @@ impl<T: ?Sized + PartialEq, A: Alloc> PartialEq for Box<T, A> {
410410
}
411411
}
412412
#[stable(feature = "rust1", since = "1.0.0")]
413-
impl<T: ?Sized + PartialOrd, A: Alloc> PartialOrd for Box<T, A> {
413+
impl<T: ?Sized + PartialOrd, A: Alloc + AllocHelper<Err = AllocErr>> PartialOrd for Box<T, A> {
414414
#[inline]
415415
fn partial_cmp(&self, other: &Box<T, A>) -> Option<Ordering> {
416416
PartialOrd::partial_cmp(&**self, &**other)
@@ -433,24 +433,24 @@ impl<T: ?Sized + PartialOrd, A: Alloc> PartialOrd for Box<T, A> {
433433
}
434434
}
435435
#[stable(feature = "rust1", since = "1.0.0")]
436-
impl<T: ?Sized + Ord, A: Alloc> Ord for Box<T, A> {
436+
impl<T: ?Sized + Ord, A: Alloc + AllocHelper<Err = AllocErr>> Ord for Box<T, A> {
437437
#[inline]
438438
fn cmp(&self, other: &Box<T, A>) -> Ordering {
439439
Ord::cmp(&**self, &**other)
440440
}
441441
}
442442
#[stable(feature = "rust1", since = "1.0.0")]
443-
impl<T: ?Sized + Eq, A: Alloc> Eq for Box<T, A> {}
443+
impl<T: ?Sized + Eq, A: Alloc + AllocHelper<Err = AllocErr>> Eq for Box<T, A> {}
444444

445445
#[stable(feature = "rust1", since = "1.0.0")]
446-
impl<T: ?Sized + Hash, A: Alloc> Hash for Box<T, A> {
446+
impl<T: ?Sized + Hash, A: Alloc + AllocHelper<Err = AllocErr>> Hash for Box<T, A> {
447447
fn hash<H: Hasher>(&self, state: &mut H) {
448448
(**self).hash(state);
449449
}
450450
}
451451

452452
#[stable(feature = "indirect_hasher_impl", since = "1.22.0")]
453-
impl<T: ?Sized + Hasher, A: Alloc> Hasher for Box<T, A> {
453+
impl<T: ?Sized + Hasher, A: Alloc + AllocHelper<Err = AllocErr>> Hasher for Box<T, A> {
454454
fn finish(&self) -> u64 {
455455
(**self).finish()
456456
}
@@ -503,14 +503,14 @@ impl<T> From<T> for Box<T> {
503503
}
504504

505505
#[unstable(feature = "allocator_api", issue = "32838")]
506-
impl<T, A: Alloc + Default> From<T> for Box<T, A> {
506+
impl<T, A: Alloc + AllocHelper<Err = AllocErr> + Default> From<T> for Box<T, A> {
507507
default fn from(t: T) -> Self {
508508
Box::new_in(t, A::default())
509509
}
510510
}
511511

512512
#[stable(feature = "box_from_slice", since = "1.17.0")]
513-
impl<'a, T: Copy, A: Alloc + Default> From<&'a [T]> for Box<[T], A> {
513+
impl<'a, T: Copy, A: Alloc + AllocHelper<Err = AllocErr> + Default> From<&'a [T]> for Box<[T], A> {
514514
fn from(slice: &'a [T]) -> Box<[T], A> {
515515
let a = A::default();
516516
let mut boxed = unsafe { RawVec::with_capacity_in(slice.len(), a).into_box() };
@@ -600,21 +600,21 @@ impl Box<dyn Any + Send> {
600600
}
601601

602602
#[stable(feature = "rust1", since = "1.0.0")]
603-
impl<T: fmt::Display + ?Sized, A: Alloc> fmt::Display for Box<T, A> {
603+
impl<T: fmt::Display + ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> fmt::Display for Box<T, A> {
604604
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
605605
fmt::Display::fmt(&**self, f)
606606
}
607607
}
608608

609609
#[stable(feature = "rust1", since = "1.0.0")]
610-
impl<T: fmt::Debug + ?Sized, A: Alloc> fmt::Debug for Box<T, A> {
610+
impl<T: fmt::Debug + ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> fmt::Debug for Box<T, A> {
611611
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
612612
fmt::Debug::fmt(&**self, f)
613613
}
614614
}
615615

616616
#[stable(feature = "rust1", since = "1.0.0")]
617-
impl<T: ?Sized, A: Alloc> fmt::Pointer for Box<T, A> {
617+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> fmt::Pointer for Box<T, A> {
618618
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
619619
// It's not possible to extract the inner Uniq directly from the Box,
620620
// instead we cast it to a *const which aliases the Unique
@@ -624,7 +624,7 @@ impl<T: ?Sized, A: Alloc> fmt::Pointer for Box<T, A> {
624624
}
625625

626626
#[stable(feature = "rust1", since = "1.0.0")]
627-
impl<T: ?Sized, A: Alloc> Deref for Box<T, A> {
627+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> Deref for Box<T, A> {
628628
type Target = T;
629629

630630
fn deref(&self) -> &T {
@@ -633,14 +633,14 @@ impl<T: ?Sized, A: Alloc> Deref for Box<T, A> {
633633
}
634634

635635
#[stable(feature = "rust1", since = "1.0.0")]
636-
impl<T: ?Sized, A: Alloc> DerefMut for Box<T, A> {
636+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> DerefMut for Box<T, A> {
637637
fn deref_mut(&mut self) -> &mut T {
638638
&mut **self
639639
}
640640
}
641641

642642
#[stable(feature = "rust1", since = "1.0.0")]
643-
impl<I: Iterator + ?Sized, A: Alloc> Iterator for Box<I, A> {
643+
impl<I: Iterator + ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> Iterator for Box<I, A> {
644644
type Item = I::Item;
645645
fn next(&mut self) -> Option<I::Item> {
646646
(**self).next()
@@ -653,13 +653,13 @@ impl<I: Iterator + ?Sized, A: Alloc> Iterator for Box<I, A> {
653653
}
654654
}
655655
#[stable(feature = "rust1", since = "1.0.0")]
656-
impl<I: DoubleEndedIterator + ?Sized, A: Alloc> DoubleEndedIterator for Box<I, A> {
656+
impl<I: DoubleEndedIterator + ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> DoubleEndedIterator for Box<I, A> {
657657
fn next_back(&mut self) -> Option<I::Item> {
658658
(**self).next_back()
659659
}
660660
}
661661
#[stable(feature = "rust1", since = "1.0.0")]
662-
impl<I: ExactSizeIterator + ?Sized, A: Alloc> ExactSizeIterator for Box<I, A> {
662+
impl<I: ExactSizeIterator + ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> ExactSizeIterator for Box<I, A> {
663663
fn len(&self) -> usize {
664664
(**self).len()
665665
}
@@ -669,7 +669,7 @@ impl<I: ExactSizeIterator + ?Sized, A: Alloc> ExactSizeIterator for Box<I, A> {
669669
}
670670

671671
#[stable(feature = "fused", since = "1.26.0")]
672-
impl<I: FusedIterator + ?Sized, A: Alloc> FusedIterator for Box<I, A> {}
672+
impl<I: FusedIterator + ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> FusedIterator for Box<I, A> {}
673673

674674

675675
/// `FnBox` is a version of the `FnOnce` intended for use with boxed
@@ -751,10 +751,10 @@ impl<'a, A, R> FnOnce<A> for Box<dyn FnBox<A, Output = R> + Send + 'a> {
751751
}
752752

753753
#[unstable(feature = "coerce_unsized", issue = "27732")]
754-
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Alloc> CoerceUnsized<Box<U, A>> for Box<T, A> {}
754+
impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> CoerceUnsized<Box<U, A>> for Box<T, A> {}
755755

756756
#[stable(feature = "box_slice_clone", since = "1.3.0")]
757-
impl<T: Clone, A: Alloc + Clone> Clone for Box<[T], A> {
757+
impl<T: Clone, A: Alloc + AllocHelper<Err = AllocErr> + Clone> Clone for Box<[T], A> {
758758
fn clone(&self) -> Self {
759759
let mut new = BoxBuilder {
760760
data: RawVec::with_capacity_in(self.len(), self.1.clone()),
@@ -775,20 +775,20 @@ impl<T: Clone, A: Alloc + Clone> Clone for Box<[T], A> {
775775
return unsafe { new.into_box() };
776776

777777
// Helper type for responding to panics correctly.
778-
struct BoxBuilder<T, A: Alloc> {
778+
struct BoxBuilder<T, A: Alloc + AllocHelper<Err = AllocErr> + AllocHelper<Err = AllocErr>> {
779779
data: RawVec<T, A>,
780780
len: usize,
781781
}
782782

783-
impl<T, A: Alloc> BoxBuilder<T, A> {
783+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> BoxBuilder<T, A> {
784784
unsafe fn into_box(self) -> Box<[T], A> {
785785
let raw = ptr::read(&self.data);
786786
mem::forget(self);
787787
raw.into_box()
788788
}
789789
}
790790

791-
impl<T, A: Alloc> Drop for BoxBuilder<T, A> {
791+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> Drop for BoxBuilder<T, A> {
792792
fn drop(&mut self) {
793793
let mut data = self.data.ptr();
794794
let max = unsafe { data.offset(self.len as isize) };
@@ -805,35 +805,35 @@ impl<T: Clone, A: Alloc + Clone> Clone for Box<[T], A> {
805805
}
806806

807807
#[stable(feature = "box_borrow", since = "1.1.0")]
808-
impl<T: ?Sized, A: Alloc> borrow::Borrow<T> for Box<T, A> {
808+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> borrow::Borrow<T> for Box<T, A> {
809809
fn borrow(&self) -> &T {
810810
&**self
811811
}
812812
}
813813

814814
#[stable(feature = "box_borrow", since = "1.1.0")]
815-
impl<T: ?Sized, A: Alloc> borrow::BorrowMut<T> for Box<T, A> {
815+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> borrow::BorrowMut<T> for Box<T, A> {
816816
fn borrow_mut(&mut self) -> &mut T {
817817
&mut **self
818818
}
819819
}
820820

821821
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
822-
impl<T: ?Sized, A: Alloc> AsRef<T> for Box<T, A> {
822+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> AsRef<T> for Box<T, A> {
823823
fn as_ref(&self) -> &T {
824824
&**self
825825
}
826826
}
827827

828828
#[stable(since = "1.5.0", feature = "smart_ptr_as_ref")]
829-
impl<T: ?Sized, A: Alloc> AsMut<T> for Box<T, A> {
829+
impl<T: ?Sized, A: Alloc + AllocHelper<Err = AllocErr>> AsMut<T> for Box<T, A> {
830830
fn as_mut(&mut self) -> &mut T {
831831
&mut **self
832832
}
833833
}
834834

835835
#[unstable(feature = "generator_trait", issue = "43122")]
836-
impl<T, A: Alloc> Generator for Box<T, A>
836+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> Generator for Box<T, A>
837837
where T: Generator + ?Sized
838838
{
839839
type Yield = T::Yield;

src/liballoc/raw_vec.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use core::ops::Drop;
1717
use core::ptr::{self, NonNull, Unique};
1818
use core::slice;
1919

20-
use alloc::{Alloc, Layout, Global, handle_alloc_error};
20+
use alloc::{Alloc, AllocHelper, AllocErr, Layout, Global, handle_alloc_error};
2121
use collections::CollectionAllocErr;
2222
use collections::CollectionAllocErr::*;
2323
use boxed::Box;
@@ -50,13 +50,13 @@ use boxed::Box;
5050
/// field. This allows zero-sized types to not be special-cased by consumers of
5151
/// this type.
5252
#[allow(missing_debug_implementations)]
53-
pub struct RawVec<T, A: Alloc = Global> {
53+
pub struct RawVec<T, A: Alloc + AllocHelper<Err = AllocErr> = Global> {
5454
ptr: Unique<T>,
5555
cap: usize,
5656
a: A,
5757
}
5858

59-
impl<T, A: Alloc> RawVec<T, A> {
59+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> RawVec<T, A> {
6060
/// Like `new` but parameterized over the choice of allocator for
6161
/// the returned RawVec.
6262
pub const fn new_in(a: A) -> Self {
@@ -157,7 +157,7 @@ impl<T> RawVec<T, Global> {
157157
}
158158
}
159159

160-
impl<T, A: Alloc> RawVec<T, A> {
160+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> RawVec<T, A> {
161161
/// Reconstitutes a RawVec from a pointer, capacity, and allocator.
162162
///
163163
/// # Undefined Behavior
@@ -200,7 +200,7 @@ impl<T> RawVec<T, Global> {
200200
}
201201
}
202202

203-
impl<T, A: Alloc> RawVec<T, A> {
203+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> RawVec<T, A> {
204204
/// Gets a raw pointer to the start of the allocation. Note that this is
205205
/// Unique::empty() if `cap = 0` or T is zero-sized. In the former case, you must
206206
/// be careful.
@@ -421,7 +421,7 @@ impl<T, A: Alloc> RawVec<T, A> {
421421
pub fn reserve_exact(&mut self, used_cap: usize, needed_extra_cap: usize) {
422422
match self.reserve_internal(used_cap, needed_extra_cap, Infallible, Exact) {
423423
Err(CapacityOverflow) => capacity_overflow(),
424-
Err(AllocErr) => unreachable!(),
424+
Err(CollectionAllocErr::AllocErr) => unreachable!(),
425425
Ok(()) => { /* yay */ }
426426
}
427427
}
@@ -501,7 +501,7 @@ impl<T, A: Alloc> RawVec<T, A> {
501501
pub fn reserve(&mut self, used_cap: usize, needed_extra_cap: usize) {
502502
match self.reserve_internal(used_cap, needed_extra_cap, Infallible, Amortized) {
503503
Err(CapacityOverflow) => capacity_overflow(),
504-
Err(AllocErr) => unreachable!(),
504+
Err(CollectionAllocErr::AllocErr) => unreachable!(),
505505
Ok(()) => { /* yay */ }
506506
}
507507
}
@@ -640,7 +640,7 @@ enum ReserveStrategy {
640640

641641
use self::ReserveStrategy::*;
642642

643-
impl<T, A: Alloc> RawVec<T, A> {
643+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> RawVec<T, A> {
644644
fn reserve_internal(
645645
&mut self,
646646
used_cap: usize,
@@ -693,7 +693,7 @@ impl<T, A: Alloc> RawVec<T, A> {
693693

694694
}
695695

696-
impl<T, A: Alloc> RawVec<T, A> {
696+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> RawVec<T, A> {
697697
/// Converts the entire buffer into `Box<[T], A>`.
698698
///
699699
/// While it is not *strictly* Undefined Behavior to call
@@ -712,7 +712,7 @@ impl<T, A: Alloc> RawVec<T, A> {
712712
}
713713
}
714714

715-
impl<T, A: Alloc> RawVec<T, A> {
715+
impl<T, A: Alloc + AllocHelper<Err = AllocErr>> RawVec<T, A> {
716716
/// Frees the memory owned by the RawVec *without* trying to Drop its contents.
717717
pub unsafe fn dealloc_buffer(&mut self) {
718718
let elem_size = mem::size_of::<T>();
@@ -724,7 +724,7 @@ impl<T, A: Alloc> RawVec<T, A> {
724724
}
725725
}
726726

727-
unsafe impl<#[may_dangle] T, A: Alloc> Drop for RawVec<T, A> {
727+
unsafe impl<#[may_dangle] T, A: Alloc + AllocHelper<Err = AllocErr>> Drop for RawVec<T, A> {
728728
/// Frees the memory owned by the RawVec *without* trying to Drop its contents.
729729
fn drop(&mut self) {
730730
unsafe { self.dealloc_buffer(); }

0 commit comments

Comments
 (0)