Trait Id

Source
pub unsafe trait Id:
    Copy
    + Clone
    + Debug
    + Eq
    + Into<u32>
    + PartialEq {
    // Required methods
    unsafe fn new_unchecked(raw_id: u32) -> Self;
    fn cardinality() -> u32;

    // Provided methods
    fn new(raw_id: u32) -> Self { ... }
    fn as_usize(self) -> usize { ... }
}
Expand description

A trait to abstract an ID type.

§Safety

There must be a 1:1 mapping between this ID type and the integers from 0 to Self::cardinality() (exclusive). This implies that the implementation must ensure that if one invokes Into::<u32>::into() for an Id value, then the returned integer always falls within 0..Id::cardinality(). Furthermore, the implementation must ensure that for any id_value of type MyId: Id, the following assertion always succeed

assert!(id_value == MyId::new(id_value.into()));

There are also constraints on the implementation of self::cardinality. For one thing, the cardinality of the ID type must not change, i.e., different calls to self::cardinality return the same value. In addition, the value of a cardinality must be greater than zero.

Required Methods§

Source

unsafe fn new_unchecked(raw_id: u32) -> Self

Creates an ID instance given a raw ID number.

§Safety

The given number must be less than Self::cardinality().

Source

fn cardinality() -> u32

The number of unique IDs representable by this type.

Provided Methods§

Source

fn new(raw_id: u32) -> Self

Creates an ID instance given a raw ID number.

§Panics

The given number must be less than Self::cardinality(). Otherwise, this method would panic.

Source

fn as_usize(self) -> usize

Returns an [usize] from the Id’s corresponding [u32].

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.

Implementors§

Source§

impl Id for CpuId