serde_scale/lib.rs
1// Copyright (C) 2020 Stephane Raux. Distributed under the zlib license.
2
3//! # Overview
4//! - [📦 crates.io](https://crates.io/crates/serde-scale)
5//! - [📖 Documentation](https://docs.rs/serde-scale)
6//! - [âš– zlib license](https://opensource.org/licenses/Zlib)
7//!
8//! Serializer and deserializer for the [SCALE encoding](https://substrate.dev/docs/en/knowledgebase/advanced/codec)
9//! based on [`serde`](https://docs.rs/serde).
10//!
11//! # Example
12//! ```rust
13//! # #[cfg(feature = "alloc")] {
14//! use serde::{Deserialize, Serialize};
15//!
16//! #[derive(Debug, Deserialize, PartialEq, Serialize)]
17//! struct Point {
18//! x: i8,
19//! y: i8,
20//! }
21//!
22//! let point = Point { x: 3, y: 4 };
23//! let deserialized = serde_scale::from_slice(&serde_scale::to_vec(&point).unwrap()).unwrap();
24//! assert_eq!(point, deserialized);
25//! # }
26//! ```
27//!
28//! # Conformance
29//! `Option<bool>` is serialized as a single byte according to the SCALE encoding.
30//!
31//! # Features
32//! `no_std` is supported by disabling default features.
33//!
34//! - `std`: Support for `std`. It is enabled by default.
35//! - `alloc`: Support for the `alloc` crate.
36//!
37//! 🔖 Features enabled in build dependencies and proc-macros are also enabled for normal
38//! dependencies, which may cause `serde` to have its `std` feature on when it is not desired.
39//! Nightly cargo prevents this from happening with
40//! [`-Z features=host_dep`](https://github.com/rust-lang/cargo/issues/7915#issuecomment-683294870)
41//! or the following in `.cargo/config`:
42//!
43//! ```toml
44//! [unstable]
45//! features = ["host_dep"]
46//! ```
47//!
48//! For example, this issue arises when depending on `parity-scale-codec-derive`.
49//!
50//! # Test
51//! Most tests live in the `serde-scale-tests` crate (part of the workspace) in order to avoid
52//! dependencies enabling `serde` features.
53//!
54//! ```sh
55//! cargo test --workspace
56//! ```
57//!
58//! # Contribute
59//! All contributions shall be licensed under the [zlib license](https://opensource.org/licenses/Zlib).
60//!
61//! # Related projects
62//! [parity-scale-codec](https://crates.io/crates/parity-scale-codec): Reference Rust implementation
63
64#![deny(warnings)]
65#![cfg_attr(not(feature = "std"), no_std)]
66
67#[cfg(feature = "alloc")]
68extern crate alloc;
69
70mod de;
71mod err;
72mod read;
73mod ser;
74mod write;
75
76pub use de::{from_slice, Deserializer};
77pub use err::{Error, OtherError};
78pub use read::{Bytes, EndOfInput, Read};
79pub use ser::Serializer;
80pub use write::Write;
81
82#[cfg(feature = "alloc")]
83pub use ser::to_vec;