| 1 | # ๐งฃ `blanket` [](https://github.com/althonos/blanket/stargazers) |
| 2 | |
| 3 | *A simple macro to derive blanket implementations for your traits.* |
| 4 | |
| 5 | [](https://github.com/althonos/blanket/actions) |
| 6 | [](https://codecov.io/gh/althonos/blanket) |
| 7 | [](https://choosealicense.com/licenses/mit/) |
| 8 | [](https://github.com/althonos/blanket) |
| 9 | [](https://crates.io/crates/blanket) |
| 10 | [](https://docs.rs/blanket) |
| 11 | [](https://github.com/althonos/blanket.rs/blob/master/CHANGELOG.md) |
| 12 | [](https://github.com/althonos/blanket/issues) |
| 13 | |
| 14 | ## ๐ Overview |
| 15 | |
| 16 | The Rust standard library has plenty of traits, but they shine in how well |
| 17 | they integrate with new types. Declare an implementation of |
| 18 | [`std::io::Write`] for |
| 19 | a type `W`, and you also get it for [`&mut W`] and [`Box<W>`]! This however |
| 20 | translates into a [lot of boilerplate code] |
| 21 | that can be hard to maintain, which is why many crates don't bother |
| 22 | providing the same convenience implementations. |
| 23 | |
| 24 | [`std::io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html |
| 25 | [lot of boilerplate code]: https://doc.rust-lang.org/src/std/io/impls.rs.html#49-79 |
| 26 | [`&mut W`]: https://doc.rust-lang.org/std/io/trait.Write.html#impl-Write-17 |
| 27 | [`Box<W>`]: https://doc.rust-lang.org/std/io/trait.Write.html#impl-Write-19 |
| 28 | |
| 29 | This is where `blanket` comes in! This crate helps you build the same kind |
| 30 | of blanket implementations for your own traits with as least additional code |
| 31 | as possible: in fact, this is as close as what a `derive` macro would look |
| 32 | like for a `trait` item. |
| 33 | |
| 34 | ## ๐ Usage |
| 35 | |
| 36 | `blanket` exports a single eponymous attribute macro, which can be imported |
| 37 | simply after the crate has been added to the `Cargo.toml` dependencies: |
| 38 | |
| 39 | ```rust |
| 40 | extern crate blanket; |
| 41 | use blanket::blanket; |
| 42 | ``` |
| 43 | |
| 44 | ### `#[blanket(derive(...))]` |
| 45 | |
| 46 | Use this macro attribute to derive a blanket implementation for a trait, |
| 47 | provided the trait methods fit the constraints for that derive, such as |
| 48 | only declaring methods with `&self` of `&mut self` as their receiver. |
| 49 | The following derives are available: |
| 50 | |
| 51 | | Derive | Impl block | `fn (&self)` | `fn (&mut self)` | `fn (self)` | |
| 52 | |--------|---------------------------------------------------------|--------------|------------------|-------------| |
| 53 | | Ref | `impl<T: Trait + ?Sized> Trait for &T` | โ๏ธ | | | |
| 54 | | Rc | `impl<T: Trait + ?Sized> Trait for Rc<T>` | โ๏ธ | | | |
| 55 | | Arc | `impl<T: Trait + ?Sized> Trait for Arc<T>` | โ๏ธ | | | |
| 56 | | Mut | `impl<T: Trait + ?Sized> Trait for &mut T` | โ๏ธ | โ๏ธ | | |
| 57 | | Boxยน | `impl<T: Trait + ?Sized> Trait for Box<T>` | โ๏ธ | โ๏ธ | | |
| 58 | | Boxยฒ | `impl<T: Trait> Trait for Box<T>` | โ๏ธ | โ๏ธ | โ๏ธ | |
| 59 | | Cow | `impl<T: Trait + ToOwned + ?Sized> Trait for Cow<_, T>` | โ๏ธ | | | |
| 60 | |
| 61 | For instance, with our own version of `std::fmt::Write`, we can provide |
| 62 | an implementation for `Box<impl Write>` and `&mut impl Write`: |
| 63 | |
| 64 | ```rust |
| 65 | extern crate blanket; |
| 66 | use blanket::blanket; |
| 67 | |
| 68 | #[blanket(derive(Mut, Box))] |
| 69 | pub trait Write { |
| 70 | fn write_str(&mut self, s: &str) -> std::fmt::Result; |
| 71 | fn write_char(&mut self, c: char) -> std::fmt::Result { |
| 72 | self.write_str(c.encode_utf8(&mut [0; 4])) |
| 73 | } |
| 74 | } |
| 75 | ``` |
| 76 | |
| 77 | Note that we can't derive `Ref` because the `Write` trait we declared expects |
| 78 | mutable references, which we can't provide from an immutable reference. If we |
| 79 | were to try, the compiler would warn us: |
| 80 | |
| 81 | ```rustc |
| 82 | ---- src/lib.rs - (line 55) stdout ---- |
| 83 | error: cannot derive `Ref` for a trait declaring `&mut self` methods |
| 84 | --> src/lib.rs:61:18 |
| 85 | | |
| 86 | 8 | fn write_str(&mut self, s: &str) -> std::fmt::Result; |
| 87 | | ^^^^^^^^^ |
| 88 | ``` |
| 89 | |
| 90 | |
| 91 | ### `#[blanket(default = "...")]` |
| 92 | |
| 93 | `blanket` can delegate default implementations of trait methods to functions |
| 94 | of another module. This can be useful for some traits such as |
| 95 | [visitors](https://github.com/rust-unofficial/patterns/blob/master/patterns/visitor.md) |
| 96 | to provide a default behaviour as an external function, such as what |
| 97 | [`syn::visit`](https://docs.rs/syn/latest/syn/visit/index.html) is doing. |
| 98 | |
| 99 | The following example implements a very simple visitor trait for types |
| 100 | able to process a `&str` char-by-char. |
| 101 | |
| 102 | ```rust,ignore |
| 103 | extern crate blanket; |
| 104 | use blanket::blanket; |
| 105 | |
| 106 | #[blanket(default = "visitor")] |
| 107 | trait Visitor { |
| 108 | fn visit_string(&self, s: &str); |
| 109 | fn visit_char(&self, c: char); |
| 110 | } |
| 111 | |
| 112 | mod visitor { |
| 113 | use super::Visitor; |
| 114 | |
| 115 | pub fn visit_string<V: Visitor + ?Sized>(v: &V, s: &str) { |
| 116 | for c in s.chars() { |
| 117 | v.visit_char(c); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub fn visit_char<V: Visitor + ?Sized>(v: &V, c: char) {} |
| 122 | } |
| 123 | ``` |
| 124 | |
| 125 | `blanket` will check that all methods are declared without a default block, |
| 126 | and then create a default implementation for all of the declared methods, |
| 127 | generating the following code: |
| 128 | |
| 129 | ```rust,ignore |
| 130 | trait Visitor { |
| 131 | fn visit_string(&self, s: &str) { |
| 132 | visitor::visit_string(self, s) |
| 133 | } |
| 134 | fn visit_char(&self, c: char) { |
| 135 | visitor::visit_char(self, c) |
| 136 | } |
| 137 | } |
| 138 | ``` |
| 139 | |
| 140 | ## ๐ To-Do |
| 141 | |
| 142 | - โ Delegation of default method to external functions. |
| 143 | - โ Support for traits with generic arguments. |
| 144 | - โ `#[derive(Ref)]` |
| 145 | - โ `#[derive(Mut)]` |
| 146 | - โ `#[derive(Box)]` for both sized and unsized types. |
| 147 | - โ `#[derive(Rc)]` |
| 148 | - โ `#[derive(Arc)]` |
| 149 | - โ `#[derive(Cow)]` |
| 150 | |
| 151 | ## ๐ค Credits |
| 152 | |
| 153 | `blanket` is developed and maintained by: |
| 154 | - [Martin Larralde](https://github.com/althonos) |
| 155 | |
| 156 | The following people contributed to the project: |
| 157 | - [Alexander Linne](https://github.com/alexanderlinne) |
| 158 | - [Naja Melan](https://github.com/najamelan) |
| 159 | - [Justin Lovinger](https://github.com/JustinLovinger) |
| 160 | |
| 161 | ## ๐ Changelog |
| 162 | |
| 163 | This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html) |
| 164 | and provides a [changelog](https://github.com/althonos/blanket/blob/master/CHANGELOG.md) |
| 165 | in the [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) format. |
| 166 | |
| 167 | |
| 168 | ## ๐ License |
| 169 | |
| 170 | This library is provided under the open-source |
| 171 | [MIT license](https://choosealicense.com/licenses/mit/). |