macro_rules! unsafe_api {
(
$(#![$($attr:tt)*])*
$([$($generics:tt)*])? for $ty:ty $(where { $($where:tt)* })?,
unsafe repr: $repr:tt,
min: $min:expr,
max: $max:expr,
$(zero $([$($_:tt)* $zero:tt])?,)?
$(one $([$($__:tt)* $one:tt])?,)?
) => { ... };
}Expand description
Unsafely provide the bounded integer API for a custom type.
This is used by the higher-level const generic types and by
the bounded_integer! macro. It is preferable to use those APIs when
possible.
Takes in a ty and a repr. ty must be a type whose layout is identical to repr.
min and max are const expressions giving the bounds of the type (inclusive).
If zero is provided, various traits like Default will be implemented;
if one is provided, num_traits::One will be implemented.
§Safety
The given type must be repr(transparent) over its claimed repr.
§Examples
#[repr(transparent)]
struct MyInteger(u8);
bounded_integer::unsafe_api! {
for MyInteger,
unsafe repr: u8,
min: 0,
max: 37,
zero,
one,
}
assert_eq!(MyInteger::new(0).unwrap(), 0);
assert_eq!(MyInteger::new(38), None);§Reprs
repr may either be a primitive integer type, or a descriptor:
unsafe repr: {
type: u32, // a primitive integer type, or something that resolves to one (e.g. c_int)
signed: false,
supersets: [u32, u64, u128, i64, i128], // Types implementing `From<u32>`
non_supersets: [u8, u16, usize, i8, i16, i32, isize], // All the other integer types
has_wide, // Include for all except `u128` and `i128`.
}