SmallExample

Struct SmallExample 

Source
pub struct SmallExample { /* private fields */ }
Expand description

A simple example of using bitstruct_small to define a BitStruct. This is a better example of showing what methods bitstruct_small will generate.

If there is a type path like SmallExample::SmallB, then this inline enum type will be more convenient to use, without considering the name conflict of the enum type. However, this feature is still unstable, so it is not supported for now. issue #8995 https://github.com/rust-lang/rust/issues/8995

使用bitstruct_small定义BitStruct的简单示例。这是为了更好的展示使用bitstruct_small会生成哪些方法。

如果有SmallExample::SmallB这样的类型路径,那么这个内联枚举类型将会更加好用,不用考虑枚举类型的名称冲突。 但是,该feature仍旧是未稳定的,所以暂时不支持。 issue #8995 https://github.com/rust-lang/rust/issues/8995

raw code:

bitstruct_small! {
    pub struct SmallExample {
        /// field `a` description.
        ///
        a: 4,
        /// field `b` description
        b: 2 =>
        #[derive(Debug, PartialEq, Eq)]
        pub enum SmallB {
            B0 = 0,
            B1 = 1,
            B2 = 2,
            B3 = 3,
        },
        /// field `c` description
        c: 2 => TwoBitEnum,
    }
}

Implementations§

Source§

impl SmallExample

Source

pub fn new() -> Self

Source

pub fn from_value(value: u8) -> Self

Source

pub unsafe fn from_ptr(ptr: *const u8) -> *const Self

§Safety
  1. The pointer must be valid and properly aligned.
  2. Other raw pointer rules.
// Recommended usage
let foo = Foo::new();
let ptr = foo.to_ptr();
// ...
// other operations
// ...
let foo2 = unsafe { Foo::from_ptr(ptr) };

// Not recommended usage
let a = 0u8; // assume a is a valid value for Foo
let ptr = &a as *const u8;
let foo = unsafe { Foo::from_ptr(ptr) };
Source

pub unsafe fn from_mut_ptr(ptr: *mut u8) -> *mut Self

§Safety
  1. The pointer must be valid and properly aligned.
  2. Other raw pointer rules.
// Recommended usage
let mut foo = Foo::new();
let ptr = foo.to_mut_ptr();
// ...
// other operations
// ...
let foo2 = unsafe { Foo::from_mut_ptr(ptr) };

// Not recommended usage
let mut a = 0u8; // assume a is a valid value for Foo
let ptr = &mut a as *mut u8;
let foo = unsafe { Foo::from_mut_ptr(ptr) };
Source

pub fn to_ptr(&self) -> *const u8

Source

pub fn to_mut_ptr(&mut self) -> *mut u8

Source

pub fn to_value(self) -> u8

Source

pub fn as_ref(&self) -> &u8

Source

pub fn as_mut(&mut self) -> &mut u8

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.