wraps

Crates.iowraps
lib.rswraps
version1.0.0
created_at2025-11-04 08:05:54.956076+00
updated_at2025-11-04 08:05:54.956076+00
descriptioncreate marker types that wrap other, inner, types - with trait impls matching PhantomData
homepagehttps://gitlab.com/infomorphic-matti/wraps
repositoryhttps://gitlab.com/infomorphic-matti/wraps
max_upload_size
id1915896
size25,707
Matti (infomorphic-matti)

documentation

https://docs.rs/wraps

README

wraps

Simple Rust macro for creating marker types that also encode some "inner type information" as a type argument - essentially like PhantomData - and that implement traits such as Default and Debug for all type arguments rather than just type arguments that themselves implement the relevant traits (as would occur if you used the #[derive] annotation).

Designed for use in creating wrapper types for use inside marker types in a more advanced version of the pattern observed in bevy_ecs, where wrapper types are used to disambiguate relevant traits or properties of some subcomponent of the marker type.

You could also create wrapper types fairly simply without any macro but it's slightly more cumbersome - and if you want the structure to be exactly like PhantomData - for example, having universal implementations of traits like Debug, Copy and Clone that don't require the type argument to implement those traits - then it takes a lot of boilerplate code that this crate will create for you automatically.

Example Usage

trait PropertyA {
    fn a(&self);
}
trait PropertyB {
    fn b(&self);
}

mod marker {
    wraps::marker! {
        /// Interpret type as implementing [`super::PropertyA`], inner field 
        /// is public.
        pub(crate) struct PropertyA;
        /// Interpret type as implementing [`super::PropertyB`], inner field 
        /// is not public.
        pub struct PropertyB<MyTypeName>;
        /// Interpret type as mysterious property C with default type being `()`, 
        /// inner field is public.
        pub struct PropertyC<pub Weird = ()>;
    }
}

pub trait AnswerMyQuestion<M> {
    fn emit_properties(&self);
}

impl <T: PropertyA, Q: PropertyB> 
    AnswerMyQuestion<(marker::PropertyA<T>, marker::PropertyB<Q>)> for (T, Q) 
{
    fn emit_properties(&self) {
        self.0.a(); 
        self.1.b();
    }    
} 


impl <T: PropertyB, Q: PropertyA> 
    AnswerMyQuestion<(marker::PropertyB<T>, marker::PropertyA<Q>)> for (T, Q) 
{
    fn emit_properties(&self) {
        self.0.b(); 
        self.1.a();
    }    
} 

License

This crate is licensed under MPL-2.0.

Commit count: 0

cargo fmt