From 6aba344b61ce71f78da32a3e8e4559af7500fa9c Mon Sep 17 00:00:00 2001 From: Aditya Pratap Singh Date: Fri, 1 Nov 2024 21:58:20 +0530 Subject: [PATCH] documented how to implement enums with endian-specific tags (#1993) --- src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f2af6f8b9f..8660d3988d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")))]