Skip to main content

Pod

Derive Macro Pod 

Source
#[derive(Pod)]
{
    // Attributes available to this derive:
    #[hexga_bit]
}
Expand description

Derive the Pod trait for a struct

The macro ensures that the struct follows all the the safety requirements for the Pod trait.

The following constraints need to be satisfied for the macro to succeed

  • All fields in the struct must implement Pod
  • The struct must be #[repr(C)] or #[repr(transparent)]
  • The struct must not contain any padding bytes
  • The struct contains no generic parameters, if it is not #[repr(transparent)]

§Examples

use std::marker::PhantomData;
use hexga_bit_derive::{Pod, BitZero};
#[derive(Copy, Clone, Pod, BitZero)]
#[repr(C)]
struct Test {
  a: u16,
  b: u16,
}

#[derive(Copy, Clone, Pod, BitZero)]
#[repr(transparent)]
struct Generic<A, B> {
  a: A,
  b: PhantomData<B>,
}

If the struct is generic, it must be #[repr(transparent)] also.

use hexga_bit::{Pod, BitZero};
use std::marker::PhantomData;
#[derive(Copy, Clone, Pod, BitZero)]
#[repr(C)] // must be `#[repr(transparent)]`
struct Generic<A> {
    a: A,
}

If the struct is generic and #[repr(transparent)], then it is only Pod when all of its generics are Pod, not just its fields.

use hexga_bit::{Pod, BitZero};
use hexga_bit_derive::{Pod, BitZero};
use std::marker::PhantomData;
#[derive(Copy, Clone, Pod, BitZero)]
#[repr(transparent)]
struct Generic<A, B> {
    a: A,
    b: PhantomData<B>,
}

let _: u32 = hexga_bit::transmute(Generic { a: 4u32, b: PhantomData::<u32> });
use hexga_bit::{Pod, BitZero};
use std::marker::PhantomData;
#[derive(Copy, Clone, Pod, BitZero)]
#[repr(transparent)]
struct Generic<A, B> {
    a: A,
    b: PhantomData<B>,
}
struct NotPod;

let _: u32 = hexga_bit::transmute(Generic { a: 4u32, b: PhantomData::<NotPod> });