pub struct Opaque(/* private fields */);Expand description
An opaque integer type, used to represent enum variants,
usize and isize.
Supports converting to and from all integer primitive types.
Conversion errors where the value wouldn’t fit in the requested
type, are coerced to EncodingErrors
Beware of providing an Opaque with an integer literal without a specific type!
By default, rust will infer i32 as the type, thus it will be converted to a Signed
enum variant value, and you will get a (bad) surprise when you try to then encode/decode
an enum that uses Unsigned variant discriminants (most enums).
How to avoid this?
- Write the type in the num literal E.G.
Opaque::from(3u32)orOpaque::from(3 as u32) - Better yet, use an explicit opaque value (
Opaque::signed,Opaque::unsigned)
Implementations§
Source§impl Opaque
impl Opaque
Sourcepub fn signed<V>(value: V) -> Selfwhere
V: Sign<Sign = Signed>,
Self: From<V>,
pub fn signed<V>(value: V) -> Selfwhere
V: Sign<Sign = Signed>,
Self: From<V>,
Constructs a new opaque integer, enforcing at compile time that the number type is signed
Sourcepub fn unsigned<V>(value: V) -> Selfwhere
V: Sign<Sign = Unsigned>,
Self: From<V>,
pub fn unsigned<V>(value: V) -> Selfwhere
V: Sign<Sign = Unsigned>,
Self: From<V>,
Constructs a new opaque integer, enforcing at compile time that the number type is unsigned
Sourcepub fn sign(&self) -> Signedness
pub fn sign(&self) -> Signedness
Returns the Signedness of the opaque integer.