esvm_rlp/lib.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//! Recursive Length Prefix serialization crate.
10//!
11//! Allows encoding, decoding, and view onto rlp-slice
12//!
13//!# What should you use when?
14//!
15//!### Use `encode` function when:
16//! * You want to encode something inline.
17//! * You do not work on big set of data.
18//! * You want to encode whole data structure at once.
19//!
20//!### Use `decode` function when:
21//! * You want to decode something inline.
22//! * You do not work on big set of data.
23//! * You want to decode whole rlp at once.
24//!
25//!### Use `RlpStream` when:
26//! * You want to encode something in portions.
27//! * You encode a big set of data.
28//!
29//!### Use `Rlp` when:
30//! * You are working on trusted data (not corrupted).
31//! * You want to get view onto rlp-slice.
32//! * You don't want to decode whole rlp at once.
33//!
34//!### Use `UntrustedRlp` when:
35//! * You are working on untrusted data (~corrupted).
36//! * You need to handle data corruption errors.
37//! * You are working on input data.
38//! * You want to get view onto rlp-slice.
39//! * You don't want to decode whole rlp at once.
40
41extern crate byteorder;
42extern crate elastic_array;
43extern crate rustc_serialize;
44
45#[macro_use]
46extern crate lazy_static;
47
48mod traits;
49mod error;
50mod rlpin;
51mod untrusted_rlp;
52mod stream;
53mod compression;
54mod common;
55mod impls;
56
57use std::borrow::Borrow;
58use elastic_array::ElasticArray1024;
59
60pub use error::DecoderError;
61pub use traits::{Decodable, Encodable, Compressible};
62pub use untrusted_rlp::{UntrustedRlp, UntrustedRlpIterator, PayloadInfo, Prototype};
63pub use rlpin::{Rlp, RlpIterator};
64pub use stream::RlpStream;
65pub use compression::RlpType;
66
67/// The RLP encoded empty data (used to mean "null value").
68pub const NULL_RLP: [u8; 1] = [0x80; 1];
69/// The RLP encoded empty list.
70pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1];
71
72/// Shortcut function to decode trusted rlp
73///
74/// ```rust
75/// extern crate rlp;
76///
77/// fn main () {
78/// let data = vec![0x83, b'c', b'a', b't'];
79/// let animal: String = rlp::decode(&data);
80/// assert_eq!(animal, "cat".to_owned());
81/// }
82/// ```
83pub fn decode<T>(bytes: &[u8]) -> T where T: Decodable {
84 let rlp = Rlp::new(bytes);
85 rlp.as_val()
86}
87
88pub fn decode_list<T>(bytes: &[u8]) -> Vec<T> where T: Decodable {
89 let rlp = Rlp::new(bytes);
90 rlp.as_list()
91}
92
93/// Shortcut function to encode structure into rlp.
94///
95/// ```rust
96/// extern crate rlp;
97///
98/// fn main () {
99/// let animal = "cat";
100/// let out = rlp::encode(&animal).to_vec();
101/// assert_eq!(out, vec![0x83, b'c', b'a', b't']);
102/// }
103/// ```
104pub fn encode<E>(object: &E) -> ElasticArray1024<u8> where E: Encodable {
105 let mut stream = RlpStream::new();
106 stream.append(object);
107 stream.drain()
108}
109
110pub fn encode_list<E, K>(object: &[K]) -> ElasticArray1024<u8> where E: Encodable, K: Borrow<E> {
111 let mut stream = RlpStream::new();
112 stream.append_list(object);
113 stream.drain()
114}