Skip to content

Commit

Permalink
documented how to implement enums with endian-specific tags (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya-PS-05 authored Nov 1, 2024
1 parent e9e8cfc commit 6aba344
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,26 @@ pub unsafe trait Immutable {
/// }
/// ```
///
/// # Portability
///
/// To ensure consistent endianness for enums with multi-byte representations,
/// explicitly specify and convert each discriminant using `.to_le()` or
/// `.to_be()`; e.g.:
///
/// ```
/// # use zerocopy_derive::TryFromBytes;
/// // `DataStoreVersion` is encoded in little-endian.
/// #[derive(TryFromBytes)]
/// #[repr(u32)]
/// pub enum DataStoreVersion {
/// /// Version 1 of the data store.
/// V1 = 9u32.to_le(),
///
/// /// Version 2 of the data store.
/// V2 = 10u32.to_le(),
/// }
/// ```
///
/// [safety conditions]: trait@TryFromBytes#safety
#[cfg(any(feature = "derive", test))]
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
Expand Down

0 comments on commit 6aba344

Please sign in to comment.