From 4a22936898b5b5746ba88ef3163281bc40cba88b Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 28 Oct 2022 11:54:19 +0200 Subject: [PATCH] feat(rlp): add encode for &str (#147) * feat(rlp): add encode for &str * add test --- crates/common/rlp/src/encode.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/common/rlp/src/encode.rs b/crates/common/rlp/src/encode.rs index a13feb00f1..20839db284 100644 --- a/crates/common/rlp/src/encode.rs +++ b/crates/common/rlp/src/encode.rs @@ -291,6 +291,16 @@ mod alloc_support { } } } + +impl Encodable for &str { + fn encode(&self, out: &mut dyn BufMut) { + self.as_bytes().encode(out); + } + fn length(&self) -> usize { + self.as_bytes().length() + } +} + slice_impl!(Bytes); slice_impl!(BytesMut); @@ -369,6 +379,13 @@ mod tests { out1 } + #[test] + fn rlp_str() { + assert_eq!(encoded("")[..], hex!("80")[..]); + assert_eq!(encoded("{")[..], hex!("7b")[..]); + assert_eq!(encoded("test str")[..], hex!("887465737420737472")[..]); + } + #[test] fn rlp_strings() { assert_eq!(encoded(hex!(""))[..], hex!("80")[..]);