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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2024-2026 Gabriel Bjørnager Jensen.
//
// SPDX: MIT OR Apache-2.0
//! Octonary transcoder.
//!
//! This library provides facilities for safe (and
//! hopefully sound) transmutations of Rust objects,
//! mainly to and from octonary (bytewise)
//! representations. These transformations may be
//! done in two ways:
//!
//! * In-place transmutations: Objects can be
//! reinterpret between different types in-place.
//! * Serialisations: Objects can also be serialise
//! into a portable representation.
//!
//! These interfaces are generally safe and are
//! designed so that most programmes can completely
//! avoid any unsafe code.
//!
//! # Security
//!
//! Do note that this project is still relatively
//! early in its development. But even with the
//! hundreds of man-hours of development so far, I
//! very much recommend auditing it before using it
//! in production (nevertheless the so-far soundness
//! holes uncovered.) I would not *yet* use this
//! project in critical systems -- although I am
//! confident that it will become realistic to do so
//! at some point.
//!
//! # Interface stability and versioning
//!
//! Breaking changes are common, and usage of this
//! library will require continuous maintenance so
//! as to avoid becoming stuck on an unmaintained
//! major release. Nonetheless, core designs are
//! likely to remain more or less the same, and most
//! breakage will be in the form of lesser,
//! syntactical changes, e.g. identifier renaming or
//! parameter reordering.
//!
//! The octonary format used by serialisations is
//! not stabilised and are not on track to become
//! stabilised. Instead, users should use in-place
//! transmutations or custom serialisers for
//! specific specifications.
//!
//! # Copyright and licence
//!
//! Copyright © 2024‐2026 Gabriel
//! Bjørnager Jensen.
//!
//! This library is distributed under either an MIT
//! licence or version 2.0 of the Apache License, at
//! your option. See `LICENCE-MIT.txt` and
//! `LICENCE-APACHE.txt` for more information.
//!
//! SPDX identifier: `MIT OR Apache-2.0`
extern crate self as oct;
extern crate alloc;
extern crate std;
pub use FromOctets;
pub use Immutable;
pub use IntoOctets;
pub use Outlay;
pub use Unaligned;
pub use ;
pub use Zeroable;
pub use ;
/// Implements [`FromOctets`] for the given type.
///
/// [`FromOctets`]: trait@FromOctets
///
/// # Examples
///
/// Any type can derive [`FromOctets`] -- as long as
/// all member fields also implement it.
///
/// [`FromOctets`]: trait@FromOctets
///
/// ```rust
/// use oct::{FromOctets, Zeroable};
///
/// #[derive(FromOctets, Zeroable)]
/// struct Reminder {
/// timestamp: i64,
/// }
/// ```
///
/// For enumerations, this includes the discriminant
/// -- which must have all values occupied:
///
/// ```rust
/// use oct::{FromOctets, Zeroable};
///
/// #[repr(u8)]
/// #[derive(FromOctets, Zeroable)]
/// enum U8Enum {
/// V000, V001, V002, V003, V004, V005, V006, V007, V008, V009,
/// V010, V011, V012, V013, V014, V015, V016, V017, V018, V019,
/// V020, V021, V022, V023, V024, V025, V026, V027, V028, V029,
/// V030, V031, V032, V033, V034, V035, V036, V037, V038, V039,
/// V040, V041, V042, V043, V044, V045, V046, V047, V048, V049,
/// V050, V051, V052, V053, V054, V055, V056, V057, V058, V059,
/// V060, V061, V062, V063, V064, V065, V066, V067, V068, V069,
/// V070, V071, V072, V073, V074, V075, V076, V077, V078, V079,
/// V080, V081, V082, V083, V084, V085, V086, V087, V088, V089,
/// V090, V091, V092, V093, V094, V095, V096, V097, V098, V099,
/// V100, V101, V102, V103, V104, V105, V106, V107, V108, V109,
/// V110, V111, V112, V113, V114, V115, V116, V117, V118, V119,
/// V120, V121, V122, V123, V124, V125, V126, V127, V128, V129,
/// V130, V131, V132, V133, V134, V135, V136, V137, V138, V139,
/// V140, V141, V142, V143, V144, V145, V146, V147, V148, V149,
/// V150, V151, V152, V153, V154, V155, V156, V157, V158, V159,
/// V160, V161, V162, V163, V164, V165, V166, V167, V168, V169,
/// V170, V171, V172, V173, V174, V175, V176, V177, V178, V179,
/// V180, V181, V182, V183, V184, V185, V186, V187, V188, V189,
/// V190, V191, V192, V193, V194, V195, V196, V197, V198, V199,
/// V200, V201, V202, V203, V204, V205, V206, V207, V208, V209,
/// V210, V211, V212, V213, V214, V215, V216, V217, V218, V219,
/// V220, V221, V222, V223, V224, V225, V226, V227, V228, V229,
/// V230, V231, V232, V233, V234, V235, V236, V237, V238, V239,
/// V240, V241, V242, V243, V244, V245, V246, V247, V248, V249,
/// V250, V251, V252, V253, V254, V255,
/// }
/// ```
///
/// Enumerations without defined representations are
/// always rejected:
///
/// ```rust,compile_fail
/// use oct::{FromOctets, Zeroable};
///
/// #[derive(FromOctets, Zeroable)]
/// enum EnumWithoutRepr {
/// V000, V001, V002, V003, V004, V005, V006, V007, V008, V009,
/// V010, V011, V012, V013, V014, V015, V016, V017, V018, V019,
/// V020, V021, V022, V023, V024, V025, V026, V027, V028, V029,
/// V030, V031, V032, V033, V034, V035, V036, V037, V038, V039,
/// V040, V041, V042, V043, V044, V045, V046, V047, V048, V049,
/// V050, V051, V052, V053, V054, V055, V056, V057, V058, V059,
/// V060, V061, V062, V063, V064, V065, V066, V067, V068, V069,
/// V070, V071, V072, V073, V074, V075, V076, V077, V078, V079,
/// V080, V081, V082, V083, V084, V085, V086, V087, V088, V089,
/// V090, V091, V092, V093, V094, V095, V096, V097, V098, V099,
/// V100, V101, V102, V103, V104, V105, V106, V107, V108, V109,
/// V110, V111, V112, V113, V114, V115, V116, V117, V118, V119,
/// V120, V121, V122, V123, V124, V125, V126, V127, V128, V129,
/// V130, V131, V132, V133, V134, V135, V136, V137, V138, V139,
/// V140, V141, V142, V143, V144, V145, V146, V147, V148, V149,
/// V150, V151, V152, V153, V154, V155, V156, V157, V158, V159,
/// V160, V161, V162, V163, V164, V165, V166, V167, V168, V169,
/// V170, V171, V172, V173, V174, V175, V176, V177, V178, V179,
/// V180, V181, V182, V183, V184, V185, V186, V187, V188, V189,
/// V190, V191, V192, V193, V194, V195, V196, V197, V198, V199,
/// V200, V201, V202, V203, V204, V205, V206, V207, V208, V209,
/// V210, V211, V212, V213, V214, V215, V216, V217, V218, V219,
/// V220, V221, V222, V223, V224, V225, V226, V227, V228, V229,
/// V230, V231, V232, V233, V234, V235, V236, V237, V238, V239,
/// V240, V241, V242, V243, V244, V245, V246, V247, V248, V249,
/// V250, V251, V252, V253, V254, V255,
/// }
/// ```
///
/// Enumerations are also rejected if they have a
/// partial discriminant:
///
/// ```rust,compile_fail
/// use oct::{FromOctets, Zeroable};
///
/// #[derive(FromOctets, Zeroable)]
/// enum EnumWithoutCompleteDiscriminant {
/// V000, V001, V002, V003, V004, V005, V006, V007, V008, V009,
/// V010, V011, V012, V013, V014, V015, V016, V017, V018, V019,
/// V020, V021, V022, V023, V024, V025, V026, V027, V028, V029,
/// V030, V031, V032, V033, V034, V035, V036, V037, V038, V039,
/// V040, V041, V042, V043, V044, V045, V046, V047, V048, V049,
/// V050, V051, V052, V053, V054, V055, V056, V057, V058, V059,
/// V060, V061, V062, V063, V064, V065, V066, V067, V068, V069,
/// V070, V071, V072, V073, V074, V075, V076, V077, V078, V079,
/// V080, V081, V082, V083, V084, V085, V086, V087, V088, V089,
/// V090, V091, V092, V093, V094, V095, V096, V097, V098, V099,
/// V100, V101, V102, V103, V104, V105, V106, V107, V108, V109,
/// V110, V111, V112, V113, V114, V115, V116, V117, V118, V119,
/// V120, V121, V122, V123, V124, V125, V126, V127, V128, V129,
/// V130, V131, V132, V133, V134, V135, V136, V137, V138, V139,
/// V140, V141, V142, V143, V144, V145, V146, V147, V148, V149,
/// V150, V151, V152, V153, V154, V155, V156, V157, V158, V159,
/// V160, V161, V162, V163, V164, V165, V166, V167, V168, V169,
/// V170, V171, V172, V173, V174, V175, V176, V177, V178, V179,
/// V180, V181, V182, V183, V184, V185, V186, V187, V188, V189,
/// V190, V191, V192, V193, V194, V195, V196, V197, V198, V199,
/// V200, V201, V202, V203, V204, V205, V206, V207, V208, V209,
/// V210, V211, V212, V213, V214, V215, V216, V217, V218, V219,
/// V220, V221, V222, V223, V224, V225, V226, V227, V228, V229,
/// V230, V231, V232, V233, V234, V235, V236, V237, V238, V239,
/// V240, V241, V242, V243, V244, V245, V246, V247, V248, V249,
/// V250, V251, V252, V253, V254, /* V255, */
/// }
/// ```
pub use FromOctets;
/// Implements [`Immutable`] for the given type.
///
/// [`Immutable`]: trait@Immutable
///
/// # Examples
///
/// Most types can simply derive [`Immutable`],
/// provided that all fields also implement
/// `Immutable`:
///
/// [`Immutable`]: trait@Immutable
///
/// ```rust
/// use oct::Immutable;
///
/// #[derive(Immutable)]
/// struct InteriourImmutableI32 {
/// value: i32,
/// }
/// ```
///
/// Types that contain [`UnsafeCell`] (or other cell
/// types) can thus not derive `Immutable`:
///
/// [`UnsafeCell`]: core::cell::UnsafeCell
///
/// ```rust,compile_fail
/// use core::cell::Cell;
/// use oct::Immutable;
///
/// #[derive(Immutable)]
/// struct InteriourMutableI32 {
/// value: Cell<i32>,
/// }
/// ```
pub use Immutable;
/// Implements [`Zeroable`] for the given type.
///
/// [`Zeroable`]: trait@Zeroable
///
/// # Examples
///
/// Enumerations must have a variant that uses `0`
/// as its discriminant:
///
/// ```rust
/// use oct::Zeroable;
///
/// #[repr(u8)]
/// #[derive(Zeroable)]
/// enum ExplicitZero {
/// Zero = 0,
/// }
///
/// #[repr(i8)]
/// #[derive(Zeroable)]
/// enum ImplicitZero {
/// NegativeOne = -1,
/// Zero,
/// }
/// ```
///
/// Thus, the following is rejected:
///
/// ```rust,compile_fail
/// use oct::Zeroable;
///
/// #[repr(i32)]
/// #[derive(Zeroable)]
/// enum NoZeroDiscriminant {
/// NegativeTwo = -2,
/// NegativeOne,
/// One = 1,
/// }
/// ```
pub use Zeroable;