Trait Pod

Source
pub unsafe trait Pod: Sized { }
Expand description

Implementing this trait means that the concrete type is plain old data (POD). Precisely, by implementing Pod the programmer asserts that it is safe to read the type from binary slices provided to read, etc.

Some guidelines for when Pod may be implemented (note that whether Pod should be implemented or not is a function of both the type and the input data. I.e., just because a type is Pod in one context does not mean it should be in another):

  • primitive numeric types (u8, i64, f32, etc.) are fine,
  • bools are fine, if the provided data ensures they may have only the values 0 or 1 (note that this is a stricter requirement that C),
  • structs may be Pod if they have a repr(C) or repr(packed) attribute to prevent rustc from performing field reordering. The former requires that the supplied data has the correct alignment.
  • enums must have valid discriminants in the supplied data, this is probably only feasible if they have a specified representation,
  • there must not be invalid enum variants in the data,
  • any kind of pointer is probably a bad idea. Theoretically one could make raw pointers work.

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.

Implementations on Foreign Types§

Source§

impl Pod for i8

Source§

impl Pod for i16

Source§

impl Pod for i32

Source§

impl Pod for i64

Source§

impl Pod for i128

Source§

impl Pod for u8

Source§

impl Pod for u16

Source§

impl Pod for u32

Source§

impl Pod for u64

Source§

impl Pod for u128

Implementors§