parity_scale_codec/
lib.rs

1// Copyright 2017-2021 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![doc = include_str!("../README.md")]
16#![warn(missing_docs)]
17#![cfg_attr(not(feature = "std"), no_std)]
18
19#[cfg(not(feature = "std"))]
20#[macro_use]
21#[doc(hidden)]
22pub extern crate alloc;
23
24#[cfg(feature = "derive")]
25#[allow(unused_imports)]
26#[macro_use]
27extern crate parity_scale_codec_derive;
28
29#[cfg(all(feature = "std", test))]
30#[macro_use]
31extern crate serde_derive;
32
33#[cfg(feature = "derive")]
34pub use parity_scale_codec_derive::*;
35
36#[cfg(feature = "std")]
37#[doc(hidden)]
38pub mod alloc {
39	pub use std::boxed;
40	pub use std::vec;
41	pub use std::string;
42	pub use std::borrow;
43	pub use std::collections;
44	pub use std::sync;
45	pub use std::rc;
46	pub use std::alloc;
47}
48
49mod codec;
50mod compact;
51mod joiner;
52mod keyedvec;
53#[cfg(feature = "bit-vec")]
54mod bit_vec;
55#[cfg(feature = "generic-array")]
56mod generic_array;
57mod decode_all;
58mod decode_finished;
59mod depth_limit;
60mod encode_append;
61mod encode_like;
62mod error;
63#[cfg(feature = "max-encoded-len")]
64mod max_encoded_len;
65#[cfg(feature = "max-encoded-len")]
66mod const_encoded_len;
67
68pub use self::error::Error;
69pub use self::codec::{
70	Input, Output, Decode, Encode, Codec, EncodeAsRef, WrapperTypeEncode, WrapperTypeDecode,
71	OptionBool, DecodeLength, FullCodec, FullEncode, decode_vec_with_len,
72};
73#[cfg(feature = "std")]
74pub use self::codec::IoReader;
75pub use self::compact::{Compact, HasCompact, CompactAs, CompactLen, CompactRef};
76pub use self::joiner::Joiner;
77pub use self::keyedvec::KeyedVec;
78pub use self::decode_all::DecodeAll;
79pub use self::decode_finished::DecodeFinished;
80pub use self::depth_limit::DecodeLimit;
81pub use self::encode_append::EncodeAppend;
82pub use self::encode_like::{EncodeLike, Ref};
83#[cfg(feature = "max-encoded-len")]
84pub use max_encoded_len::MaxEncodedLen;
85#[cfg(feature = "max-encoded-len")]
86pub use const_encoded_len::ConstEncodedLen;
87
88/// Derive macro for [`MaxEncodedLen`][max_encoded_len::MaxEncodedLen].
89///
90/// # Examples
91///
92/// ```
93/// # use parity_scale_codec::{Encode, MaxEncodedLen};
94/// #[derive(Encode, MaxEncodedLen)]
95/// struct Example;
96/// ```
97///
98/// ```
99/// # use parity_scale_codec::{Encode, MaxEncodedLen};
100/// #[derive(Encode, MaxEncodedLen)]
101/// struct TupleStruct(u8, u32);
102///
103/// assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len());
104/// ```
105///
106/// ```
107/// # use parity_scale_codec::{Encode, MaxEncodedLen};
108/// #[derive(Encode, MaxEncodedLen)]
109/// enum GenericEnum<T> {
110///     A,
111///     B(T),
112/// }
113///
114/// assert_eq!(GenericEnum::<u8>::max_encoded_len(), u8::max_encoded_len() + u8::max_encoded_len());
115/// assert_eq!(GenericEnum::<u128>::max_encoded_len(), u8::max_encoded_len() + u128::max_encoded_len());
116/// ```
117///
118/// # Within other macros
119///
120/// Sometimes the `MaxEncodedLen` trait and macro are used within another macro, and it can't be
121/// guaranteed that the `parity_scale_codec` module is available at the call site. In that case, the
122/// macro should reexport the `parity_scale_codec` module and specify the path to the reexport:
123///
124/// ```ignore
125/// pub use parity_scale_codec as codec;
126///
127/// #[derive(Encode, MaxEncodedLen)]
128/// #[codec(crate = $crate::codec)]
129/// struct Example;
130/// ```
131#[cfg(all(feature = "derive", feature = "max-encoded-len"))]
132pub use parity_scale_codec_derive::MaxEncodedLen;
133
134#[cfg(feature = "bytes")]
135pub use self::codec::decode_from_bytes;