[][src]Trait typelayout::FromZeros

pub unsafe trait FromZeros {
    fn zeroed() -> Self
    where
        Self: Sized
, { ... } }

Working Example

This marker trait is implemented for structs iff it is ReprC, has no padding bytes, and all fields are also FromZeros:

use typelayout::{ReprC, Generic, Layout, FromZeros};

#[derive(Generic, Default, Debug, PartialEq)]
#[repr(C)]
pub struct Struct {
  first: u8,
  second: u8,
}

unsafe impl ReprC for Struct {}

assert_eq!(<Struct as Default>::default(), <Struct as FromZeros>::zeroed());

Failing Example

This marker trait is not implemented if a type has padding:

This example deliberately fails to compile
use typelayout::{ReprC, Generic, Layout, FromZeros};

#[derive(Generic, Default, Debug, PartialEq)]
#[repr(C)]
pub struct Struct {
  first: u8,
  second: u16, // padding will be inserted between `first` and `second`
}

unsafe impl ReprC for Struct {}

// `Struct` does not implement `FromZeros`, because it has a padding byte!
assert_eq!(<Struct as Default>::default(), <Struct as FromZeros>::zeroed());

Provided methods

fn zeroed() -> Self where
    Self: Sized

Initialize an instance of Self from zeroed bytes.

Loading content...

Implementations on Foreign Types

impl<T> FromZeros for *const T[src]

A *const T from zeros is a null pointer.

impl<T> FromZeros for *mut T[src]

A *mut T from zeros is a null pointer.

impl FromZeros for i8[src]

impl FromZeros for i16[src]

impl FromZeros for i32[src]

impl FromZeros for i64[src]

impl FromZeros for i128[src]

impl FromZeros for isize[src]

impl FromZeros for u8[src]

impl FromZeros for u16[src]

impl FromZeros for u32[src]

impl FromZeros for u64[src]

impl FromZeros for u128[src]

impl FromZeros for usize[src]

impl FromZeros for f32[src]

impl FromZeros for f64[src]

Loading content...

Implementors

impl<T: Generic + ReprC> FromZeros for T where
    T: NoPadding,
    Struct<Self::Repr>: FromZeros
[src]

Loading content...