Skip to content

Commit

Permalink
Add Encodable and Decodable impls for Arc<[T]>
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Dec 21, 2017
1 parent 30733b3 commit 84ce4f1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/libserialize/collection_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
})
}
}

0 comments on commit 84ce4f1

Please sign in to comment.