#proc-macro #template #ensure #uniform #fields #compile-time #check

ensure-uniform-type

确保类型只使用统一字段

1 个不稳定版本

0.1.1 2024年6月15日
0.1.0 2024年6月15日

#231FFI

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

331 每月下载量
3 crates 中使用

EUPL-1.2

9KB

#[ensure_uniform_type]: 在编译时确保统一结构字段类型

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

一个编译时检查,确保类型在其字段中使用统一类型。

此宏的一个示例用途是确保结构 #[repr(C)] 布局可以正确地映射到(统一)字段类型的切片。

示例

假设以下类型

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

    // Different type
    offending: u32,
}

上面的代码将无法编译,而是给出错误

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 | | }
   | |_^

相比之下,以下代码可以无错误地编译

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

依赖关系

~285–730KB
~18K SLoC