44//! # Series of compact encoding schemes for building small and fast parsers and serializers
55//!
66//! Binary compatible with the
7- //! [original Javascript compact-encoding library](https://github.com/compact-encoding/compact-encoding/).
7+ //! [original JavaScript compact-encoding library](https://github.com/compact-encoding/compact-encoding/).
88//!
99//! ## Usage
1010//!
11- //! ### Quick start
11+ //! The simplest way to encoded and decode a some data is using the [`to_encoded_bytes`] and
12+ //! [`map_decode`] macros:
1213//! ```
1314//! use compact_encoding::{map_decode, to_encoded_bytes};
1415//!
2324//! assert_eq!(word_dec, word);
2425//! # Ok::<(), Box<dyn std::error::Error>>(())
2526//! ```
26- //! ### Manually implement encoding and decoding
27+ //! ### Manual encoding and decoding
2728//!
28- //! This example shows how to manually calculate the needed buffer size, create the buffer, encode
29- //! data, and decode it. Using only the types which include a default [`CompactEncoding`]
30- //! implementation. A more ergonomic pattern is demonstrated in other examples
29+ //! When more fine-grained control of encoding and decoding is needed you manually do each step of
30+ //! encoding and decoding like in the following example, where we want to use a fixed width
31+ //! encoding for `number` (instead of the default variable width encoding). It shows how to
32+ //! manually calculate the needed buffer size, create the buffer, encode data, and decode it.
3133//! ```
32- //! use compact_encoding::CompactEncoding;
34+ //! use compact_encoding::{ CompactEncoding, FixedWidthEncoding, FixedWidthU32} ;
3335//!
3436//! let number = 41_u32;
3537//! let word = "hi";
3638//!
37- //! // Use `encoded_size` to figure out how big a buffer should be.
38- //! let size = number.encoded_size()? + word.encoded_size()?;
39+ //! // Use `encoded_size` to figure out how big a buffer should be
40+ //! let size = number.as_fixed_width(). encoded_size()? + word.encoded_size()?;
3941//!
4042//! // Create a buffer with the calculated size
4143//! let mut buffer = vec![0; size];
42- //! assert_eq!(buffer.len(), 1 + 1 + 2);
44+ //! assert_eq!(buffer.len(), 4 + 1 + 2);
4345//!
4446//! // Then actually encode the values
45- //! let mut remaining_buffer = number.encode(&mut buffer)?;
47+ //! let mut remaining_buffer = number.as_fixed_width(). encode(&mut buffer)?;
4648//! remaining_buffer = word.encode(remaining_buffer)?;
4749//! assert!(remaining_buffer.is_empty());
48- //! assert_eq!(buffer.to_vec(), vec![41_u8, 2_u8, b'h', b'i']);
50+ //! assert_eq!(buffer.to_vec(), vec![41_u8, 0, 0, 0, 2_u8, b'h', b'i']);
4951//!
5052//! // `buffer` now contains all the encoded data, and we can decode from it
51- //! let (number_dec, remaining_buffer) = u32 ::decode(&buffer)?;
53+ //! let (number_dec, remaining_buffer) = FixedWidthU32 ::decode(&buffer)?;
5254//! let (word_dec, remaining_buffer) = String::decode(remaining_buffer)?;
5355//! assert!(remaining_buffer.is_empty());
5456//! assert_eq!(number_dec, number);
5557//! assert_eq!(word_dec, word);
5658//! # Ok::<(), Box<dyn std::error::Error>>(())
5759//! ```
5860//!
59- //! ### Implementing CompactEncoding for new types
61+ //! ### Implementing CompactEncoding for custom types
6062//!
61- //! You can implement [`CompactEncoding`]` for your own structs like below:
63+ //! Here we demonstrate how to implement [`CompactEncoding`] for a custom struct.
6264//! ```
6365//! use compact_encoding::{
6466//! map_decode, map_encode, sum_encoded_size, to_encoded_bytes, CompactEncoding, EncodingError,
@@ -346,7 +348,7 @@ macro_rules! create_buffer {
346348 + $val. encoded_size( ) ?
347349 ) *
348350 ) ;
349- vec![ 0 ; len]
351+ vec![ 0 ; len] . into_boxed_slice ( )
350352 } }
351353}
352354
@@ -360,7 +362,7 @@ macro_rules! create_buffer {
360362/// let mut buff = create_buffer!(num, word);
361363/// let result = map_encode!(&mut buff, num, word);
362364/// assert!(result.is_empty());
363- /// assert_eq!(&buff, &[42, 2, b'y', b'o']);
365+ /// assert_eq!(&* buff, &[42, 2, b'y', b'o']);
364366/// # Ok::<(), Box<dyn std::error::Error>>(())
365367/// ```
366368macro_rules! map_encode {
@@ -384,7 +386,7 @@ macro_rules! map_encode {
384386/// ```
385387/// # use crate::compact_encoding::to_encoded_bytes;
386388/// let result = to_encoded_bytes!(42u64, "yo");
387- /// assert_eq!(&result, &[42, 2, 121, 111]);
389+ /// assert_eq!(&* result, &[42, 2, 121, 111]);
388390/// # Ok::<(), Box<dyn std::error::Error>>(())
389391/// ```
390392macro_rules! to_encoded_bytes {
0 commit comments