Debian Codegraph @

third_party/rust/rmp/README.md

raw
1 ## The Rust MessagePack Library
2
3 RMP is a pure Rust [MessagePack](http://msgpack.org) implementation of an efficient binary
4 serialization format. This crate provides low-level core functionality, writers and readers for
5 primitive values with direct mapping between binary MessagePack format.
6
7 [Looking for Serde support](https://lib.rs/crates/rmp-serde)?
8
9 This crate represents the very basic functionality needed to work with MessagePack format.
10 Ideologically it is developed as a basis for building high-level abstractions.
11
12 ### Usage
13
14 To use `rmp`, first add this to your `Cargo.toml`:
15
16 ```toml
17 [dependencies.rmp]
18 rmp = "0.8"
19 ```
20
21 ### Features
22
23 - **Low-level API**
24
25 RMP is designed to be lightweight and straightforward. There are low-level APIs, which give you
26 full control over the encoding/decoding process. `no-std` environments are supported.
27
28 - **Zero-copy value decoding**
29
30 RMP allows to decode bytes from a buffer in a zero-copy manner, without any heap allocations.
31 easily and blazingly fast. Rust static checks guarantee that the data will be valid until buffer lives.
32
33 - **Clear error handling**
34
35 RMP's error system guarantees that you never receive an error enum with unreachable variant.
36
37 - **Robust and tested**
38
39 This project is developed using TDD and CI, so any found bugs will be fixed without breaking
40 existing functionality.
41
42 ### Detailed
43
44 Currently there are two large modules: encode and decode. More detail you can find in the
45 corresponding sections.
46
47 Formally every MessagePack message consists of some marker encapsulating a data type and the
48 data itself. Sometimes there are no separate data chunk, for example for booleans. In these
49 cases a marker contains the value. For example, the `true` value is encoded as `0xc3`.
50
51 ```rust
52 let mut buf = Vec::new();
53 rmp::encode::write_bool(&mut buf, true).unwrap();
54
55 assert_eq!([0xc3], buf[..]);
56 ```
57
58 Sometimes a single value can be encoded in multiple ways. For example a value of `42` can be
59 represented as: `[0x2a], [0xcc, 0x2a], [0xcd, 0x00, 0x2a]` and so on, and all of them are
60 considered as valid representations. To allow fine-grained control over encoding such values
61 the library provides direct mapping functions.
62
63 ```rust
64 let mut bufs = vec![vec![]; 5];
65
66 rmp::encode::write_pfix(&mut bufs[0], 42).unwrap();
67 rmp::encode::write_u8(&mut bufs[1], 42).unwrap();
68 rmp::encode::write_u16(&mut bufs[2], 42).unwrap();
69 rmp::encode::write_u32(&mut bufs[3], 42).unwrap();
70 rmp::encode::write_u64(&mut bufs[4], 42).unwrap();
71
72 assert_eq!([0x2a], bufs[0][..]);
73 assert_eq!([0xcc, 0x2a], bufs[1][..]);
74 assert_eq!([0xcd, 0x00, 0x2a], bufs[2][..]);
75 assert_eq!([0xce, 0x00, 0x00, 0x00, 0x2a], bufs[3][..]);
76 assert_eq!([0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a], bufs[4][..]);
77 ```
78
79 But they aren't planned to be widely used. Instead we often need to encode bytes compactly to
80 save space. In these cases RMP provides functions that guarantee that for encoding the most
81 compact representation will be chosen.
82
83 ```rust
84 let mut buf = Vec::new();
85
86 rmp::encode::write_sint(&mut buf, 300).unwrap();
87
88 assert_eq!([0xcd, 0x1, 0x2c], buf[..]);
89 ```
90
91 On the other hand for deserialization it is not matter in which representation the value is
92 encoded - RMP deals with all of them.
93
94 Sometimes you know the exact type representation and want to enforce the deserialization process
95 to make it strongly type safe.
96
97 ```rust
98 let buf = [0xcd, 0x1, 0x2c];
99
100 assert_eq!(300, rmp::decode::read_u16(&mut &buf[..]).unwrap());
101 ```
102
103 However if you try to decode such bytearray as other integer type, for example `u32`, there will
104 be type mismatch error.
105
106 ```rust
107 let buf = [0xcd, 0x1, 0x2c];
108 rmp::decode::read_u32(&mut &buf[..]).err().unwrap();
109 ```
110
111 But sometimes all you want is just to encode an integer that *must* fit in the specified type
112 no matter how it was encoded. RMP provides [`such`][read_int] function to ease integration with
113 other MessagePack libraries.
114
115 ```rust
116 let buf = [0xcd, 0x1, 0x2c];
117
118 assert_eq!(300i16, rmp::decode::read_int(&mut &buf[..]).unwrap());
119 assert_eq!(300i32, rmp::decode::read_int(&mut &buf[..]).unwrap());
120 assert_eq!(300i64, rmp::decode::read_int(&mut &buf[..]).unwrap());
121 assert_eq!(300u16, rmp::decode::read_int(&mut &buf[..]).unwrap());
122 assert_eq!(300u32, rmp::decode::read_int(&mut &buf[..]).unwrap());
123 assert_eq!(300u64, rmp::decode::read_int(&mut &buf[..]).unwrap());
124 ```
125
126 ### API
127
128 Almost all API are represented as pure functions, which accepts a generic `Write` or `Read` and
129 the value to be encoded/decoded. For example let's do a round trip for π number.
130
131 ```rust
132 let pi = std::f64::consts::PI;
133 let mut buf = Vec::new();
134 rmp::encode::write_f64(&mut buf, pi).unwrap();
135
136 assert_eq!([0xcb, 0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18], buf[..]);
137 assert_eq!(pi, rmp::decode::read_f64(&mut &buf[..]).unwrap());
138 ```
139
140 License: MIT
141
142 [read_int]: https://docs.rs/rmp/latest/rmp/decode/fn.read_int.html