From 6660b3d65feaff95c886ec09ffed5791e8a88e4b Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 30 Nov 2015 16:14:03 +0100 Subject: [PATCH 1/2] replaced usages of append(&"") to append_empty_data() --- src/triehash.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/triehash.rs b/src/triehash.rs index bc8dedf76..e26cc255f 100644 --- a/src/triehash.rs +++ b/src/triehash.rs @@ -138,9 +138,9 @@ fn as_nibbles(bytes: &[u8]) -> Vec { fn hash256rlp(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStream) { let inlen = input.len(); - // in case of empty slice, just append null + // in case of empty slice, just append empty data if inlen == 0 { - stream.append(&""); + stream.append_empty_data(); return; } @@ -196,7 +196,7 @@ fn hash256rlp(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStre // if at least 1 successive element has the same nibble // append their suffixes match len { - 0 => { stream.append(&""); }, + 0 => { stream.append_empty_data(); }, _ => hash256aux(&input[begin..(begin + len)], pre_len + 1, stream) } begin += len; @@ -205,7 +205,7 @@ fn hash256rlp(input: &[(Vec, Vec)], pre_len: usize, stream: &mut RlpStre // if fist key len is equal prefix, append it's value match pre_len == key.len() { true => { stream.append(&value); }, - false => { stream.append(&""); } + false => { stream.append_empty_data(); } }; } From 84b872819ed68e044e25e763365e4d359388b97b Mon Sep 17 00:00:00 2001 From: debris Date: Mon, 30 Nov 2015 17:23:52 +0100 Subject: [PATCH 2/2] rlp lifetimes --- src/rlp.rs | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/rlp.rs b/src/rlp.rs index e8562828a..b74651799 100644 --- a/src/rlp.rs +++ b/src/rlp.rs @@ -132,7 +132,7 @@ pub enum Prototype { List(usize), } -impl<'a> Rlp<'a> { +impl<'a, 'view> Rlp<'a> where 'a: 'view { /// Create a new instance of `Rlp` pub fn new(bytes: &'a [u8]) -> Rlp<'a> { Rlp { @@ -162,12 +162,11 @@ impl<'a> Rlp<'a> { /// 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); - /// let view = rlp.at(1); - /// let dog = view.data(); + /// let dog = rlp.at(1).data(); /// assert_eq!(dog, &[0x83, b'd', b'o', b'g']); /// } /// ``` - pub fn data(&self) -> &[u8] { + pub fn data(&'view self) -> &'a [u8] { self.rlp.data() } @@ -223,7 +222,7 @@ impl<'a> Rlp<'a> { /// assert_eq!(dog, "dog".to_string()); /// } /// ``` - pub fn at(&self, index: usize) -> Rlp<'a> { + pub fn at(&'view self, index: usize) -> Rlp<'a> { From::from(self.rlp.at(index).unwrap()) } @@ -325,7 +324,7 @@ impl<'a> Rlp<'a> { } } -impl<'a> UntrustedRlp<'a> { +impl<'a, 'view> UntrustedRlp<'a> where 'a: 'view { /// returns new instance of `UntrustedRlp` pub fn new(bytes: &'a [u8]) -> UntrustedRlp<'a> { UntrustedRlp { @@ -343,12 +342,11 @@ impl<'a> UntrustedRlp<'a> { /// 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); - /// let view = rlp.at(1).unwrap(); - /// let dog = view.data(); + /// let dog = rlp.at(1).unwrap().data(); /// assert_eq!(dog, &[0x83, b'd', b'o', b'g']); /// } /// ``` - pub fn data(&self) -> &[u8] { + pub fn data(&'view self) -> &'a [u8] { self.bytes } @@ -410,7 +408,7 @@ impl<'a> UntrustedRlp<'a> { /// assert_eq!(dog, "dog".to_string()); /// } /// ``` - pub fn at(&self, index: usize) -> Result, DecoderError> { + pub fn at(&'view self, index: usize) -> Result, DecoderError> { if !self.is_list() { return Err(DecoderError::RlpExpectedToBeList); } @@ -1452,4 +1450,31 @@ mod tests { vec![0xc5, 0xc4, 0x83, b'c', b'a', b't'])]; run_decode_tests(tests); } + + #[test] + fn test_view() { + struct View<'a> { + bytes: &'a [u8] + } + + impl <'a, 'view> View<'a> where 'a: 'view { + fn new(bytes: &'a [u8]) -> View<'a> { + View { + bytes: bytes + } + } + + fn offset(&'view self, len: usize) -> View<'a> { + View::new(&self.bytes[len..]) + } + + fn data(&'view self) -> &'a [u8] { + self.bytes + } + } + + let data = vec![0, 1, 2, 3]; + let view = View::new(&data); + let _data_slice = view.offset(1).data(); + } }