#self #duplicate #signature #box #macro #consume

macro box-self

Easy way to duplicate a new function with self: Box<Self> signature

6 releases

0.1.5 Aug 24, 2023
0.1.4 Aug 24, 2023
0.1.3 Oct 7, 2022

#482 in Procedural macros

MIT/Apache

11KB
72 lines

box-self

Easy way to duplicate a new function with self: Box<Self> signature.

Sometimes you need both functions fn consume(self) and fn consume_boxed(self: Box<Self>). This macro generates the second one for you.

Examples

   use box_self::box_self;

   trait Animal {
       fn consume(self);
       fn consume_boxed(self: Box<Self>);
   }

   struct Dog{}
   impl Animal for Dog{
       #[box_self(_boxed)]
       fn consume(self) {
           println!("Bark");
       }
   }

   struct Cat{}
   impl Animal for Cat{
       #[box_self(_boxed)]
       fn consume(self) {
           println!("Jump");
       }
   }

   fn main(){
       let animals:Vec<Box<dyn Animal>>=
            vec![Box::new(Dog{}), Box::new(Cat{})];

       for anim in animals{
           anim.consume_boxed();
       }
   }



Motivation:


License

Licensed under either of LICENSE-APACHE or LICENSE-MIT at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

License: MIT OR Apache-2.0

Dependencies

~3–11MB
~91K SLoC