pub trait TryRawRepr:
ByteRepr
+ TryFrom<Self::Raw>
+ IntoByteArray
+ TryFromByteArray {
type Raw: ByteRepr + From<Self> + IntoByteArray + FromByteArray;
}Expand description
A trait for types that have a corresponding raw representation type with fallible conversion.
This trait is similar to RawRepr, but is designed for types where conversion from
the raw representation might fail. This is particularly useful for enums, where not all
byte patterns represent valid discriminants.
This trait is automatically implemented by the #[derive(Byteable)] macro for enums.
The raw type can be converted to the original type using TryFrom.
§Examples
use byteable::{Byteable, TryRawRepr, TryFromByteArray, IntoByteArray};
#[derive(Clone, Copy, Byteable, Debug, PartialEq)]
#[repr(u8)]
enum Status {
Idle = 0,
Running = 1,
Completed = 2,
}
// Enums automatically implement TryRawRepr
// Converting enum to raw (always succeeds)
let status = Status::Running;
let raw: <Status as TryRawRepr>::Raw = status.into();
// Converting raw back to enum (might fail for invalid discriminants)
let restored: Status = Status::try_from(raw).unwrap();
assert_eq!(restored, Status::Running);
// TryFromByteArray is also implemented for enums
let bytes = status.into_byte_array();
let from_bytes = Status::try_from_byte_array(bytes).unwrap();
assert_eq!(from_bytes, Status::Running);Required Associated Types§
Sourcetype Raw: ByteRepr + From<Self> + IntoByteArray + FromByteArray
type Raw: ByteRepr + From<Self> + IntoByteArray + FromByteArray
The raw type used for byte conversion.
This is typically a #[repr(C, packed)] struct with endianness wrappers
that handles the actual memory layout and byte-level operations.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Implementors§
Source§impl<T: RawRepr> TryRawRepr for T
Blanket implementation of TryRawRepr for types that implement RawRepr.
impl<T: RawRepr> TryRawRepr for T
Blanket implementation of TryRawRepr for types that implement RawRepr.
Types with infallible raw conversion automatically get fallible conversion
with Infallible as the error type (via the blanket TryFrom impl for types implementing From).