From 21f87cfb0f92754d2833444e50d853fa86e77007 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 2 Dec 2015 13:58:29 +0100 Subject: [PATCH] ItemInfo -> PayloadInfo --- src/rlp.rs | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/rlp.rs b/src/rlp.rs index c023b1dfb..1648f8807 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -65,16 +65,16 @@ impl OffsetCache { } } -/// stores basic information about item -struct ItemInfo { - prefix_len: usize, - value_len: usize, +/// Stores basic information about item +pub struct PayloadInfo { + pub header_len: usize, + pub value_len: usize, } -impl ItemInfo { - fn new(prefix_len: usize, value_len: usize) -> ItemInfo { - ItemInfo { - prefix_len: prefix_len, +impl PayloadInfo { + fn new(header_len: usize, value_len: usize) -> PayloadInfo { + PayloadInfo { + header_len: header_len, value_len: value_len, } } @@ -356,7 +356,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { pub fn data(&'view self) -> &'a [u8] { let ii = Self::item_info(self.bytes).unwrap(); - &self.bytes[ii.prefix_len..(ii.prefix_len + ii.value_len)] + &self.bytes[ii.header_len..(ii.header_len + ii.value_len)] } /// Returns number of rlp items. @@ -401,6 +401,8 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { } } + //pub fn payload_offset(&self) -> + /// Get view onto rlp-slice at index /// /// Caches offset to given index, so access to successive @@ -438,7 +440,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { // construct new rlp let found = try!(UntrustedRlp::item_info(bytes)); - Ok(UntrustedRlp::new(&bytes[0..found.prefix_len + found.value_len])) + Ok(UntrustedRlp::new(&bytes[0..found.header_len + found.value_len])) } /// No value @@ -553,7 +555,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { /// consumes first found prefix fn consume_list_prefix(&self) -> Result<&'a [u8], DecoderError> { let item = try!(UntrustedRlp::item_info(self.bytes)); - let bytes = try!(UntrustedRlp::consume(self.bytes, item.prefix_len)); + let bytes = try!(UntrustedRlp::consume(self.bytes, item.header_len)); Ok(bytes) } @@ -562,7 +564,7 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { let mut result = bytes; for _ in 0..items { let i = try!(UntrustedRlp::item_info(result)); - result = try!(UntrustedRlp::consume(result, (i.prefix_len + i.value_len))); + result = try!(UntrustedRlp::consume(result, (i.header_len + i.value_len))); } Ok(result) } @@ -570,28 +572,29 @@ impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { /// return first item info /// /// TODO: move this to decoder (?) - fn item_info(bytes: &[u8]) -> Result { + fn item_info(bytes: &[u8]) -> Result { let item = match bytes.first().map(|&x| x) { None => return Err(DecoderError::RlpIsTooShort), - Some(0...0x7f) => ItemInfo::new(0, 1), - Some(l @ 0x80...0xb7) => ItemInfo::new(1, l as usize - 0x80), + Some(0...0x7f) => PayloadInfo::new(0, 1), + Some(l @ 0x80...0xb7) => PayloadInfo::new(1, l as usize - 0x80), Some(l @ 0xb8...0xbf) => { let len_of_len = l as usize - 0xb7; - let prefix_len = 1 + len_of_len; - let value_len = try!(usize::from_bytes(&bytes[1..prefix_len])); - ItemInfo::new(prefix_len, value_len) + let header_len = 1 + len_of_len; + let value_len = try!(usize::from_bytes(&bytes[1..header_len])); + PayloadInfo::new(header_len, value_len) } - Some(l @ 0xc0...0xf7) => ItemInfo::new(1, l as usize - 0xc0), + Some(l @ 0xc0...0xf7) => PayloadInfo::new(1, l as usize - 0xc0), Some(l @ 0xf8...0xff) => { let len_of_len = l as usize - 0xf7; - let prefix_len = 1 + len_of_len; - let value_len = try!(usize::from_bytes(&bytes[1..prefix_len])); - ItemInfo::new(prefix_len, value_len) + let header_len = 1 + len_of_len; + let value_len = try!(usize::from_bytes(&bytes[1..header_len])); + PayloadInfo::new(header_len, value_len) } + // we cant reach this place, but rust requires _ to be implemented _ => return Err(DecoderError::BadRlp), }; - match item.prefix_len + item.value_len <= bytes.len() { + match item.header_len + item.value_len <= bytes.len() { true => Ok(item), false => Err(DecoderError::RlpIsTooShort), }