From be1d208c361eb9fad35e2e994ec11f72fec4157d Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 30 Nov 2015 13:06:51 +0100 Subject: [PATCH] rlp size function --- src/rlp.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/rlp.rs b/src/rlp.rs index 8454600b7..b9f58e78f 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -170,6 +170,24 @@ impl<'a> Rlp<'a> { self.rlp.item_count() } + /// Returns the number of bytes in the data, or zero if it isn't data. + /// + /// ```rust + /// extern crate ethcore_util as util; + /// use util::rlp::*; + /// + /// fn main () { + /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; + /// let rlp = Rlp::new(&data); + /// assert_eq!(rlp.size(), 0); + /// let view = rlp.at(1); + /// assert_eq!(view.size(), 3); + /// } + /// ``` + pub fn size(&self) -> usize { + self.rlp.size() + } + /// Get view onto rlp-slice at index. /// /// Caches offset to given index, so access to successive @@ -336,6 +354,27 @@ impl<'a> UntrustedRlp<'a> { } } + /// Returns the number of bytes in the data, or zero if it isn't data. + /// + /// ```rust + /// extern crate ethcore_util as util; + /// use util::rlp::*; + /// + /// fn main () { + /// let data = vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']; + /// let rlp = UntrustedRlp::new(&data); + /// assert_eq!(rlp.size(), 0); + /// let view = rlp.at(1).unwrap(); + /// assert_eq!(view.size(), 3); + /// } + /// ``` + pub fn size(&self) -> usize { + match self.is_data() { + true => Self::item_info(self.bytes).unwrap().value_len, + false => 0 + } + } + /// Get view onto rlp-slice at index /// /// Caches offset to given index, so access to successive