Auto merge of #116468 - nnethercote:rustc_serialize, r=Mark-Simulacrum

Streamline `rustc_serialize`

r? `@Mark-Simulacrum`
This commit is contained in:
bors 2023-10-09 00:03:52 +00:00
commit 37fda989ea
3 changed files with 241 additions and 319 deletions

View File

@ -1,279 +0,0 @@
//! Implementations of serialization for structures found in liballoc
use crate::{Decodable, Decoder, Encodable, Encoder};
use smallvec::{Array, SmallVec};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
use std::hash::{BuildHasher, Hash};
use std::rc::Rc;
use std::sync::Arc;
use thin_vec::ThinVec;
impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
fn encode(&self, s: &mut S) {
let slice: &[A::Item] = self;
slice.encode(s);
}
}
impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
fn decode(d: &mut D) -> SmallVec<A> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, T: Encodable<S>> Encodable<S> for ThinVec<T> {
fn encode(&self, s: &mut S) {
self.as_slice().encode(s);
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for ThinVec<T> {
fn decode(d: &mut D) -> ThinVec<T> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, T: Encodable<S>> Encodable<S> for LinkedList<T> {
fn encode(&self, s: &mut S) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for LinkedList<T> {
fn decode(d: &mut D) -> LinkedList<T> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
fn encode(&self, s: &mut S) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
fn decode(d: &mut D) -> VecDeque<T> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V>
where
K: Encodable<S> + PartialEq + Ord,
V: Encodable<S>,
{
fn encode(&self, e: &mut S) {
e.emit_usize(self.len());
for (key, val) in self.iter() {
key.encode(e);
val.encode(e);
}
}
}
impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
where
K: Decodable<D> + PartialEq + Ord,
V: Decodable<D>,
{
fn decode(d: &mut D) -> BTreeMap<K, V> {
let len = d.read_usize();
let mut map = BTreeMap::new();
for _ in 0..len {
let key = Decodable::decode(d);
let val = Decodable::decode(d);
map.insert(key, val);
}
map
}
}
impl<S: Encoder, T> Encodable<S> for BTreeSet<T>
where
T: Encodable<S> + PartialEq + Ord,
{
fn encode(&self, s: &mut S) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
where
T: Decodable<D> + PartialEq + Ord,
{
fn decode(d: &mut D) -> BTreeSet<T> {
let len = d.read_usize();
let mut set = BTreeSet::new();
for _ in 0..len {
set.insert(Decodable::decode(d));
}
set
}
}
impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S>
where
K: Encodable<E> + Eq,
V: Encodable<E>,
S: BuildHasher,
{
fn encode(&self, e: &mut E) {
e.emit_usize(self.len());
for (key, val) in self.iter() {
key.encode(e);
val.encode(e);
}
}
}
impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
where
K: Decodable<D> + Hash + Eq,
V: Decodable<D>,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> HashMap<K, V, S> {
let len = d.read_usize();
let state = Default::default();
let mut map = HashMap::with_capacity_and_hasher(len, state);
for _ in 0..len {
let key = Decodable::decode(d);
let val = Decodable::decode(d);
map.insert(key, val);
}
map
}
}
impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S>
where
T: Encodable<E> + Eq,
S: BuildHasher,
{
fn encode(&self, s: &mut E) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
where
T: Decodable<D> + Hash + Eq,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> HashSet<T, S> {
let len = d.read_usize();
let state = Default::default();
let mut set = HashSet::with_capacity_and_hasher(len, state);
for _ in 0..len {
set.insert(Decodable::decode(d));
}
set
}
}
impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S>
where
K: Encodable<E> + Hash + Eq,
V: Encodable<E>,
S: BuildHasher,
{
fn encode(&self, e: &mut E) {
e.emit_usize(self.len());
for (key, val) in self.iter() {
key.encode(e);
val.encode(e);
}
}
}
impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
where
K: Decodable<D> + Hash + Eq,
V: Decodable<D>,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
let len = d.read_usize();
let state = Default::default();
let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state);
for _ in 0..len {
let key = Decodable::decode(d);
let val = Decodable::decode(d);
map.insert(key, val);
}
map
}
}
impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S>
where
T: Encodable<E> + Hash + Eq,
S: BuildHasher,
{
fn encode(&self, s: &mut E) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
where
T: Decodable<D> + Hash + Eq,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> indexmap::IndexSet<T, S> {
let len = d.read_usize();
let state = Default::default();
let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state);
for _ in 0..len {
set.insert(Decodable::decode(d));
}
set
}
}
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
fn encode(&self, s: &mut E) {
let slice: &[T] = self;
slice.encode(s);
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
fn decode(d: &mut D) -> Rc<[T]> {
let vec: Vec<T> = Decodable::decode(d);
vec.into()
}
}
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
fn encode(&self, s: &mut E) {
let slice: &[T] = self;
slice.encode(s);
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> {
fn decode(d: &mut D) -> Arc<[T]> {
let vec: Vec<T> = Decodable::decode(d);
vec.into()
}
}

View File

@ -1,25 +1,19 @@
//! Support code for encoding and decoding types.
/*
Core encoding and decoding interfaces.
*/
#![doc(
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
html_playground_url = "https://play.rust-lang.org/",
test(attr(allow(unused_variables), deny(warnings)))
)]
#![feature(never_type)]
#![feature(associated_type_bounds)]
#![feature(min_specialization)]
#![feature(core_intrinsics)]
#![feature(maybe_uninit_slice)]
#![feature(new_uninit)]
#![feature(allocator_api)]
#![feature(associated_type_bounds)]
#![feature(const_option)]
#![feature(core_intrinsics)]
#![feature(inline_const)]
#![feature(min_specialization)]
#![feature(never_type)]
#![feature(ptr_sub_ptr)]
#![feature(slice_first_last_chunk)]
#![feature(inline_const)]
#![feature(const_option)]
#![cfg_attr(test, feature(test))]
#![allow(rustc::internal)]
#![deny(rustc::untranslatable_diagnostic)]
@ -27,7 +21,6 @@ Core encoding and decoding interfaces.
pub use self::serialize::{Decodable, Decoder, Encodable, Encoder};
mod collection_impls;
mod serialize;
pub mod leb128;

