1acbe7573d
There are two impls of the `Encoder` trait: `opaque::Encoder` and `opaque::FileEncoder`. The former encodes into memory and is infallible, the latter writes to file and is fallible. Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a bit verbose and has non-trivial cost, which is annoying given how rare failures are (especially in the infallible `opaque::Encoder` case). This commit changes how `Encoder` fallibility is handled. All the `emit_*` methods are now infallible. `opaque::Encoder` requires no great changes for this. `opaque::FileEncoder` now implements a delayed error handling strategy. If a failure occurs, it records this via the `res` field, and all subsequent encoding operations are skipped if `res` indicates an error has occurred. Once encoding is complete, the new `finish` method is called, which returns a `Result`. In other words, there is now a single `Result`-producing method instead of many of them. This has very little effect on how any file errors are reported if `opaque::FileEncoder` has any failures. Much of this commit is boring mechanical changes, removing `Result` return values and `?` or `unwrap` from expressions. The more interesting parts are as follows. - serialize.rs: The `Encoder` trait gains an `Ok` associated type. The `into_inner` method is changed into `finish`, which returns `Result<Vec<u8>, !>`. - opaque.rs: The `FileEncoder` adopts the delayed error handling strategy. Its `Ok` type is a `usize`, returning the number of bytes written, replacing previous uses of `FileEncoder::position`. - Various methods that take an encoder now consume it, rather than being passed a mutable reference, e.g. `serialize_query_result_cache`.
268 lines
6.5 KiB
Rust
268 lines
6.5 KiB
Rust
//! Implementations of serialization for structures found in liballoc
|
|
|
|
use std::hash::{BuildHasher, Hash};
|
|
|
|
use crate::{Decodable, Decoder, Encodable, Encoder};
|
|
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
use smallvec::{Array, SmallVec};
|
|
|
|
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 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()
|
|
}
|
|
}
|