1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! [![GitHub][github-badge]][github-url]
//! [![Crates.io][crates-io-badge]][crates-io-url]
//! [![Docs.rs][docsrs-badge]][docsrs-url]
//! ![license-badge]
//!
//! Serialization according to the SOME/IP on-wire format.
//!
//! This crate provides traits and types to assist in correctly implementing the serialization and
//! deserialization of data according to the [Open SOME/IP Specification][open-someip-spec].
//!
//! # Getting started
//!
//! 1. Add `rsomeip-bytes` as a dependency to your project.
//!
//! ```toml
//! # Cargo.toml
//!
//! [dependencies]
//! rsomeip-bytes = "0.2.0"
//! ```
//!
//! 2. Implement [`Serialize`] and [`Deserialize`] for your data types.
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use rsomeip_bytes::{
//! Serialize, SerializeError, Deserialize, DeserializeError, bytes::{Buf, BufMut}
//! };
//!
//! /// Example of a composite type that is used in a SOME/IP message payload.
//! #[derive(Debug, PartialEq, Eq)]
//! struct Foo {
//! bar: u8,
//! baz: u16,
//! }
//!
//! impl Serialize for Foo {
//! // This method is used to write the data to the buffer.
//! fn serialize<Buffer>(&self, buffer: &mut Buffer) -> Result<usize, SerializeError>
//! where
//! Buffer: BufMut + ?Sized,
//! {
//! // Most basic types already implement `Serialize`.
//! let mut size = 0;
//! size += self.bar.serialize(buffer)?;
//! size += self.baz.serialize(buffer)?;
//! Ok(size)
//! }
//!
//! // This method is used for calculating the value of length fields, for example.
//! fn size(&self) -> Option<usize> {
//! // It's important that this value matches the size returned by the `serialize` method.
//! let mut size = 0;
//! size += self.bar.size()?;
//! size += self.baz.size()?;
//! Some(size)
//! }
//! }
//!
//! impl Deserialize for Foo {
//! type Output = Self;
//!
//! // This method is used to read data from the buffer.
//! fn deserialize<Buffer>(buffer: &mut Buffer) -> Result<Self::Output, DeserializeError>
//! where
//! Buffer: Buf + ?Sized,
//! {
//! // Like before, most basic types also implement `Deserialize`.
//! let value = Self {
//! bar: u8::deserialize(buffer)?,
//! baz: u16::deserialize(buffer)?,
//! };
//! Ok(value)
//! }
//!
//! // This method is used as an estimate of the minimum amount of data required to deserialize
//! // the output from the buffer.
//! fn size_hint() -> Option<usize> {
//! let mut size = 0;
//! size += u8::size_hint()?;
//! size += u16::size_hint()?;
//! Some(size)
//! }
//! }
//!
//! // A buffer can be any type that implements `Buf` and `BufMut`.
//! let mut buffer = [0u8; 3];
//!
//! // Use the `Serialize` trait to write data to the buffer.
//! let value = Foo { bar: 0x01_u8, baz: 0x0203_u16 };
//! assert_eq!(Some(3), value.size());
//! assert_eq!(Ok(3), value.serialize(&mut buffer.as_mut_slice()));
//!
//! // By default, data is serialized in Big Endian byte order.
//! assert_eq!(buffer, [0x01_u8, 0x02, 0x03]);
//!
//! // Use the `Deserialize` trait to read data from the buffer.
//! assert_eq!(Some(3), Foo::size_hint());
//! assert_eq!(Ok(value), Foo::deserialize(&mut buffer.as_slice()));
//! # Ok(()) }
//!
//! # Usage
//!
//! The main goal with this crate is to have your types implement the [`Serialize`] and
//! [`Deserialize`] traits so that the other `rsomeip` crates can abstract the serialization and
//! deserialization process.
//!
//! To make this easier, this crate implements these traits for several types of the Rust standard
//! library and provides some convenient wrappers for those that don't. This is enough to cover most
//! use cases foreseen by the specification.
//!
//! ## Basic types
//!
//! All basic types defined in the specification implement [`Serialize`] and [`Deserialize`]. These
//! include:
//!
//! - [`bool`]
//! - [`u8`] and [`i8`]
//! - [`u16`] and [`i16`]
//! - [`u32`] and [`i32`]
//! - [`u64`] and [`i64`]
//! - [`f32`] and [`f64`]
//!
//! ### Endianess
//!
//! The [`Serialize::serialize`] and [`Deserialize::deserialize`] implementations for these types
//! use Big Endian byte orders for writing and reading data. If another endianess is desired, then
//! you must manually implement it yourself.
//!
//! ## Tuples
//!
//! There's a blanket implementation of both traits for any tuple whose types also implement
//! [`Serialize`] and [`Deserialize`].
//!
//! This can be used as a convenient way to call these methods on a bunch of elements all at once.
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use rsomeip_bytes::{Serialize, Deserialize};
//!
//! let mut buffer = [0u8; 7];
//!
//! let value = (0x01_u8, 0x0203_u16, 0x0405_0607_u32);
//! assert_eq!(Ok(7), value.serialize(&mut buffer.as_mut_slice()));
//! assert_eq!(buffer, [0x01_u8, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]);
//! assert_eq!(Ok(value), <(u8, u16, u32)>::deserialize(&mut buffer.as_slice()));
//! # Ok(()) }
//! ```
//!
//! ## Structs
//!
//! Structs required you to manually implement the [`Serialize`] and [`Deserialize`] traits as
//! described in the [Getting started](#getting-started) section.
//!
//! ## Dynamic arrays
//!
//! This crate doesn't implement any traits for container types of the standard library. Instead, it
//! provides the [`DynamicArray`] wrapper to serialize and deserialize any type that implements
//! [`IntoIterator`] and [`FromIterator`], respectively.
//!
//! This [`DynamicArray`] type takes a [`Length`] as a generic parameter to encode the size of the
//! array in a preceding length field.
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use rsomeip_bytes::{Serialize, Deserialize, LengthU32, DynamicArray};
//!
//! let mut buffer = [0u8; 7];
//!
//! let value = vec![1_u8, 2, 3];
//! let array = DynamicArray::<LengthU32, _>::from(&value);
//! assert_eq!(Ok(7), array.serialize(&mut buffer.as_mut_slice()));
//! assert_eq!(buffer, [0x00_u8, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03]); // Length before the payload.
//! assert_eq!(Ok(value), DynamicArray::<LengthU32, Vec<u8>>::deserialize(&mut buffer.as_slice()));
//! # Ok(()) }
//! ```
//!
//! ## Static arrays
//!
//! Static arrays use Rust's [`prim@array`] primitive since it translates directly into the SOME/IP
//! notion of an array.
//!
//! Since their size is fixed, static arrays don't require a length field.
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use rsomeip_bytes::{Serialize, Deserialize};
//!
//! let mut buffer = [0u8; 4];
//!
//! let value = [1_u8, 2, 3, 4];
//! assert_eq!(Ok(4), value.serialize(&mut buffer.as_mut_slice()));
//! assert_eq!(buffer, [0x01_u8, 0x02, 0x03, 0x04]);
//! assert_eq!(Ok(value), <[u8; 4]>::deserialize(&mut buffer.as_slice()));
//! # Ok(()) }
//! ```
//!
//! ## Dynamic strings
//!
//! Like for dynamic arrays, this crate provides the [`DynamicString`] wrapper for any type that
//! implements [`AsRef<str>`] and [`From<String>`].
//!
//! Besides also accepting a [`Length`] parameter, this wrapper takes an [`Encoding`] parameter to
//! specify the encoding of the serialized string.
//!
//! Available encodings include [`Utf8`], [`Utf16BE`], and [`Utf16LE`].
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use rsomeip_bytes::{Serialize, Deserialize, LengthU32, Utf8, DynamicString};
//!
//! let mut buffer = [0_u8; 15];
//!
//! let value = String::from("rsomeip");
//! let string = DynamicString::<LengthU32, Utf8, _>::from(&value);
//! assert_eq!(Ok(15), string.serialize(&mut buffer.as_mut_slice()));
//! assert_eq!(
//! buffer,
//! [
//! 0x00_u8, 0x00, 0x00, 0x0b, // Length
//! 0xef, 0xbb, 0xbf, // UTF-8 BOM
//! 0x72, 0x73, 0x6f, 0x6d, 0x65, 0x69, 0x70, // "rsomeip"
//! 0x00, // Delimiter
//! ]
//! );
//! assert_eq!(
//! Ok(value),
//! DynamicString::<LengthU32, Utf8, String>::deserialize(&mut buffer.as_slice())
//! );
//! # Ok(()) }
//! ```
//!
//! ## Static strings
//!
//! These fill the same role as static arrays do for generic containers. The [`StaticString`]
//! wrapper takes an [`Encoding`] parameter like the dynamic strings, but the [`Length`] parameter
//! is dropped in favor of specifying a fixed size for the string.
//!
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use rsomeip_bytes::{Serialize, Deserialize, Utf8, StaticString};
//!
//! let mut buffer = [0_u8; 15];
//!
//! let value = String::from("rsomeip");
//! let string = StaticString::<15, Utf8, _>::from(&value);
//! assert_eq!(Ok(15), string.serialize(&mut buffer.as_mut_slice()));
//! assert_eq!(
//! buffer,
//! [
//! 0xef_u8, 0xbb, 0xbf, // UTF-8 BOM
//! 0x72, 0x73, 0x6f, 0x6d, 0x65, 0x69, 0x70, // "rsomeip"
//! 0x00, // Delimiter
//! 0x00, 0x00, 0x00, 0x00, // Padding
//! ]
//! );
//! assert_eq!(
//! Ok(value),
//! StaticString::<15, Utf8, String>::deserialize(&mut buffer.as_slice())
//! );
//! # Ok(()) }
//! ```
//!
//! ## Enums and unions
//!
//! These need to be manually implemented by you.
//!
//! # License
//!
//! This project is licensed under either the [Apache-2.0 License] or [MIT License],
//! at your option.
//!
//! [Apache-2.0 License]: http://www.apache.org/licenses/LICENSE-2.0
//! [crates-io-badge]: https://img.shields.io/crates/v/rsomeip_bytes
//! [crates-io-url]: https://crates.io/crates/rsomeip-bytes
//! [docsrs-badge]: https://img.shields.io/docsrs/rsomeip-bytes
//! [docsrs-url]: https://docs.rs/rsomeip-bytes/latest/rsomeip_bytes/
//! [github-badge]: https://img.shields.io/badge/GitHub-rsomeip-blue
//! [github-url]: https://github.com/brunoldsilva/rsomeip
//! [license-badge]: https://img.shields.io/crates/l/rsomeip_bytes
//! [MIT License]: http://opensource.org/licenses/MIT
//! [open-someip-spec]: https://some-ip.com/standards.shtml
extern crate alloc;
extern crate core;
// Re-export for convenience.
pub use ;
pub use DynamicArray;
pub use ;
pub use ;
pub use ;
pub use ;
;