tetsy_rlp/traits.rs
1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Common RLP traits
10use bytes::BytesMut;
11
12use crate::{error::DecoderError, rlpin::Rlp, stream::RlpStream};
13
14/// RLP decodable trait
15pub trait Decodable: Sized {
16 /// Decode a value from RLP bytes
17 fn decode(rlp: &Rlp) -> Result<Self, DecoderError>;
18}
19
20/// Structure encodable to RLP
21pub trait Encodable {
22 /// Append a value to the stream
23 fn rlp_append(&self, s: &mut RlpStream);
24
25 /// Get rlp-encoded bytes for this instance
26 fn rlp_bytes(&self) -> BytesMut {
27 let mut s = RlpStream::new();
28 self.rlp_append(&mut s);
29 s.out()
30 }
31}