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