diff --git a/src/rlp/mod.rs b/src/rlp/mod.rs index cb36426c9..981e5f973 100644 --- a/src/rlp/mod.rs +++ b/src/rlp/mod.rs @@ -74,8 +74,7 @@ pub fn decode(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']); /// } /// ``` -pub fn encode(object: &E) -> Vec where E: Encodable -{ +pub fn encode(object: &E) -> Vec where E: Encodable { let mut stream = RlpStream::new(); stream.append(object); stream.out() diff --git a/src/rlp/rlpstream.rs b/src/rlp/rlpstream.rs index 557c4af25..301a39053 100644 --- a/src/rlp/rlpstream.rs +++ b/src/rlp/rlpstream.rs @@ -207,13 +207,13 @@ impl Encoder for BasicEncoder { } impl Encodable for T where T: ToBytes { - fn encode(&self, encoder: &mut E) -> () where E: Encoder { + fn encode(&self, encoder: &mut E) where E: Encoder { encoder.emit_value(&self.to_bytes()) } } impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a { - fn encode(&self, encoder: &mut E) -> () where E: Encoder { + fn encode(&self, encoder: &mut E) where E: Encoder { encoder.emit_list(|e| { // insert all list elements for el in self.iter() { @@ -224,7 +224,7 @@ impl<'a, T> Encodable for &'a [T] where T: Encodable + 'a { } impl Encodable for Vec where T: Encodable { - fn encode(&self, encoder: &mut E) -> () where E: Encoder { + fn encode(&self, encoder: &mut E) where E: Encoder { let r: &[T] = self.as_ref(); r.encode(encoder) } @@ -233,7 +233,7 @@ impl Encodable for Vec where T: Encodable { /// lets treat bytes differently than other lists /// they are a single value impl<'a> Encodable for &'a [u8] { - fn encode(&self, encoder: &mut E) -> () where E: Encoder { + fn encode(&self, encoder: &mut E) where E: Encoder { encoder.emit_value(self) } } @@ -241,7 +241,16 @@ impl<'a> Encodable for &'a [u8] { /// lets treat bytes differently than other lists /// they are a single value impl Encodable for Vec { - fn encode(&self, encoder: &mut E) -> () where E: Encoder { + fn encode(&self, encoder: &mut E) where E: Encoder { encoder.emit_value(self) } } + +impl Encodable for Option where T: Encodable { + fn encode(&self, encoder: &mut E) where E: Encoder { + match *self { + Some(ref x) => x.encode(encoder), + None => encoder.emit_value(&[]) + } + } +} diff --git a/src/rlp/untrusted_rlp.rs b/src/rlp/untrusted_rlp.rs index eae253eca..ec68fce99 100644 --- a/src/rlp/untrusted_rlp.rs +++ b/src/rlp/untrusted_rlp.rs @@ -340,3 +340,15 @@ impl Decodable for Vec { }) } } + +impl Decodable for Option where T: Decodable { + fn decode(decoder: &D) -> Result where D: Decoder { + decoder.read_value(| bytes | { + let res = match bytes.len() { + 0 => None, + _ => Some(try!(T::decode(decoder))) + }; + Ok(res) + }) + } +}