Skip to content

Commit 28457e1

Browse files
authored
Rollup merge of #99920 - emarteca:custom-allocator-support, r=oli-obk
Custom allocator support in `rustc_serialize` Adding support for `rustc_serialize` encode/decode for `Box` and `Vec` that use a custom allocator.
2 parents f7f80c2 + 258d367 commit 28457e1

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Diff for: compiler/rustc_serialize/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Core encoding and decoding interfaces.
1616
#![feature(maybe_uninit_slice)]
1717
#![feature(let_else)]
1818
#![feature(new_uninit)]
19+
#![feature(allocator_api)]
1920
#![cfg_attr(test, feature(test))]
2021
#![allow(rustc::internal)]
2122
#![deny(rustc::untranslatable_diagnostic)]

Diff for: compiler/rustc_serialize/src/serialize.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Core encoding and decoding interfaces.
55
*/
66

7+
use std::alloc::Allocator;
78
use std::borrow::Cow;
89
use std::cell::{Cell, RefCell};
910
use std::marker::PhantomData;
@@ -229,9 +230,9 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
229230
}
230231
}
231232

232-
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
233-
fn decode(d: &mut D) -> Box<[T]> {
234-
let v: Vec<T> = Decodable::decode(d);
233+
impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
234+
fn decode(d: &mut D) -> Box<[T], A> {
235+
let v: Vec<T, A> = Decodable::decode(d);
235236
v.into_boxed_slice()
236237
}
237238
}
@@ -264,12 +265,13 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
264265
}
265266
}
266267

267-
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
268-
default fn decode(d: &mut D) -> Vec<T> {
268+
impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
269+
default fn decode(d: &mut D) -> Vec<T, A> {
269270
let len = d.read_usize();
271+
let allocator = A::default();
270272
// SAFETY: we set the capacity in advance, only write elements, and
271273
// only set the length at the end once the writing has succeeded.
272-
let mut vec = Vec::with_capacity(len);
274+
let mut vec = Vec::with_capacity_in(len, allocator);
273275
unsafe {
274276
let ptr: *mut T = vec.as_mut_ptr();
275277
for i in 0..len {
@@ -457,13 +459,15 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
457459
}
458460
}
459461

460-
impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
462+
impl<S: Encoder, T: ?Sized + Encodable<S>, A: Allocator + Default> Encodable<S> for Box<T, A> {
461463
fn encode(&self, s: &mut S) {
462-
(**self).encode(s);
464+
(**self).encode(s)
463465
}
464466
}
465-
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
466-
fn decode(d: &mut D) -> Box<T> {
467-
Box::new(Decodable::decode(d))
467+
468+
impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<T, A> {
469+
fn decode(d: &mut D) -> Box<T, A> {
470+
let allocator = A::default();
471+
Box::new_in(Decodable::decode(d), allocator)
468472
}
469473
}

0 commit comments

Comments
 (0)