rlp/
traits.rs

1// Copyright 2015-2017 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 elastic_array::ElasticArray1024;
11use {DecoderError, UntrustedRlp, RlpStream};
12
13/// RLP decodable trait
14pub trait Decodable: Sized {
15	/// Decode a value from RLP bytes
16	fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError>;
17}
18
19/// Structure encodable to RLP
20pub trait Encodable {
21	/// Append a value to the stream
22	fn rlp_append(&self, s: &mut RlpStream);
23
24	/// Get rlp-encoded bytes for this instance
25	fn rlp_bytes(&self) -> ElasticArray1024<u8> {
26		let mut s = RlpStream::new();
27		self.rlp_append(&mut s);
28		s.drain()
29	}
30}
31
32/// Trait for compressing and decompressing RLP by replacement of common terms.
33pub trait Compressible: Sized {
34	/// Indicates the origin of RLP to be compressed.
35	type DataType;
36
37	/// Compress given RLP type using appropriate methods.
38	fn compress(&self, t: Self::DataType) -> ElasticArray1024<u8>;
39	/// Decompress given RLP type using appropriate methods.
40	fn decompress(&self, t: Self::DataType) -> ElasticArray1024<u8>;
41}