Crates.io | ensure-uniform-type |
lib.rs | ensure-uniform-type |
version | 0.1.1 |
source | src |
created_at | 2024-06-15 14:22:40.031415 |
updated_at | 2024-06-15 14:39:49.607082 |
description | Ensures a type only uses uniform fields |
homepage | https://github.com/sunsided/ensure-uniform-type-rs |
repository | https://github.com/sunsided/ensure-uniform-type-rs |
max_upload_size | |
id | 1272926 |
size | 22,933 |
#[ensure_uniform_type]
: Ensure uniform struct field types at compile-timeA 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.
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,
}