Add Encodable and Decodable impls for Arc<[T]>

This commit is contained in:
John Kåre Alsaker 2017-12-03 14:31:27 +01:00
parent 30733b3e68
commit 84ce4f1c10

View File

@ -15,6 +15,7 @@ use std::hash::{Hash, BuildHasher};
use {Decodable, Encodable, Decoder, Encoder};
use std::collections::{LinkedList, VecDeque, BTreeMap, BTreeSet, HashMap, HashSet};
use std::rc::Rc;
use std::sync::Arc;
impl<
T: Encodable
@ -218,3 +219,26 @@ impl<T: Decodable> Decodable for Rc<[T]> {
})
}
}
impl<T: Encodable> Encodable for Arc<[T]> {
fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> {
s.emit_seq(self.len(), |s| {
for (index, e) in self.iter().enumerate() {
s.emit_seq_elt(index, |s| e.encode(s))?;
}
Ok(())
})
}
}
impl<T: Decodable> Decodable for Arc<[T]> {
fn decode<D: Decoder>(d: &mut D) -> Result<Arc<[T]>, D::Error> {
d.read_seq(|d, len| {
let mut vec = Vec::with_capacity(len);
for index in 0..len {
vec.push(d.read_seq_elt(index, |d| Decodable::decode(d))?);
}
Ok(vec.into())
})
}
}