#slice #unsafe #uniform #type #derive #structs #behavior

macro no-std uniform-array-derive

A derive macro for turning uniform types into slices

1 unstable release

0.1.0 Jul 3, 2024

#1701 in Procedural macros

Download history 492/week @ 2024-07-01 99/week @ 2024-07-08 13/week @ 2024-07-15 24/week @ 2024-07-22 53/week @ 2024-07-29 49/week @ 2024-08-05 22/week @ 2024-08-12 45/week @ 2024-08-19 37/week @ 2024-08-26 8/week @ 2024-09-02 26/week @ 2024-09-09 33/week @ 2024-09-16 13/week @ 2024-09-23 14/week @ 2024-09-30

86 downloads per month
Used in 2 crates

EUPL-1.2

15KB
217 lines

uniform-array-derive

Derive array-like behavior for your structs of uniform types.

Crates.io Docs Build Status Safety Dance MSRV EUPL 1.2 licensed

This will derive AsRef<[T]>, AsMut<[T]>, Deref<Target = [T]>, DerefMut, Index<Target = T>andIndexMutfor your homogeneous structs field typeT. Since the generated code is unsafe, it is feature gated behind a #[cfg(feature = "unsafe")]` by default.

#[derive(UniformArray)]
pub struct Vector3 {
    pub x: f32,
    pub y: f32,
    pub z: f32,
}

#[derive(UniformArray)]
pub struct Coordinate(u32, u32);

If you need to rename the feature gate, you can do that as well:

/// Use a custom feature gate instead of "unsafe".
#[derive(UniformArray)]
#[uniform_array(safety_gate = "super_unsafe")]
pub struct Quaternion<T>
where
    T: Sized,
{
    pub a: T,
    pub b: T,
    pub c: T,
    pub d: T,
}

If you only need to ensure that all your fields have the same type, consider the ensure-uniform-type crate instead.

Dependencies

~0.6–1MB
~23K SLoC