autoimpl

Crates.ioautoimpl
lib.rsautoimpl
version0.1.0
sourcesrc
created_at2017-04-12 20:39:47.856714
updated_at2017-04-12 20:39:47.856714
descriptionThe user-facing part of the autoimpl macro. Automatically implement a generic trait using autoimpl!
homepage
repositoryhttps://github.com/blakepettersson/autoimpl
max_upload_size
id10434
size1,619
Blake Pettersson (blakepettersson)

documentation

README

DRY blanket implementations for generic traits!

Do you have a generic trait? Do you feel that adding a blanket impl for it is unnecessary repetition? Then this is the macro for you! This is a macro based on proc-macro-hack that helps to reduce some of the repetition, which works with any Rust version >= 1.15.0.

The autoimpl! macro generates a default blanket impl for a generic trait for all T with the same bounds as the trait passed into the autoimpl! block.

Caveats

  • This only works for traits with type parameters
  • You can only pass in one trait per autoimpl! block
  • Only a single type parameter is currently supported
  • Currently, due to a limitation in proc-macro-hack, you can only have one autoimpl! block per module.

Example

    #[macro_use] extern crate autoimpl;

    struct Dog {}

    struct Duck {}

    fn main() {
        autoimpl! {
            trait Quack<T> {
                fn say(&self) -> String {
                    "quack".to_string()
                }
            }
        }
        
        let dog = Dog {};
        let duck = Duck {};
        assert_eq!(dog.say(), duck.say());
    }
Commit count: 4

cargo fmt