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.
This commit is contained in:
Yuki Okushi 2022-08-26 09:51:41 +09:00 committed by GitHub
commit 28457e10f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View File

@ -16,6 +16,7 @@ Core encoding and decoding interfaces.
#![feature(maybe_uninit_slice)] #![feature(maybe_uninit_slice)]
#![feature(let_else)] #![feature(let_else)]
#![feature(new_uninit)] #![feature(new_uninit)]
#![feature(allocator_api)]
#![cfg_attr(test, feature(test))] #![cfg_attr(test, feature(test))]
#![allow(rustc::internal)] #![allow(rustc::internal)]
#![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::untranslatable_diagnostic)]

View File

@ -4,6 +4,7 @@
Core encoding and decoding interfaces. Core encoding and decoding interfaces.
*/ */
use std::alloc::Allocator;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::marker::PhantomData; use std::marker::PhantomData;
@ -229,9 +230,9 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
} }
} }
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> { impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
fn decode(d: &mut D) -> Box<[T]> { fn decode(d: &mut D) -> Box<[T], A> {
let v: Vec<T> = Decodable::decode(d); let v: Vec<T, A> = Decodable::decode(d);
v.into_boxed_slice() v.into_boxed_slice()
} }
} }
@ -264,12 +265,13 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
} }
} }
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> { impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
default fn decode(d: &mut D) -> Vec<T> { default fn decode(d: &mut D) -> Vec<T, A> {
let len = d.read_usize(); let len = d.read_usize();
let allocator = A::default();
// SAFETY: we set the capacity in advance, only write elements, and // SAFETY: we set the capacity in advance, only write elements, and
// only set the length at the end once the writing has succeeded. // only set the length at the end once the writing has succeeded.
let mut vec = Vec::with_capacity(len); let mut vec = Vec::with_capacity_in(len, allocator);
unsafe { unsafe {
let ptr: *mut T = vec.as_mut_ptr(); let ptr: *mut T = vec.as_mut_ptr();
for i in 0..len { for i in 0..len {
@ -457,13 +459,15 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
} }
} }
impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> { impl<S: Encoder, T: ?Sized + Encodable<S>, A: Allocator + Default> Encodable<S> for Box<T, A> {
fn encode(&self, s: &mut S) { fn encode(&self, s: &mut S) {
(**self).encode(s); (**self).encode(s)
} }
} }
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
fn decode(d: &mut D) -> Box<T> { impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<T, A> {
Box::new(Decodable::decode(d)) fn decode(d: &mut D) -> Box<T, A> {
let allocator = A::default();
Box::new_in(Decodable::decode(d), allocator)
} }
} }