//! 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> Encodable for SmallVec { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; } Ok(()) }) } } impl> Decodable for SmallVec { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut vec = SmallVec::with_capacity(len); // FIXME(#48994) - could just be collected into a Result for i in 0..len { vec.push(d.read_seq_elt(i, |d| Decodable::decode(d))?); } Ok(vec) }) } } impl Encodable for LinkedList { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; } Ok(()) }) } } impl Decodable for LinkedList { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut list = LinkedList::new(); for i in 0..len { list.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?); } Ok(list) }) } } impl Encodable for VecDeque { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; } Ok(()) }) } } impl Decodable for VecDeque { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut deque: VecDeque = VecDeque::with_capacity(len); for i in 0..len { deque.push_back(d.read_seq_elt(i, |d| Decodable::decode(d))?); } Ok(deque) }) } } impl Encodable for BTreeMap where K: Encodable + PartialEq + Ord, V: Encodable, { fn encode(&self, e: &mut S) -> Result<(), S::Error> { e.emit_map(self.len(), |e| { let mut i = 0; for (key, val) in self { e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?; i += 1; } Ok(()) }) } } impl Decodable for BTreeMap where K: Decodable + PartialEq + Ord, V: Decodable, { fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let mut map = BTreeMap::new(); for i in 0..len { let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?; let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?; map.insert(key, val); } Ok(map) }) } } impl Encodable for BTreeSet where T: Encodable + PartialEq + Ord, { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { let mut i = 0; for e in self { s.emit_seq_elt(i, |s| e.encode(s))?; i += 1; } Ok(()) }) } } impl Decodable for BTreeSet where T: Decodable + PartialEq + Ord, { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let mut set = BTreeSet::new(); for i in 0..len { set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?); } Ok(set) }) } } impl Encodable for HashMap where K: Encodable + Eq, V: Encodable, S: BuildHasher, { fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { let mut i = 0; for (key, val) in self { e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?; i += 1; } Ok(()) }) } } impl Decodable for HashMap where K: Decodable + Hash + Eq, V: Decodable, S: BuildHasher + Default, { fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let state = Default::default(); let mut map = HashMap::with_capacity_and_hasher(len, state); for i in 0..len { let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?; let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?; map.insert(key, val); } Ok(map) }) } } impl Encodable for HashSet where T: Encodable + Eq, S: BuildHasher, { fn encode(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { let mut i = 0; for e in self { s.emit_seq_elt(i, |s| e.encode(s))?; i += 1; } Ok(()) }) } } impl Decodable for HashSet where T: Decodable + Hash + Eq, S: BuildHasher + Default, { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let state = Default::default(); let mut set = HashSet::with_capacity_and_hasher(len, state); for i in 0..len { set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?); } Ok(set) }) } } impl Encodable for indexmap::IndexMap where K: Encodable + Hash + Eq, V: Encodable, S: BuildHasher, { fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { let mut i = 0; for (key, val) in self { e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?; i += 1; } Ok(()) }) } } impl Decodable for indexmap::IndexMap where K: Decodable + Hash + Eq, V: Decodable, S: BuildHasher + Default, { fn decode(d: &mut D) -> Result, D::Error> { d.read_map(|d, len| { let state = Default::default(); let mut map = indexmap::IndexMap::with_capacity_and_hasher(len, state); for i in 0..len { let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?; let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?; map.insert(key, val); } Ok(map) }) } } impl Encodable for indexmap::IndexSet where T: Encodable + Hash + Eq, S: BuildHasher, { fn encode(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { let mut i = 0; for e in self { s.emit_seq_elt(i, |s| e.encode(s))?; i += 1; } Ok(()) }) } } impl Decodable for indexmap::IndexSet where T: Decodable + Hash + Eq, S: BuildHasher + Default, { fn decode(d: &mut D) -> Result, D::Error> { d.read_seq(|d, len| { let state = Default::default(); let mut set = indexmap::IndexSet::with_capacity_and_hasher(len, state); for i in 0..len { set.insert(d.read_seq_elt(i, |d| Decodable::decode(d))?); } Ok(set) }) } } impl Encodable for Rc<[T]> { fn encode(&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 Decodable for Rc<[T]> { fn decode(d: &mut D) -> Result, 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()) }) } } impl Encodable for Arc<[T]> { fn encode(&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 Decodable for Arc<[T]> { fn decode(d: &mut D) -> Result, 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()) }) } }