Skip to content
Prev Previous commit
Next Next commit
replace Allocator trait, add adapters
  • Loading branch information
pitaj committed Jun 6, 2023
commit dfb32b207546471ba4740eda73b53c9ef31a737e
12 changes: 10 additions & 2 deletions compiler/rustc_serialize/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::alloc::Allocator;
use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::collections::TryReserveError;
use std::marker::PhantomData;
use std::path;
use std::rc::Rc;
Expand Down Expand Up @@ -273,7 +274,11 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
}
}

impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
impl<D: Decoder, A: Default, T: Decodable<D>> Decodable<D> for Box<[T], A>
where
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
A: Allocator<Result<Box<[T], A>, TryReserveError> = Box<[T], A>>,
{
fn decode(d: &mut D) -> Box<[T], A> {
let v: Vec<T, A> = Decodable::decode(d);
v.into_boxed_slice()
Expand Down Expand Up @@ -308,7 +313,10 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
}
}

impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
impl<D: Decoder, T: Decodable<D>, A: Default> Decodable<D> for Vec<T, A>
where
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
{
default fn decode(d: &mut D) -> Vec<T, A> {
let len = d.read_usize();
let allocator = A::default();
Expand Down
31 changes: 30 additions & 1 deletion library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#![stable(feature = "alloc_module", since = "1.28.0")]

#[cfg(not(test))]
use core::error::Error;
#[cfg(not(test))]
use core::intrinsics;
use core::intrinsics::{min_align_of_val, size_of_val};
Expand All @@ -10,9 +12,15 @@ use core::ptr::Unique;
#[cfg(not(test))]
use core::ptr::{self, NonNull};

#[unstable(feature = "allocator_api", issue = "32838")]
pub use crate::falloc::{Allocator, FallibleAdapter};
#[unstable(feature = "allocator_api", issue = "32838")]
#[cfg(not(no_global_oom_handling))]
pub use crate::falloc::{InfallibleAdapter, IntoLayout};
#[stable(feature = "alloc_module", since = "1.28.0")]
#[doc(inline)]
pub use core::alloc::*;
#[allow(deprecated)]
pub use core::alloc::{AllocError, GlobalAlloc, Layout, LayoutErr, LayoutError};

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -321,6 +329,27 @@ unsafe impl Allocator for Global {
},
}
}

#[cfg(not(no_global_oom_handling))]
type Result<T, E: Error> = T
where
E: IntoLayout;

#[cfg(not(no_global_oom_handling))]
fn map_result<T, E: Error>(result: Result<T, E>) -> Self::Result<T, E>
where
E: IntoLayout,
{
result.unwrap_or_else(|e| handle_alloc_error(e.into_layout()))
}

#[cfg(no_global_oom_handling)]
type Result<T, E: Error> = Result<T, E>;

#[cfg(no_global_oom_handling)]
fn map_result<T, E: Error>(result: Result<T, E>) -> Self::Result<T, E> {
result
}
}

/// The allocator for unique pointers.
Expand Down
10 changes: 9 additions & 1 deletion library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,15 @@ impl<I> FromIterator<I> for Box<[I]> {

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl<T: Clone, A: Allocator<Result<Self, TryReserveError> = Self> + Clone> Clone for Box<[T], A> {
impl<T: Clone, A: Clone> Clone for Box<[T], A>
where
// Would like to see something like this work eventually,
// using `feature(non_lifetime_binders)` (#108185), but
// for now we'll have to enumerate each case that's needed.
// A: for<X, Y> Allocator<Result<X, Y> = X>,
A: Allocator<Result<Vec<T, A>, TryReserveError> = Vec<T, A>>,
A: Allocator<Result<Self, TryReserveError> = Self>,
{
fn clone(&self) -> Self {
let alloc = Box::allocator(self).clone();
self.to_vec_in(alloc).into_boxed_slice()
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/append.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::merge_iter::MergeIterInner;
use super::node::{self, Root};
use core::alloc::Allocator;
use crate::alloc::Allocator;
use core::iter::FusedIterator;

impl<K, V> Root<K, V> {
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/fix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::map::MIN_LEN;
use super::node::{marker, ForceResult::*, Handle, LeftOrRight::*, NodeRef, Root};
use core::alloc::Allocator;
use crate::alloc::Allocator;

impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
/// Stocks up a possibly underfull node by merging with or stealing from a
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/remove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::map::MIN_LEN;
use super::node::{marker, ForceResult::*, Handle, LeftOrRight::*, NodeRef};
use core::alloc::Allocator;
use crate::alloc::Allocator;

impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::KV> {
/// Removes a key-value pair from the tree, and returns that pair, as well as
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/split.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::node::{ForceResult::*, Root};
use super::search::SearchResult::*;
use core::alloc::Allocator;
use crate::alloc::Allocator;
use core::borrow::Borrow;

impl<K, V> Root<K, V> {
Expand Down
11 changes: 10 additions & 1 deletion library/alloc/src/collections/vec_deque/into_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use super::VecDeque;
/// (provided by the [`IntoIterator`] trait). See its documentation for more.
///
/// [`into_iter`]: VecDeque::into_iter
#[derive(Clone)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<
T,
Expand All @@ -21,6 +20,16 @@ pub struct IntoIter<
inner: VecDeque<T, A>,
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator> Clone for IntoIter<T, A>
where
VecDeque<T, A>: Clone,
{
fn clone(&self) -> Self {
Self { inner: self.inner.clone() }
}
}

impl<T, A: Allocator> IntoIter<T, A> {
pub(super) fn new(inner: VecDeque<T, A>) -> Self {
IntoIter { inner }
Expand Down
Loading