#bounds #attributes #attr #apply #items #macro

构建 attr-bounds

一个应用于项的属性宏

1 个不稳定版本

0.1.0 2024年6月12日

287构建工具

Download history 153/week @ 2024-06-11 4/week @ 2024-06-18

157 每月下载量

MIT/Apache

10KB
109 代码行

attr-bounds

一个指定边界的属性宏。

该属性将边界应用于结构体、枚举、联合、特质、函数和impl块。

use attr_bounds::bounds;

#[bounds(T: Copy)]
pub struct Wrapper<T>(T);

let var = Wrapper(42);

使用说明

基本上,该属性是为了条件编译而设计的,否则您不需要该属性。

use attr_bounds::bounds;

#[cfg(feature = "unstable_feature_a")]
pub trait UnstableA {}
#[cfg(feature = "unstable_feature_b")]
pub trait UnstableB {}

#[cfg_attr(feature = "unstable_feature_a", bounds(Self: UnstableA))]
#[cfg_attr(feature = "unstable_feature_b", bounds(Self: UnstableB))]
pub trait Trait {}

#[cfg(feature = "unstable_feature_a")]
impl UnstableA for () {}
#[cfg(feature = "unstable_feature_b")]
impl UnstableB for () {}

impl Trait for () {}

lib.rs:

一个指定边界的属性宏。

该属性将边界应用于结构体、枚举、联合、特质、函数、关联类型和impl块。

use attr_bounds::bounds;

#[bounds(T: Copy)]
pub struct Wrapper<T>(T);

let var = Wrapper(42);
use attr_bounds::bounds;

#[bounds(T: Copy)]
pub struct Wrapper<T>(T);

let var = Wrapper(Vec::<i32>::new());
//                ^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Vec<i32>`

使用说明

基本上,该属性是为了条件编译而设计的,否则您不需要该属性。

use attr_bounds::bounds;

#[cfg(feature = "unstable_feature_a")]
pub trait UnstableA {}
#[cfg(feature = "unstable_feature_b")]
pub trait UnstableB {}

#[cfg_attr(feature = "unstable_feature_a", bounds(Self: UnstableA))]
#[cfg_attr(feature = "unstable_feature_b", bounds(Self: UnstableB))]
pub trait Trait {}

#[cfg(feature = "unstable_feature_a")]
impl UnstableA for () {}
#[cfg(feature = "unstable_feature_b")]
impl UnstableB for () {}

impl Trait for () {}

依赖项

~270–720KB
~17K SLoC