Skip to content

Commit 15a2cf5

Browse files
documented how to implement enums with endian-specific tags (#1993) (#2390)
Co-authored-by: Aditya Pratap Singh <[email protected]>
1 parent 47949a4 commit 15a2cf5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,26 @@ pub unsafe trait Immutable {
12851285
/// }
12861286
/// ```
12871287
///
1288+
/// # Portability
1289+
///
1290+
/// To ensure consistent endianness for enums with multi-byte representations,
1291+
/// explicitly specify and convert each discriminant using `.to_le()` or
1292+
/// `.to_be()`; e.g.:
1293+
///
1294+
/// ```
1295+
/// # use zerocopy_derive::TryFromBytes;
1296+
/// // `DataStoreVersion` is encoded in little-endian.
1297+
/// #[derive(TryFromBytes)]
1298+
/// #[repr(u32)]
1299+
/// pub enum DataStoreVersion {
1300+
/// /// Version 1 of the data store.
1301+
/// V1 = 9u32.to_le(),
1302+
///
1303+
/// /// Version 2 of the data store.
1304+
/// V2 = 10u32.to_le(),
1305+
/// }
1306+
/// ```
1307+
///
12881308
/// [safety conditions]: trait@TryFromBytes#safety
12891309
#[cfg(any(feature = "derive", test))]
12901310
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]

0 commit comments

Comments
 (0)