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#![cfg_attr(all(test, nightly), feature(allocator_api))]
16#![cfg_attr(all(test, nightly), feature(btreemap_alloc))]
17#![doc = include_str!("../README.md")]
18#![warn(missing_docs)]
19#![cfg_attr(not(feature = "std"), no_std)]
20
21#[cfg(not(feature = "std"))]
22#[macro_use]
23#[doc(hidden)]
24pub extern crate alloc;
25
26#[cfg(feature = "derive")]
27#[allow(unused_imports)]
28#[macro_use]
29extern crate parity_scale_codec_derive;
30
31#[cfg(all(feature = "std", test))]
32#[macro_use]
33extern crate serde_derive;
34
35#[cfg(feature = "derive")]
36pub use parity_scale_codec_derive::*;
37
38#[cfg(feature = "std")]
39#[doc(hidden)]
40pub mod alloc {
41 pub use std::{alloc, borrow, boxed, collections, rc, string, sync, vec};
42}
43
44/// Private module to reexport items used by derive macros.
45// We don't feature gate this module with `derive` to avoid compilation error when
46// `parity-scale-codec-derive` is used on its own and this crate doesn't have the feature enabled.
47#[doc(hidden)]
48pub mod __private {
49 pub use const_format::concatcp;
50}
51
52#[cfg(feature = "bit-vec")]
53mod bit_vec;
54mod btree_utils;
55mod codec;
56mod compact;
57#[cfg(feature = "max-encoded-len")]
58mod const_encoded_len;
59mod counted_input;
60mod decode_all;
61mod decode_finished;
62mod depth_limit;
63mod encode_append;
64mod encode_like;
65mod error;
66#[cfg(feature = "generic-array")]
67mod generic_array;
68mod joiner;
69mod keyedvec;
70#[cfg(feature = "max-encoded-len")]
71mod max_encoded_len;
72mod mem_tracking;
73
74#[cfg(feature = "std")]
75pub use self::codec::IoReader;
76pub use self::{
77 codec::{
78 decode_vec_with_len, Codec, Decode, DecodeLength, Encode, EncodeAsRef, FullCodec,
79 FullEncode, Input, OptionBool, Output, WrapperTypeDecode, WrapperTypeEncode,
80 },
81 compact::{Compact, CompactAs, CompactLen, CompactRef, HasCompact},
82 counted_input::CountedInput,
83 decode_all::DecodeAll,
84 decode_finished::DecodeFinished,
85 depth_limit::DecodeLimit,
86 encode_append::EncodeAppend,
87 encode_like::{EncodeLike, Ref},
88 error::Error,
89 joiner::Joiner,
90 keyedvec::KeyedVec,
91 mem_tracking::{DecodeWithMemLimit, DecodeWithMemTracking, MemTrackingInput},
92};
93#[cfg(feature = "max-encoded-len")]
94pub use const_encoded_len::ConstEncodedLen;
95#[cfg(feature = "max-encoded-len")]
96pub use max_encoded_len::MaxEncodedLen;
97
98/// Derive macro for [`MaxEncodedLen`][max_encoded_len::MaxEncodedLen].
99///
100/// # Examples
101///
102/// ```
103/// # use parity_scale_codec::{Encode, MaxEncodedLen};
104/// #[derive(Encode, MaxEncodedLen)]
105/// struct Example;
106/// ```
107///
108/// ```
109/// # use parity_scale_codec::{Encode, MaxEncodedLen};
110/// #[derive(Encode, MaxEncodedLen)]
111/// struct TupleStruct(u8, u32);
112///
113/// assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len());
114/// ```
115///
116/// ```
117/// # use parity_scale_codec::{Encode, MaxEncodedLen};
118/// #[derive(Encode, MaxEncodedLen)]
119/// enum GenericEnum<T> {
120/// A,
121/// B(T),
122/// }
123///
124/// assert_eq!(GenericEnum::<u8>::max_encoded_len(), u8::max_encoded_len() + u8::max_encoded_len());
125/// assert_eq!(GenericEnum::<u128>::max_encoded_len(), u8::max_encoded_len() + u128::max_encoded_len());
126/// ```
127///
128/// # Within other macros
129///
130/// Sometimes the `MaxEncodedLen` trait and macro are used within another macro, and it can't
131/// be guaranteed that the `parity_scale_codec` module is available at the call site. In that
132/// case, the macro should reexport the `parity_scale_codec` module and specify the path to the
133/// reexport:
134///
135/// ```ignore
136/// pub use parity_scale_codec as codec;
137///
138/// #[derive(Encode, MaxEncodedLen)]
139/// #[codec(crate = $crate::codec)]
140/// struct Example;
141/// ```
142#[cfg(all(feature = "derive", feature = "max-encoded-len"))]
143pub use parity_scale_codec_derive::MaxEncodedLen;
144
145#[cfg(feature = "bytes")]
146pub use self::codec::decode_from_bytes;