https://github.com/sunsided/ensure-uniform-type-rs
A compile-time check to ensure that a type uses uniform types across its fields.
https://github.com/sunsided/ensure-uniform-type-rs
procedural-macro rust
Last synced: about 2 months ago
JSON representation
A compile-time check to ensure that a type uses uniform types across its fields.
- Host: GitHub
- URL: https://github.com/sunsided/ensure-uniform-type-rs
- Owner: sunsided
- License: eupl-1.2
- Created: 2024-06-15T14:16:05.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-06-15T14:39:43.000Z (12 months ago)
- Last Synced: 2025-03-09T08:51:53.533Z (3 months ago)
- Topics: procedural-macro, rust
- Language: Rust
- Homepage: https://crates.io/crates/ensure-uniform-type
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# `#[ensure_uniform_type]`: Ensure uniform struct field types at compile-time
[](https://crates.io/crates/ensure-uniform-type)
[](https://crates.io/crates/ensure-uniform-type)

[](https://docs.rs/ensure-uniform-type/)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:
```rust
#[ensure_uniform_type]
pub struct Example
{
/// 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
... |
37 | | offending: u32,
38 | | }
| |_^
```By contrast, the following would compile without an error:
```rust
#[ensure_uniform_type]
pub struct Example
{
x: T,
not_offending: T,
}
```