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
//! Convenient interfaces to CXS serialization.
//!
//! This module provides high-level functions for encoding and decoding XDR
//! data using the CXS (Compact XDR Serialization) format.
use crate*;
use crate*;
use crate*;
use crate::*;
use MaybeUninit as MU;
/// Decodes CXS-encoded XDR data from a byte slice.
///
/// This function reads the entire buffer and deserializes a value of type `T`.
/// It returns an error if there are any remaining bytes after decoding.
///
/// # Example
///
/// ```rust
/// use xdrs::cxs;
///
/// let data = [0u8]; // valid CXS encoding for the unsigned integer 0
/// let value: u32 = cxs::decode(&data)?;
/// # Ok::<(), xdrs::ReadError>(())
/// ```
/// Encodes a value as CXS-encoded XDR data into a pre-allocated buffer.
///
/// This function serializes the value into the provided buffer and returns the
/// number of bytes written. Returns `Err(WriteError::SpaceExhausted)` if the
/// buffer is not large enough to hold the serialized data.
///
/// # Example
///
/// ```rust
/// use xdrs::cxs;
/// use core::mem::MaybeUninit;
///
/// let value: u32 = 42;
///
/// let mut buf = [MaybeUninit::uninit(); 16];
///
/// let written = cxs::encode_into(&value, &mut buf)?;
/// # Ok::<(), xdrs::WriteError>(())
/// ```
Sized>
/// Encodes a value as CXS-encoded XDR data into a pre-allocated buffer,
/// without runtime checks.
///
/// This is the infallible, unsafe counterpart to [`encode_into`]: no bounds
/// checking is performed and no error can be returned, minimizing code size
/// on constrained targets. Returns the number of bytes written.
///
/// # Safety
///
/// - `buf.len()` must be at least the encoded size of `value`. With
/// `#[derive(ConstXDRSize)]`, `<T as cxs::ConstMaxSize>::MAX_SIZE` (worst
/// case over all values) — or, with `#[derive(ConstWriteXDR)]`,
/// `const_cxs_size()` (exact for this value) — provide suitable sizes.
/// - `value` must respect all declared XDR bounds (e.g. a `VecM<T, MAX>`
/// must not be longer than `MAX`), since a violation would make the
/// encoding exceed any precomputed size bound. This includes the implicit
/// `u32::MAX` upper limit on variable-length arrays and strings: the
/// checked path returns [`WriteError::InvalidArrayLength`] for lengths
/// exceeding `u32::MAX`; the unchecked path only `debug_assert!`s this.
///
/// Violations are caught by `debug_assert!` in debug builds; in release
/// builds they are undefined behavior.
///
/// # Example
///
/// ```rust
/// use xdrs::cxs;
/// use core::mem::MaybeUninit;
///
/// let value: u32 = 300;
///
/// // Worst-case CXS size of a u32 is 5 bytes.
/// let mut buf = [MaybeUninit::uninit(); 5];
///
/// // SAFETY: the buffer is sized to the worst case.
/// let written = unsafe { cxs::encode_into_unchecked(&value, &mut buf) };
/// assert_eq!(written, 3); // 300 takes the 2-byte repr plus a header byte
/// ```
pub unsafe
pub use *;