Skip to content

Commit dfb32b2

Browse files
committed
replace Allocator trait, add adapters
1 parent 2b15154 commit dfb32b2

File tree

17 files changed

+653
-42
lines changed

17 files changed

+653
-42
lines changed

compiler/rustc_serialize/src/serialize.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::alloc::Allocator;
44
use std::borrow::Cow;
55
use std::cell::{Cell, RefCell};
6+
use std::collections::TryReserveError;
67
use std::marker::PhantomData;
78
use std::path;
89
use std::rc::Rc;
@@ -273,7 +274,11 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
273274
}
274275
}
275276

276-
impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
277+
impl<D: Decoder, A: Default, T: Decodable<D>> Decodable<D> for Box<[T], A>
278+
where
279+
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
280+
A: Allocator<Result<Box<[T], A>, TryReserveError> = Box<[T], A>>,
281+
{
277282
fn decode(d: &mut D) -> Box<[T], A> {
278283
let v: Vec<T, A> = Decodable::decode(d);
279284
v.into_boxed_slice()
@@ -308,7 +313,10 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
308313
}
309314
}
310315

311-
impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
316+
impl<D: Decoder, T: Decodable<D>, A: Default> Decodable<D> for Vec<T, A>
317+
where
318+
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
319+
{
312320
default fn decode(d: &mut D) -> Vec<T, A> {
313321
let len = d.read_usize();
314322
let allocator = A::default();

library/alloc/src/alloc.rs

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

5+
#[cfg(not(test))]
6+
use core::error::Error;
57
#[cfg(not(test))]
68
use core::intrinsics;
79
use core::intrinsics::{min_align_of_val, size_of_val};
@@ -10,9 +12,15 @@ use core::ptr::Unique;
1012
#[cfg(not(test))]
1113
use core::ptr::{self, NonNull};
1214

15+
#[unstable(feature = "allocator_api", issue = "32838")]
16+
pub use crate::falloc::{Allocator, FallibleAdapter};
17+
#[unstable(feature = "allocator_api", issue = "32838")]
18+
#[cfg(not(no_global_oom_handling))]
19+
pub use crate::falloc::{InfallibleAdapter, IntoLayout};
1320
#[stable(feature = "alloc_module", since = "1.28.0")]
1421
#[doc(inline)]
15-
pub use core::alloc::*;
22+
#[allow(deprecated)]
23+
pub use core::alloc::{AllocError, GlobalAlloc, Layout, LayoutErr, LayoutError};
1624

1725
#[cfg(test)]
1826
mod tests;
@@ -321,6 +329,27 @@ unsafe impl Allocator for Global {
321329
},
322330
}
323331
}
332+
333+
#[cfg(not(no_global_oom_handling))]
334+
type Result<T, E: Error> = T
335+
where
336+
E: IntoLayout;
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: IntoLayout,
342+
{
343+
result.unwrap_or_else(|e| handle_alloc_error(e.into_layout()))
344+
}
345+
346+
#[cfg(no_global_oom_handling)]
347+
type Result<T, E: Error> = Result<T, E>;
348+
349+
#[cfg(no_global_oom_handling)]
350+
fn map_result<T, E: Error>(result: Result<T, E>) -> Self::Result<T, E> {
351+
result
352+
}
324353
}
325354

326355
/// The allocator for unique pointers.

library/alloc/src/boxed.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,15 @@ impl<I> FromIterator<I> for Box<[I]> {
20162016

20172017
#[cfg(not(no_global_oom_handling))]
20182018
#[stable(feature = "box_slice_clone", since = "1.3.0")]
2019-
impl<T: Clone, A: Allocator<Result<Self, TryReserveError> = Self> + Clone> Clone for Box<[T], A> {
2019+
impl<T: Clone, A: Clone> Clone for Box<[T], A>
2020+
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>,
2027+
{
20202028
fn clone(&self) -> Self {
20212029
let alloc = Box::allocator(self).clone();
20222030
self.to_vec_in(alloc).into_boxed_slice()

library/alloc/src/collections/btree/append.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::merge_iter::MergeIterInner;
22
use super::node::{self, Root};
3-
use core::alloc::Allocator;
3+
use crate::alloc::Allocator;
44
use core::iter::FusedIterator;
55

66
impl<K, V> Root<K, V> {

library/alloc/src/collections/btree/fix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::map::MIN_LEN;
22
use super::node::{marker, ForceResult::*, Handle, LeftOrRight::*, NodeRef, Root};
3-
use core::alloc::Allocator;
3+
use crate::alloc::Allocator;
44

55
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
66
/// Stocks up a possibly underfull node by merging with or stealing from a

library/alloc/src/collections/btree/remove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::map::MIN_LEN;
22
use super::node::{marker, ForceResult::*, Handle, LeftOrRight::*, NodeRef};
3-
use core::alloc::Allocator;
3+
use crate::alloc::Allocator;
44

55
impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::KV> {
66
/// Removes a key-value pair from the tree, and returns that pair, as well as

library/alloc/src/collections/btree/split.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::node::{ForceResult::*, Root};
22
use super::search::SearchResult::*;
3-
use core::alloc::Allocator;
3+
use crate::alloc::Allocator;
44
use core::borrow::Borrow;
55

66
impl<K, V> Root<K, V> {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use super::VecDeque;
1212
/// (provided by the [`IntoIterator`] trait). See its documentation for more.
1313
///
1414
/// [`into_iter`]: VecDeque::into_iter
15-
#[derive(Clone)]
1615
#[stable(feature = "rust1", since = "1.0.0")]
1716
pub struct IntoIter<
1817
T,
@@ -21,6 +20,16 @@ pub struct IntoIter<
2120
inner: VecDeque<T, A>,
2221
}
2322

23+
#[stable(feature = "rust1", since = "1.0.0")]
24+
impl<T, A: Allocator> Clone for IntoIter<T, A>
25+
where
26+
VecDeque<T, A>: Clone,
27+
{
28+
fn clone(&self) -> Self {
29+
Self { inner: self.inner.clone() }
30+
}
31+
}
32+
2433
impl<T, A: Allocator> IntoIter<T, A> {
2534
pub(super) fn new(inner: VecDeque<T, A>) -> Self {
2635
IntoIter { inner }

0 commit comments

Comments
 (0)