Skip to content

Commit 4b5eb49

Browse files
committed
refactor for object safety
uses separate trait so there's no generic associated types
1 parent adc0771 commit 4b5eb49

File tree

12 files changed

+206
-244
lines changed

12 files changed

+206
-244
lines changed

compiler/rustc_serialize/src/serialize.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Support code for encoding and decoding types.
22
3-
use std::alloc::Allocator;
3+
use std::alloc::{Allocator, Fatal};
44
use std::borrow::Cow;
55
use std::cell::{Cell, RefCell};
6-
use std::collections::TryReserveError;
76
use std::marker::PhantomData;
87
use std::path;
98
use std::rc::Rc;
@@ -276,8 +275,7 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
276275

277276
impl<D: Decoder, A: Default, T: Decodable<D>> Decodable<D> for Box<[T], A>
278277
where
279-
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
280-
A: Allocator<Result<Box<[T], A>, TryReserveError> = Box<[T], A>>,
278+
A: Allocator<ErrorHandling = Fatal>,
281279
{
282280
fn decode(d: &mut D) -> Box<[T], A> {
283281
let v: Vec<T, A> = Decodable::decode(d);
@@ -315,7 +313,7 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
315313

316314
impl<D: Decoder, T: Decodable<D>, A: Default> Decodable<D> for Vec<T, A>
317315
where
318-
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
316+
A: Allocator<ErrorHandling = Fatal>,
319317
{
320318
default fn decode(d: &mut D) -> Vec<T, A> {
321319
let len = d.read_usize();

library/alloc/src/alloc.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
#![stable(feature = "alloc_module", since = "1.28.0")]
44

5-
#[cfg(not(test))]
6-
use core::error::Error;
75
#[cfg(not(test))]
86
use core::intrinsics;
97
use core::intrinsics::{min_align_of_val, size_of_val};
@@ -13,10 +11,10 @@ use core::ptr::Unique;
1311
use core::ptr::{self, NonNull};
1412

1513
#[unstable(feature = "allocator_api", issue = "32838")]
16-
pub use crate::falloc::{Allocator, FallibleAdapter};
14+
pub use crate::falloc::{Allocator, Fallible};
1715
#[unstable(feature = "allocator_api", issue = "32838")]
1816
#[cfg(not(no_global_oom_handling))]
19-
pub use crate::falloc::{HandleAllocError, InfallibleAdapter};
17+
pub use crate::falloc::{FallibleAdapter, Fatal, HandleAllocError, InfallibleAdapter};
2018
#[stable(feature = "alloc_module", since = "1.28.0")]
2119
#[doc(inline)]
2220
#[allow(deprecated)]
@@ -331,25 +329,9 @@ unsafe impl Allocator for Global {
331329
}
332330

333331
#[cfg(not(no_global_oom_handling))]
334-
type Result<T, E: Error> = T
335-
where
336-
E: HandleAllocError;
337-
338-
#[cfg(not(no_global_oom_handling))]
339-
fn map_result<T, E: Error>(result: Result<T, E>) -> Self::Result<T, E>
340-
where
341-
E: HandleAllocError,
342-
{
343-
result.unwrap_or_else(|e| e.handle_alloc_error())
344-
}
345-
346-
#[cfg(no_global_oom_handling)]
347-
type Result<T, E: Error> = Result<T, E>;
348-
332+
type ErrorHandling = Fatal;
349333
#[cfg(no_global_oom_handling)]
350-
fn map_result<T, E: Error>(result: Result<T, E>) -> Self::Result<T, E> {
351-
result
352-
}
334+
type ErrorHandling = Fallible;
353335
}
354336

355337
/// The allocator for unique pointers.

library/alloc/src/boxed.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw};
170170
#[cfg(not(no_global_oom_handling))]
171171
use crate::borrow::Cow;
172172
use crate::collections::TryReserveError;
173-
use crate::falloc::{AllocError, Allocator, Global, Layout};
173+
#[cfg(not(no_global_oom_handling))]
174+
use crate::falloc::Fatal;
175+
use crate::falloc::{AllocError, AllocResult, Allocator, ErrorHandling, Global, Layout};
174176
use crate::raw_vec::RawVec;
175177
#[cfg(not(no_global_oom_handling))]
176178
use crate::str::from_boxed_utf8_unchecked;
@@ -626,7 +628,8 @@ impl<T> Box<[T]> {
626628
#[must_use]
627629
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
628630
unsafe {
629-
<Global as Allocator>::map_result(RawVec::with_capacity_in(len, Global)).into_box(len)
631+
<Global as Allocator>::ErrorHandling::map_result(RawVec::with_capacity_in(len, Global))
632+
.into_box(len)
630633
}
631634
}
632635

@@ -653,8 +656,10 @@ impl<T> Box<[T]> {
653656
#[must_use]
654657
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
655658
unsafe {
656-
<Global as Allocator>::map_result(RawVec::with_capacity_zeroed_in(len, Global))
657-
.into_box(len)
659+
<Global as Allocator>::ErrorHandling::map_result(RawVec::with_capacity_zeroed_in(
660+
len, Global,
661+
))
662+
.into_box(len)
658663
}
659664
}
660665

@@ -741,8 +746,12 @@ impl<T, A: Allocator> Box<[T], A> {
741746
pub fn new_uninit_slice_in(
742747
len: usize,
743748
alloc: A,
744-
) -> A::Result<Box<[mem::MaybeUninit<T>], A>, TryReserveError> {
745-
unsafe { A::map_result(RawVec::with_capacity_in(len, alloc).map(|r| r.into_box(len))) }
749+
) -> AllocResult<A, Box<[mem::MaybeUninit<T>], A>, TryReserveError> {
750+
unsafe {
751+
A::ErrorHandling::map_result(
752+
RawVec::with_capacity_in(len, alloc).map(|r| r.into_box(len)),
753+
)
754+
}
746755
}
747756

748757
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -771,9 +780,11 @@ impl<T, A: Allocator> Box<[T], A> {
771780
pub fn new_zeroed_slice_in(
772781
len: usize,
773782
alloc: A,
774-
) -> A::Result<Box<[mem::MaybeUninit<T>], A>, TryReserveError> {
783+
) -> AllocResult<A, Box<[mem::MaybeUninit<T>], A>, TryReserveError> {
775784
unsafe {
776-
A::map_result(RawVec::with_capacity_zeroed_in(len, alloc).map(|r| r.into_box(len)))
785+
A::ErrorHandling::map_result(
786+
RawVec::with_capacity_zeroed_in(len, alloc).map(|r| r.into_box(len)),
787+
)
777788
}
778789
}
779790
}
@@ -1474,7 +1485,8 @@ impl<T: Copy> BoxFromSlice<T> for Box<[T]> {
14741485
#[inline]
14751486
fn from_slice(slice: &[T]) -> Self {
14761487
let len = slice.len();
1477-
let buf = <Global as Allocator>::map_result(RawVec::with_capacity_in(len, Global));
1488+
let buf =
1489+
<Global as Allocator>::ErrorHandling::map_result(RawVec::with_capacity_in(len, Global));
14781490
unsafe {
14791491
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
14801492
buf.into_box(slice.len()).assume_init()
@@ -2018,12 +2030,7 @@ impl<I> FromIterator<I> for Box<[I]> {
20182030
#[stable(feature = "box_slice_clone", since = "1.3.0")]
20192031
impl<T: Clone, A: Clone> Clone for Box<[T], A>
20202032
where
2021-
// Would like to see something like this work eventually,
2022-
// using `feature(non_lifetime_binders)` (#108185), but
2023-
// for now we'll have to enumerate each case that's needed.
2024-
// A: for<X, Y> Allocator<Result<X, Y> = X>,
2025-
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
2026-
A: Allocator<Result<Self, TryReserveError> = Self>,
2033+
A: Allocator<ErrorHandling = Fatal>,
20272034
{
20282035
fn clone(&self) -> Self {
20292036
let alloc = Box::allocator(self).clone();

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::mem;
2424

2525
use crate::collections::TryReserveError;
2626
use crate::collections::TryReserveErrorKind;
27-
use crate::falloc::{Allocator, Global};
27+
use crate::falloc::{AllocResult, Allocator, ErrorHandling, Fatal, Global};
2828
use crate::raw_vec::RawVec;
2929
use crate::vec::Vec;
3030

@@ -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, TryReserveError> = Self> + Clone,
111+
A: Allocator<ErrorHandling = Fatal> + Clone,
112112
{
113113
fn clone(&self) -> Self {
114114
let mut deq = Self::with_capacity_in(self.len(), self.allocator().clone());
@@ -597,8 +597,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
597597
pub fn with_capacity_in(
598598
capacity: usize,
599599
alloc: A,
600-
) -> A::Result<VecDeque<T, A>, TryReserveError> {
601-
A::map_result(Self::try_with_capacity_in(capacity, alloc))
600+
) -> AllocResult<A, VecDeque<T, A>, TryReserveError> {
601+
A::ErrorHandling::map_result(Self::try_with_capacity_in(capacity, alloc))
602602
}
603603

604604
/// Creates a `VecDeque` from a raw allocation, when the initialized
@@ -761,8 +761,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
761761
///
762762
/// [`reserve`]: VecDeque::reserve
763763
#[stable(feature = "rust1", since = "1.0.0")]
764-
pub fn reserve_exact(&mut self, additional: usize) -> A::Result<(), TryReserveError> {
765-
A::map_result(self.try_reserve_exact(additional))
764+
pub fn reserve_exact(&mut self, additional: usize) -> AllocResult<A, (), TryReserveError> {
765+
A::ErrorHandling::map_result(self.try_reserve_exact(additional))
766766
}
767767

768768
/// Reserves capacity for at least `additional` more elements to be inserted in the given
@@ -782,8 +782,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
782782
/// assert!(buf.capacity() >= 11);
783783
/// ```
784784
#[stable(feature = "rust1", since = "1.0.0")]
785-
pub fn reserve(&mut self, additional: usize) -> A::Result<(), TryReserveError> {
786-
A::map_result(self.try_reserve(additional))
785+
pub fn reserve(&mut self, additional: usize) -> AllocResult<A, (), TryReserveError> {
786+
A::ErrorHandling::map_result(self.try_reserve(additional))
787787
}
788788

789789
/// Tries to reserve the minimum capacity for at least `additional` more elements to
@@ -928,8 +928,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
928928
/// assert!(buf.capacity() >= 4);
929929
/// ```
930930
#[stable(feature = "shrink_to", since = "1.56.0")]
931-
pub fn shrink_to(&mut self, min_capacity: usize) -> A::Result<(), TryReserveError> {
932-
A::map_result((|| {
931+
pub fn shrink_to(&mut self, min_capacity: usize) -> AllocResult<A, (), TryReserveError> {
932+
A::ErrorHandling::map_result((|| {
933933
// Substitute for try block
934934
let target_cap = min_capacity.max(self.len);
935935

@@ -1625,8 +1625,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
16251625
/// assert_eq!(d.front(), Some(&2));
16261626
/// ```
16271627
#[stable(feature = "rust1", since = "1.0.0")]
1628-
pub fn push_front(&mut self, value: T) -> A::Result<(), TryReserveError> {
1629-
A::map_result((|| {
1628+
pub fn push_front(&mut self, value: T) -> AllocResult<A, (), TryReserveError> {
1629+
A::ErrorHandling::map_result((|| {
16301630
// Substitute for try block
16311631
if self.is_full() {
16321632
self.grow()?;
@@ -1656,8 +1656,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
16561656
/// assert_eq!(3, *buf.back().unwrap());
16571657
/// ```
16581658
#[stable(feature = "rust1", since = "1.0.0")]
1659-
pub fn push_back(&mut self, value: T) -> A::Result<(), TryReserveError> {
1660-
A::map_result((|| {
1659+
pub fn push_back(&mut self, value: T) -> AllocResult<A, (), TryReserveError> {
1660+
A::ErrorHandling::map_result((|| {
16611661
// Substsitute for try block
16621662
if self.is_full() {
16631663
self.grow()?;
@@ -1770,8 +1770,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
17701770
/// assert_eq!(vec_deque, &['a', 'd', 'b', 'c']);
17711771
/// ```
17721772
#[stable(feature = "deque_extras_15", since = "1.5.0")]
1773-
pub fn insert(&mut self, index: usize, value: T) -> A::Result<(), TryReserveError> {
1774-
A::map_result((|| {
1773+
pub fn insert(&mut self, index: usize, value: T) -> AllocResult<A, (), TryReserveError> {
1774+
A::ErrorHandling::map_result((|| {
17751775
// Substitute for try block
17761776
assert!(index <= self.len(), "index out of bounds");
17771777
if self.is_full() {
@@ -1877,11 +1877,11 @@ impl<T, A: Allocator> VecDeque<T, A> {
18771877
#[inline]
18781878
#[must_use = "use `.truncate()` if you don't need the other half"]
18791879
#[stable(feature = "split_off", since = "1.4.0")]
1880-
pub fn split_off(&mut self, at: usize) -> A::Result<Self, TryReserveError>
1880+
pub fn split_off(&mut self, at: usize) -> AllocResult<A, Self, TryReserveError>
18811881
where
18821882
A: Clone,
18831883
{
1884-
A::map_result((|| {
1884+
A::ErrorHandling::map_result((|| {
18851885
let len = self.len;
18861886
assert!(at <= len, "`at` out of bounds");
18871887

0 commit comments

Comments
 (0)