rlp encodable and decodable for options
This commit is contained in:
parent
0bf6d03c0a
commit
0e343d078c
@ -74,8 +74,7 @@ pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
|
|||||||
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
/// assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn encode<E>(object: &E) -> Vec<u8> where E: Encodable
|
pub fn encode<E>(object: &E) -> Vec<u8> where E: Encodable {
|
||||||
{
|
|
||||||
let mut stream = RlpStream::new();
|
let mut stream = RlpStream::new();
|
||||||
stream.append(object);
|
stream.append(object);
|
||||||
stream.out()
|
stream.out()
|
||||||
|
@ -207,13 +207,13 @@ impl Encoder for BasicEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Encodable for T where T: ToBytes {
|
impl<T> Encodable for T where T: ToBytes {
|
||||||
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||||
encoder.emit_value(&self.to_bytes())
|
encoder.emit_value(&self.to_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a {
|
impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a {
|
||||||
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||||
encoder.emit_list(|e| {
|
encoder.emit_list(|e| {
|
||||||
// insert all list elements
|
// insert all list elements
|
||||||
for el in self.iter() {
|
for el in self.iter() {
|
||||||
@ -224,7 +224,7 @@ impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Encodable for Vec<T> where T: Encodable {
|
impl<T> Encodable for Vec<T> where T: Encodable {
|
||||||
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||||
let r: &[T] = self.as_ref();
|
let r: &[T] = self.as_ref();
|
||||||
r.encode(encoder)
|
r.encode(encoder)
|
||||||
}
|
}
|
||||||
@ -233,7 +233,7 @@ impl<T> Encodable for Vec<T> where T: Encodable {
|
|||||||
/// lets treat bytes differently than other lists
|
/// lets treat bytes differently than other lists
|
||||||
/// they are a single value
|
/// they are a single value
|
||||||
impl<'a> Encodable for &'a [u8] {
|
impl<'a> Encodable for &'a [u8] {
|
||||||
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||||
encoder.emit_value(self)
|
encoder.emit_value(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -241,7 +241,16 @@ impl<'a> Encodable for &'a [u8] {
|
|||||||
/// lets treat bytes differently than other lists
|
/// lets treat bytes differently than other lists
|
||||||
/// they are a single value
|
/// they are a single value
|
||||||
impl Encodable for Vec<u8> {
|
impl Encodable for Vec<u8> {
|
||||||
fn encode<E>(&self, encoder: &mut E) -> () where E: Encoder {
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||||
encoder.emit_value(self)
|
encoder.emit_value(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Encodable for Option<T> where T: Encodable {
|
||||||
|
fn encode<E>(&self, encoder: &mut E) where E: Encoder {
|
||||||
|
match *self {
|
||||||
|
Some(ref x) => x.encode(encoder),
|
||||||
|
None => encoder.emit_value(&[])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -340,3 +340,15 @@ impl Decodable for Vec<u8> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Decodable for Option<T> where T: Decodable {
|
||||||
|
fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
|
||||||
|
decoder.read_value(| bytes | {
|
||||||
|
let res = match bytes.len() {
|
||||||
|
0 => None,
|
||||||
|
_ => Some(try!(T::decode(decoder)))
|
||||||
|
};
|
||||||
|
Ok(res)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user