#macro-derive #traits #proc-macro #proc-macro-attributes #impl #attributes

macro blanket

A simple macro to derive blanket implementations for your traits

8 unstable releases (3 breaking)

0.4.0 Mar 4, 2024
0.3.0 Jun 19, 2023
0.2.0 Jun 5, 2021
0.1.5 May 30, 2021
0.1.2 Jul 23, 2020

#246 in Rust patterns

Download history 2647/week @ 2024-07-22 3827/week @ 2024-07-29 3887/week @ 2024-08-05 3556/week @ 2024-08-12 3598/week @ 2024-08-19 3940/week @ 2024-08-26 4455/week @ 2024-09-02 4143/week @ 2024-09-09 4528/week @ 2024-09-16 5452/week @ 2024-09-23 6308/week @ 2024-09-30 3995/week @ 2024-10-07 4638/week @ 2024-10-14 5194/week @ 2024-10-21 5344/week @ 2024-10-28 4847/week @ 2024-11-04

20,264 downloads per month
Used in 112 crates (7 directly)

MIT license

86KB
2K SLoC

๐Ÿงฃ blanket Star me

A simple macro to derive blanket implementations for your traits.

Actions Codecov License Source Crate Documentation Changelog GitHub issues

๐Ÿ” Overview

The Rust standard library has plenty of traits, but they shine in how well they integrate with new types. Declare an implementation of std::io::Write for a type W, and you also get it for &mut W and Box<W>! This however translates into a lot of boilerplate code that can be hard to maintain, which is why many crates don't bother providing the same convenience implementations.

This is where blanket comes in! This crate helps you build the same kind of blanket implementations for your own traits with as least additional code as possible: in fact, this is as close as what a derive macro would look like for a trait item.

๐Ÿ”Œ Usage

blanket exports a single eponymous attribute macro, which can be imported simply after the crate has been added to the Cargo.toml dependencies:

extern crate blanket;
use blanket::blanket;

#[blanket(derive(...))]

Use this macro attribute to derive a blanket implementation for a trait, provided the trait methods fit the constraints for that derive, such as only declaring methods with &self of &mut self as their receiver. The following derives are available:

Derive Impl block fn (&self) fn (&mut self) fn (self)
Ref impl<T: Trait + ?Sized> Trait for &T โœ”๏ธ
Rc impl<T: Trait + ?Sized> Trait for Rc<T> โœ”๏ธ
Arc impl<T: Trait + ?Sized> Trait for Arc<T> โœ”๏ธ
Mut impl<T: Trait + ?Sized> Trait for &mut T โœ”๏ธ โœ”๏ธ
Boxยน impl<T: Trait + ?Sized> Trait for Box<T> โœ”๏ธ โœ”๏ธ
Boxยฒ impl<T: Trait> Trait for Box<T> โœ”๏ธ โœ”๏ธ โœ”๏ธ
Cow impl<T: Trait + ToOwned + ?Sized> Trait for Cow<_, T> โœ”๏ธ

For instance, with our own version of std::fmt::Write, we can provide an implementation for Box<impl Write> and &mut impl Write:

extern crate blanket;
use blanket::blanket;

#[blanket(derive(Mut, Box))]
pub trait Write {
    fn write_str(&mut self, s: &str) -> std::fmt::Result;
    fn write_char(&mut self, c: char) -> std::fmt::Result {
         self.write_str(c.encode_utf8(&mut [0; 4]))
    }
}

Note that we can't derive Ref because the Write trait we declared expects mutable references, which we can't provide from an immutable reference. If we were to try, the compiler would warn us:

---- src/lib.rs - (line 55) stdout ----
error: cannot derive `Ref` for a trait declaring `&mut self` methods
 --> src/lib.rs:61:18
  |
8 |     fn write_str(&mut self, s: &str) -> std::fmt::Result;
  |                  ^^^^^^^^^

#[blanket(default = "...")]

blanket can delegate default implementations of trait methods to functions of another module. This can be useful for some traits such as visitors to provide a default behaviour as an external function, such as what syn::visit is doing.

The following example implements a very simple visitor trait for types able to process a &str char-by-char.

extern crate blanket;
use blanket::blanket;

#[blanket(default = "visitor")]
trait Visitor {
    fn visit_string(&self, s: &str);
    fn visit_char(&self, c: char);
}

mod visitor {
    use super::Visitor;

    pub fn visit_string<V: Visitor + ?Sized>(v: &V, s: &str) {
        for c in s.chars() {
            v.visit_char(c);
        }
    }

    pub fn visit_char<V: Visitor + ?Sized>(v: &V, c: char) {}
}

blanket will check that all methods are declared without a default block, and then create a default implementation for all of the declared methods, generating the following code:

trait Visitor {
    fn visit_string(&self, s: &str) {
      visitor::visit_string(self, s)
    }
    fn visit_char(&self, c: char) {
      visitor::visit_char(self, c)
    }
}

๐Ÿ“ To-Do

  • โœ“ Delegation of default method to external functions.
  • โœ“ Support for traits with generic arguments.
  • โœ“ #[derive(Ref)]
  • โœ“ #[derive(Mut)]
  • โœ“ #[derive(Box)] for both sized and unsized types.
  • โœ“ #[derive(Rc)]
  • โœ“ #[derive(Arc)]
  • โœ“ #[derive(Cow)]

๐Ÿค Credits

blanket is developed and maintained by:

The following people contributed to the project:

๐Ÿ“‹ Changelog

This project adheres to Semantic Versioning and provides a changelog in the Keep a Changelog format.

๐Ÿ“œ License

This library is provided under the open-source MIT license.

Dependencies

~240โ€“690KB
~16K SLoC