#bounds #attributes

macro build attr-bounds

An attribute macro to apply bounds to items

1 unstable release

0.1.0 Jun 12, 2024

#486 in Build Utils

MIT/Apache

10KB
109 lines

attr-bounds

An attribute macro to stipulate bounds.

The attribute applies bounds to structs, enums, unions, traits, fns, and impl blocks.

use attr_bounds::bounds;

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

let var = Wrapper(42);

Usage notes

Basically, the attribute is designed to be used for conditional compilation and otherwise you will not need the attribute.

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:

An attribute macro to stipulate bounds.

The attribute applies bounds to structs, enums, unions, traits, fns, associated types, and impl blocks.

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>`

Usage notes

Basically, the attribute is designed to be used for conditional compilation and otherwise you will not need the attribute.

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 () {}

Dependencies

~230–680KB
~16K SLoC