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
impl SmallExample
pub fn new() -> Self
pub fn from_value(value: u8) -> Self
Sourcepub unsafe fn from_ptr(ptr: *const u8) -> *const Self
pub unsafe fn from_ptr(ptr: *const u8) -> *const Self
§Safety
- The pointer must be valid and properly aligned.
- 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) };Sourcepub unsafe fn from_mut_ptr(ptr: *mut u8) -> *mut Self
pub unsafe fn from_mut_ptr(ptr: *mut u8) -> *mut Self
§Safety
- The pointer must be valid and properly aligned.
- 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) };pub fn to_ptr(&self) -> *const u8
pub fn to_mut_ptr(&mut self) -> *mut u8
pub fn to_value(self) -> u8
pub fn as_ref(&self) -> &u8
pub fn as_mut(&mut self) -> &mut u8
Auto Trait Implementations§
impl Freeze for SmallExample
impl RefUnwindSafe for SmallExample
impl Send for SmallExample
impl Sync for SmallExample
impl Unpin for SmallExample
impl UnwindSafe for SmallExample
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more