macro_rules! include_slice {
    ($target_ty:ty, $file:expr $(,)?) => { ... };
}
Expand description

Include data from a file as static data, consisting of a slice of bytemuck::Pod types.

For any type T: bytemuck::Pod, include_slice!(T, path) will return a &'static [T] slice containing the contents of the file at path.

A compiler error will be thrown source file cannot fit evenly into a &[T] slice. That is, if the file size is not divisible by size_of::<T>(). The path is interpreted by core::include_bytes and is host-platform-specific.

While include_slice!(u8, path) is supported, core::include_bytes should be preferred in almost every case as it is a compiler built-in.

Why do I have to specify the type twice?

In order to ensure alignment, these macros internally create a static value with the same alignment as the target type, which the compiler copies the data into at compile-time. Since this is a static value, the type used for alignment must be explicitly specified and cannot be inferred.

Example

static DATA_U32: &[u32] = include_slice!(u32, "../tests/test_data/binary_32");

Safety

This macro is safe. However, if used on a custom type, that type must implement bytemuck::Pod. Implementing that trait has very struct safety requirements which must be observed.