pub unsafe trait BufferContents: Send + Sync + 'static {
    const LAYOUT: BufferContentsLayout;
}
Expand description

Trait for types of data that can be put in a buffer.

This trait is not intended to be implemented manually (ever) and attempting so will make you one sad individual very quickly. Rather you should use the derive macro. Note also that there are blanket implementations of this trait: you don’t need to implement it if the type in question already implements bytemuck’s AnyBitPattern. Most if not all linear algebra crates have a feature flag that you can enable for bytemuck support. The trait is also already implemented for all slices where the element type implements BufferContents.

Examples

Deriving the trait for sized types:

#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    x: f32,
    y: f32,
    array: [i32; 12],
}

Deriving the trait for unsized types works the same:

#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    x: f32,
    y: f32,
    slice: [i32],
}

This even works if the last field is a user-defined DST too:

#[derive(BufferContents)]
#[repr(C)]
struct MyData {
    x: f32,
    y: f32,
    other: OtherData,
}

#[derive(BufferContents)]
#[repr(C)]
struct OtherData {
    slice: [i32],
}

You can also use generics if you please:

#[derive(BufferContents)]
#[repr(C)]
struct MyData<T, U> {
    x: T,
    y: T,
    slice: [U],
}

This even works with dependently-sized types:

#[derive(BufferContents)]
#[repr(C)]
struct MyData<T>
where
    T: ?Sized,
{
    x: f32,
    y: f32,
    z: T,
}

Required Associated Constants§

source

const LAYOUT: BufferContentsLayout

The layout of the contents.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T> BufferContents for [T]where T: BufferContents,

Implementors§