rlp/lib.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//! 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 need to handle data corruption errors.
31//! * You are working on input data.
32//! * You want to get view onto rlp-slice.
33//! * You don't want to decode whole rlp at once.
34
35#![cfg_attr(not(feature = "std"), no_std)]
36
37#[cfg(not(feature = "std"))]
38extern crate alloc;
39
40mod error;
41mod impls;
42mod rlpin;
43mod stream;
44mod traits;
45
46#[cfg(not(feature = "std"))]
47use alloc::vec::Vec;
48use bytes::BytesMut;
49use core::borrow::Borrow;
50
51#[cfg(feature = "derive")]
52pub use rlp_derive::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper};
53
54pub use self::{
55 error::DecoderError,
56 rlpin::{PayloadInfo, Prototype, Rlp, RlpIterator},
57 stream::RlpStream,
58 traits::{Decodable, Encodable},
59};
60
61/// The RLP encoded empty data (used to mean "null value").
62pub const NULL_RLP: [u8; 1] = [0x80; 1];
63/// The RLP encoded empty list.
64pub const EMPTY_LIST_RLP: [u8; 1] = [0xC0; 1];
65
66/// Shortcut function to decode trusted rlp
67///
68/// ```
69/// let data = vec![0x83, b'c', b'a', b't'];
70/// let animal: String = rlp::decode(&data).expect("could not decode");
71/// assert_eq!(animal, "cat".to_owned());
72/// ```
73pub fn decode<T>(bytes: &[u8]) -> Result<T, DecoderError>
74where
75 T: Decodable,
76{
77 let rlp = Rlp::new(bytes);
78 rlp.as_val()
79}
80
81pub fn decode_list<T>(bytes: &[u8]) -> Vec<T>
82where
83 T: Decodable,
84{
85 let rlp = Rlp::new(bytes);
86 rlp.as_list().expect("trusted rlp should be valid")
87}
88
89/// Shortcut function to encode structure into rlp.
90///
91/// ```
92/// let animal = "cat";
93/// let out = rlp::encode(&animal);
94/// assert_eq!(out, vec![0x83, b'c', b'a', b't']);
95/// ```
96pub fn encode<E>(object: &E) -> BytesMut
97where
98 E: Encodable,
99{
100 let mut stream = RlpStream::new();
101 stream.append(object);
102 stream.out()
103}
104
105pub fn encode_list<E, K>(object: &[K]) -> BytesMut
106where
107 E: Encodable,
108 K: Borrow<E>,
109{
110 let mut stream = RlpStream::new();
111 stream.append_list(object);
112 stream.out()
113}