#proc-macro #template #struct-fields

macro ensure-uniform-type

Ensures a type only uses uniform fields

1 unstable release

0.1.1 Jun 15, 2024
0.1.0 Jun 15, 2024

#261 in FFI

Download history 294/week @ 2024-06-15 12/week @ 2024-06-22 394/week @ 2024-06-29 129/week @ 2024-07-06 57/week @ 2024-07-13 20/week @ 2024-07-20 84/week @ 2024-07-27 49/week @ 2024-08-03 23/week @ 2024-08-10 47/week @ 2024-08-17 31/week @ 2024-08-24 21/week @ 2024-08-31 29/week @ 2024-09-07 20/week @ 2024-09-14 21/week @ 2024-09-21 4/week @ 2024-09-28

75 downloads per month
Used in 3 crates

EUPL-1.2

9KB

#[ensure_uniform_type]: Ensure uniform struct field types at compile-time

Crates.io Crates.io GitHub Workflow Status docs.rs

A compile-time check to ensure that a type uses uniform types across its fields.

An example use for this macro is to ensure that a struct #[repr(C)] layout can be correctly mapped onto a slice of the (uniform) field type.

Example

Assume the following type:

#[ensure_uniform_type]
pub struct Example<T>
{
    /// First field
    x: T,

    // Different type
    offending: u32,
}

The above would fail to compile, instead giving the error:

error: Struct DifferentialDriveState has fields of different types. Expected uniform use of T, found u32 in field offending.
  --> src/differential_drive.rs:16:1
   |
16 | / /// A state of a differential drive robot, or differential wheeled robot.
18 | | #[ensure_uniform_type]
19 | | pub struct Example<T>
...  |
37 | |     offending: u32,
38 | | }
   | |_^

By contrast, the following would compile without an error:

#[ensure_uniform_type]
pub struct Example<T>
{
    x: T,
    not_offending: T,
}

Dependencies

~235–670KB
~16K SLoC