Skip to main content

float_pigment_consistent_bincode/
lib.rs

1#![no_std]
2#![allow(clippy::legacy_numeric_constants)]
3#![allow(clippy::multiple_bound_locations)]
4
5//! Bincode is a crate for encoding and decoding using a tiny binary
6//! serialization strategy.  Using it, you can easily go from having
7//! an object in memory, quickly serialize it to bytes, and then
8//! deserialize it back just as fast!
9//!
10//! ### Using Basic Functions
11//!
12//! ```edition2018
13//! fn test() {
14//!     // The object that we will serialize.
15//!     let target: Option<String>  = Some("hello world".to_string());
16//!
17//!     let encoded: Vec<u8> = float_pigment_consistent_bincode::serialize(&target).unwrap();
18//!     let decoded: Option<String> = float_pigment_consistent_bincode::deserialize(&encoded[..]).unwrap();
19//!     assert_eq!(target, decoded);
20//! }
21//! ```
22//!
23//! ### 128bit numbers
24//!
25//! Support for `i128` and `u128` is automatically enabled on Rust toolchains
26//! greater than or equal to `1.26.0` and disabled for targets which do not support it
27
28#![crate_name = "float_pigment_consistent_bincode"]
29#![crate_type = "rlib"]
30
31#[macro_use]
32extern crate alloc;
33#[cfg(feature = "std")]
34extern crate std;
35
36extern crate byteorder;
37#[macro_use]
38extern crate serde;
39
40use alloc::vec::Vec;
41
42pub mod config;
43/// Deserialize bincode data to a Rust data structure.
44pub mod de;
45pub mod io;
46
47mod error;
48mod internal;
49mod ser;
50
51pub use crate::config::{DefaultOptions, Options, SizeDetail};
52pub use crate::de::read::BincodeRead;
53pub use crate::de::Deserializer;
54pub use crate::error::{Error, ErrorKind, Result};
55pub use crate::ser::Serializer;
56
57/// Get a default configuration object.
58///
59/// ### Default Configuration:
60///
61/// | Byte limit | Endianness | Int Encoding | Trailing Behavior |
62/// |------------|------------|--------------|-------------------|
63/// | Unlimited  | Little     | Varint       | Reject            |
64#[inline(always)]
65pub fn options() -> DefaultOptions {
66    DefaultOptions::new()
67}
68
69/// Serializes an object directly into a `Writer` using the default configuration.
70///
71/// If the serialization would take more bytes than allowed by the size limit, an error
72/// is returned and *no bytes* will be written into the `Writer`.
73#[cfg(feature = "std")]
74pub fn serialize_into<W, T: ?Sized>(writer: W, value: &T) -> Result<()>
75where
76    W: crate::io::Write,
77    T: serde::Serialize,
78{
79    let (_, sizes_list) = DefaultOptions::new().serialized_size(value)?;
80    DefaultOptions::new().serialize_into(writer, value, sizes_list)
81}
82
83/// Serializes a serializable object into a `Vec` of bytes using the default configuration.
84pub fn serialize<T: ?Sized>(value: &T) -> Result<Vec<u8>>
85where
86    T: serde::Serialize,
87{
88    DefaultOptions::new().serialize(value)
89}
90
91/// Deserializes an object directly from a `Read`er using the default configuration.
92///
93/// If this returns an `Error`, `reader` may be in an invalid state.
94#[cfg(feature = "std")]
95pub fn deserialize_from<R, T>(reader: R) -> Result<T>
96where
97    R: crate::io::Read,
98    T: serde::de::DeserializeOwned,
99{
100    DefaultOptions::new().deserialize_from(reader)
101}
102
103/// Deserializes an object from a custom `BincodeRead`er using the default configuration.
104/// It is highly recommended to use `deserialize_from` unless you need to implement
105/// `BincodeRead` for performance reasons.
106///
107/// If this returns an `Error`, `reader` may be in an invalid state.
108pub fn deserialize_from_custom<'a, R, T>(reader: R) -> Result<T>
109where
110    R: de::read::BincodeRead<'a>,
111    T: serde::de::DeserializeOwned,
112{
113    DefaultOptions::new().deserialize_from_custom(reader)
114}
115
116/// Only use this if you know what you're doing.
117///
118/// This is part of the public API.
119#[doc(hidden)]
120pub fn deserialize_in_place<'a, R, T>(reader: R, place: &mut T) -> Result<()>
121where
122    T: serde::de::Deserialize<'a>,
123    R: BincodeRead<'a>,
124{
125    DefaultOptions::new().deserialize_in_place(reader, place)
126}
127
128/// Deserializes a slice of bytes into an instance of `T` using the default configuration.
129pub fn deserialize<'a, T>(bytes: &'a [u8]) -> Result<T>
130where
131    T: serde::de::Deserialize<'a>,
132{
133    DefaultOptions::new().deserialize(bytes)
134}
135
136/// Returns the size that an object would be if serialized using Bincode with the default configuration.
137pub fn serialized_size<T: ?Sized>(value: &T) -> Result<(u64, SizeDetail)>
138where
139    T: serde::Serialize,
140{
141    DefaultOptions::new().serialized_size(value)
142}