View File

@ -1,12 +1,15 @@
//! Support code for encoding and decoding types.
use std::alloc::Allocator;
use smallvec::{Array, SmallVec};
use std::borrow::Cow;
use std::cell::{Cell, RefCell};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;
use std::path;
use std::rc::Rc;
use std::sync::Arc;
use thin_vec::ThinVec;
/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
/// This way we can skip validation and still be relatively sure that deserialization
@ -273,9 +276,9 @@ impl<D: Decoder, T> Decodable<D> for PhantomData<T> {
}
}
impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<[T], A> {
fn decode(d: &mut D) -> Box<[T], A> {
let v: Vec<T, A> = Decodable::decode(d);
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<[T]> {
fn decode(d: &mut D) -> Box<[T]> {
let v: Vec<T> = Decodable::decode(d);
v.into_boxed_slice()
}
}
@ -303,33 +306,20 @@ impl<S: Encoder, T: Encodable<S>> Encodable<S> for [T] {
impl<S: Encoder, T: Encodable<S>> Encodable<S> for Vec<T> {
fn encode(&self, s: &mut S) {
let slice: &[T] = self;
slice.encode(s);
self.as_slice().encode(s);
}
}
impl<D: Decoder, T: Decodable<D>, A: Allocator + Default> Decodable<D> for Vec<T, A> {
default fn decode(d: &mut D) -> Vec<T, A> {
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Vec<T> {
default fn decode(d: &mut D) -> Vec<T> {
let len = d.read_usize();
let allocator = A::default();
// SAFETY: we set the capacity in advance, only write elements, and
// only set the length at the end once the writing has succeeded.
let mut vec = Vec::with_capacity_in(len, allocator);
unsafe {
let ptr: *mut T = vec.as_mut_ptr();
for i in 0..len {
std::ptr::write(ptr.add(i), Decodable::decode(d));
}
vec.set_len(len);
}
vec
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, T: Encodable<S>, const N: usize> Encodable<S> for [T; N] {
fn encode(&self, s: &mut S) {
let slice: &[T] = self;
slice.encode(s);
self.as_slice().encode(s);
}
}
@ -497,15 +487,233 @@ impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<T> {
}
}
impl<S: Encoder, T: ?Sized + Encodable<S>, A: Allocator + Default> Encodable<S> for Box<T, A> {
impl<S: Encoder, T: ?Sized + Encodable<S>> Encodable<S> for Box<T> {
fn encode(&self, s: &mut S) {
(**self).encode(s)
}
}
impl<D: Decoder, A: Allocator + Default, T: Decodable<D>> Decodable<D> for Box<T, A> {
fn decode(d: &mut D) -> Box<T, A> {
let allocator = A::default();
Box::new_in(Decodable::decode(d), allocator)
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Box<T> {
fn decode(d: &mut D) -> Box<T> {
Box::new(Decodable::decode(d))
}
}
impl<S: Encoder, A: Array<Item: Encodable<S>>> Encodable<S> for SmallVec<A> {
fn encode(&self, s: &mut S) {
self.as_slice().encode(s);
}
}
impl<D: Decoder, A: Array<Item: Decodable<D>>> Decodable<D> for SmallVec<A> {
fn decode(d: &mut D) -> SmallVec<A> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, T: Encodable<S>> Encodable<S> for ThinVec<T> {
fn encode(&self, s: &mut S) {
self.as_slice().encode(s);
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for ThinVec<T> {
fn decode(d: &mut D) -> ThinVec<T> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, T: Encodable<S>> Encodable<S> for VecDeque<T> {
fn encode(&self, s: &mut S) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for VecDeque<T> {
fn decode(d: &mut D) -> VecDeque<T> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<S: Encoder, K, V> Encodable<S> for BTreeMap<K, V>
where
K: Encodable<S> + PartialEq + Ord,
V: Encodable<S>,
{
fn encode(&self, e: &mut S) {
e.emit_usize(self.len());
for (key, val) in self.iter() {
key.encode(e);
val.encode(e);
}
}
}
impl<D: Decoder, K, V> Decodable<D> for BTreeMap<K, V>
where
K: Decodable<D> + PartialEq + Ord,
V: Decodable<D>,
{
fn decode(d: &mut D) -> BTreeMap<K, V> {
let len = d.read_usize();
(0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
}
}
impl<S: Encoder, T> Encodable<S> for BTreeSet<T>
where
T: Encodable<S> + PartialEq + Ord,
{
fn encode(&self, s: &mut S) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T> Decodable<D> for BTreeSet<T>
where
T: Decodable<D> + PartialEq + Ord,
{
fn decode(d: &mut D) -> BTreeSet<T> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<E: Encoder, K, V, S> Encodable<E> for HashMap<K, V, S>
where
K: Encodable<E> + Eq,
V: Encodable<E>,
S: BuildHasher,
{
fn encode(&self, e: &mut E) {
e.emit_usize(self.len());
for (key, val) in self.iter() {
key.encode(e);
val.encode(e);
}
}
}
impl<D: Decoder, K, V, S> Decodable<D> for HashMap<K, V, S>
where
K: Decodable<D> + Hash + Eq,
V: Decodable<D>,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> HashMap<K, V, S> {
let len = d.read_usize();
(0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
}
}
impl<E: Encoder, T, S> Encodable<E> for HashSet<T, S>
where
T: Encodable<E> + Eq,
S: BuildHasher,
{
fn encode(&self, s: &mut E) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T, S> Decodable<D> for HashSet<T, S>
where
T: Decodable<D> + Hash + Eq,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> HashSet<T, S> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<E: Encoder, K, V, S> Encodable<E> for indexmap::IndexMap<K, V, S>
where
K: Encodable<E> + Hash + Eq,
V: Encodable<E>,
S: BuildHasher,
{
fn encode(&self, e: &mut E) {
e.emit_usize(self.len());
for (key, val) in self.iter() {
key.encode(e);
val.encode(e);
}
}
}
impl<D: Decoder, K, V, S> Decodable<D> for indexmap::IndexMap<K, V, S>
where
K: Decodable<D> + Hash + Eq,
V: Decodable<D>,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> indexmap::IndexMap<K, V, S> {
let len = d.read_usize();
(0..len).map(|_| (Decodable::decode(d), Decodable::decode(d))).collect()
}
}
impl<E: Encoder, T, S> Encodable<E> for indexmap::IndexSet<T, S>
where
T: Encodable<E> + Hash + Eq,
S: BuildHasher,
{
fn encode(&self, s: &mut E) {
s.emit_usize(self.len());
for e in self.iter() {
e.encode(s);
}
}
}
impl<D: Decoder, T, S> Decodable<D> for indexmap::IndexSet<T, S>
where
T: Decodable<D> + Hash + Eq,
S: BuildHasher + Default,
{
fn decode(d: &mut D) -> indexmap::IndexSet<T, S> {
let len = d.read_usize();
(0..len).map(|_| Decodable::decode(d)).collect()
}
}
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Rc<[T]> {
fn encode(&self, s: &mut E) {
let slice: &[T] = self;
slice.encode(s);
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Rc<[T]> {
fn decode(d: &mut D) -> Rc<[T]> {
let vec: Vec<T> = Decodable::decode(d);
vec.into()
}
}
impl<E: Encoder, T: Encodable<E>> Encodable<E> for Arc<[T]> {
fn encode(&self, s: &mut E) {
let slice: &[T] = self;
slice.encode(s);
}
}
impl<D: Decoder, T: Decodable<D>> Decodable<D> for Arc<[T]> {
fn decode(d: &mut D) -> Arc<[T]> {
let vec: Vec<T> = Decodable::decode(d);
vec.into()
}
}