Debian Codegraph @

README.md

raw symbols SCIP
1 # ๐Ÿงฃ `blanket` [![Star me](https://img.shields.io/github/stars/althonos/blanket.svg?style=social&label=Star&maxAge=3600)](https://github.com/althonos/blanket/stargazers)
2
3 *A simple macro to derive blanket implementations for your traits.*
4
5 [![Actions](https://img.shields.io/github/actions/workflow/status/althonos/blanket/test.yml?branch=master&style=flat-square&maxAge=600)](https://github.com/althonos/blanket/actions)
6 [![Codecov](https://img.shields.io/codecov/c/gh/althonos/blanket/master.svg?style=flat-square&maxAge=600)](https://codecov.io/gh/althonos/blanket)
7 [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square&maxAge=2678400)](https://choosealicense.com/licenses/mit/)
8 [![Source](https://img.shields.io/badge/source-GitHub-303030.svg?maxAge=2678400&style=flat-square)](https://github.com/althonos/blanket)
9 [![Crate](https://img.shields.io/crates/v/blanket.svg?maxAge=600&style=flat-square)](https://crates.io/crates/blanket)
10 [![Documentation](https://img.shields.io/badge/docs.rs-latest-4d76ae.svg?maxAge=2678400&style=flat-square)](https://docs.rs/blanket)
11 [![Changelog](https://img.shields.io/badge/keep%20a-changelog-8A0707.svg?maxAge=2678400&style=flat-square)](https://github.com/althonos/blanket.rs/blob/master/CHANGELOG.md)
12 [![GitHub issues](https://img.shields.io/github/issues/althonos/blanket.svg?style=flat-square&maxAge=600)](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/).