From 20cadef4f085ec890f8758734298055887733063 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 25 Nov 2015 22:29:21 +0100 Subject: [PATCH] RlpStream uses BasicEncoder --- src/rlp.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/rlp.rs b/src/rlp.rs index bd5561635..6161e5cca 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -193,7 +193,7 @@ impl <'a> Iterator for RlpIterator<'a> { pub struct RlpStream { len: usize, max_len: usize, - bytes: Vec + encoder: BasicEncoder } impl RlpStream { @@ -208,14 +208,14 @@ impl RlpStream { RlpStream { len: 0, max_len: max_len, - bytes: vec![] + encoder: BasicEncoder::new() } } /// apends value to the end of stream, chainable pub fn append<'a, E>(&'a mut self, object: &E) -> &'a mut RlpStream where E: Encodable { // encode given value and add it at the end of the stream - self.bytes.extend(encode(object)); + object.encode(&mut self.encoder); self.len += 1; // if list is finished, prepend the length @@ -235,30 +235,20 @@ impl RlpStream { /// streams out encoded bytes pub fn out(self) -> Result, EncoderError> { match self.is_finished() { - true => Ok(self.bytes), + true => Ok(self.encoder.out()), false => Err(EncoderError::StreamIsUnfinished) } } /// prepend the length of the bytes to the beginning of the vector fn prepend_the_length(&mut self) -> () { - let mut v = match self.bytes.len() { - len @ 0...55 => vec![0xc0u8 + len as u8], - len => { - let mut res = vec![0x7fu8 + len.to_bytes_len() as u8]; - let mut b = len.to_bytes(); - res.append(&mut b); - res - } - }; - - v.append(&mut self.bytes); - self.bytes = v; + let len = self.encoder.bytes.len(); + self.encoder.insert_list_len_at_pos(len, 0); } } /// shortcut function to encode a `T: Encodable` into a Rlp `Vec` -fn encode(object: &E) -> Vec where E: Encodable { +pub fn encode(object: &E) -> Vec where E: Encodable { let mut encoder = BasicEncoder::new(); object.encode(&mut encoder); encoder.out()