rustc_serialize: merge collection_impls.rs into serialize.rs.

`serialize.rs` has the `Encodable`/`Decodable` impls for lots of basic
types, including `Vec`. `collection_impls` has it for lots of collection
types. The distinction isn't really meaningful, and it's simpler to have
them all in a single file.
This commit is contained in:
Nicholas Nethercote 2023-10-06 09:50:23 +11:00
parent f703475b4e
commit 5f69ca62f2
3 changed files with 258 additions and 264 deletions

View File

@ -1,263 +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, 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 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

@ -21,7 +21,6 @@
pub use self::serialize::{Decodable, Decoder, Encodable, Encoder}; pub use self::serialize::{Decodable, Decoder, Encodable, Encoder};
mod collection_impls;
mod serialize; mod serialize;
pub mod leb128; pub mod leb128;

View File

@ -1,12 +1,16 @@
//! Support code for encoding and decoding types. //! Support code for encoding and decoding types.
use smallvec::{Array, SmallVec};
use std::alloc::Allocator; use std::alloc::Allocator;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::path; use std::path;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; 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. /// 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 /// This way we can skip validation and still be relatively sure that deserialization
@ -509,3 +513,257 @@ fn decode(d: &mut D) -> Box<T, A> {
Box::new_in(Decodable::decode(d), allocator) Box::new_in(Decodable::decode(d), allocator)
} }
} }
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 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()
}
}