feat(rlp): add encode for &str (#147)

* feat(rlp): add encode for &str

* add test
This commit is contained in:
Matthias Seitz
2022-10-28 11:54:19 +02:00
committed by GitHub
parent 30ef6bff21
commit 4a22936898

View File

@ -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!(Bytes);
slice_impl!(BytesMut); slice_impl!(BytesMut);
@ -369,6 +379,13 @@ mod tests {
out1 out1
} }
#[test]
fn rlp_str() {
assert_eq!(encoded("")[..], hex!("80")[..]);
assert_eq!(encoded("{")[..], hex!("7b")[..]);
assert_eq!(encoded("test str")[..], hex!("887465737420737472")[..]);
}
#[test] #[test]
fn rlp_strings() { fn rlp_strings() {
assert_eq!(encoded(hex!(""))[..], hex!("80")[..]); assert_eq!(encoded(hex!(""))[..], hex!("80")[..]);