Trait CLike

Source
pub trait CLike {
    // Required methods
    fn to_u32(&self) -> u32;
    unsafe fn from_u32(_: u32) -> Self;
}
Expand description

An interface for casting C-like enum to u32 and back.

The returned value must be no more than 31: EnumSet does not support more cases than this.

A typical implementation can be seen below:

use enum_set::CLike;
use std::mem;

#[derive(Clone, Copy)]
#[repr(u32)]
enum Foo {
    A, B, C
}

impl CLike for Foo {
    fn to_u32(&self) -> u32 {
        *self as u32
    }

    unsafe fn from_u32(v: u32) -> Foo {
        mem::transmute(v)
    }
}

Required Methods§

Source

fn to_u32(&self) -> u32

Converts a C-like enum to a u32. The value must be <= 31.

Source

unsafe fn from_u32(_: u32) -> Self

Converts a u32 to a C-like enum. This method only needs to be safe for possible return values of to_u32 of this trait.

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§