https://github.com/althonos/blanket

A simple Rust macro to derive blanket implementations for your traits.

https://github.com/althonos/blanket

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 5 committers (20.0%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (10.8%) to scientific vocabulary

Keywords

derive proc-macro-attributes rust-macro rust-trait

Keywords from Contributors

genomics metagenomics
Last synced: 10 months ago · JSON representation

Repository

A simple Rust macro to derive blanket implementations for your traits.

Basic Info
Statistics
  • Stars: 25
  • Watchers: 2
  • Forks: 3
  • Open Issues: 3
  • Releases: 8
Topics
derive proc-macro-attributes rust-macro rust-trait
Created almost 6 years ago · Last pushed over 2 years ago
Metadata Files
Readme Changelog License

README.md

🧣 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:

rust 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:

```rust extern crate blanket; use blanket::blanket;

[blanket(derive(Mut, Box))]

pub trait Write { fn writestr(&mut self, s: &str) -> std::fmt::Result; fn writechar(&mut self, c: char) -> std::fmt::Result { self.writestr(c.encodeutf8(&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:

rustc ---- 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.

```rust,ignore extern crate blanket; use blanket::blanket;

[blanket(default = "visitor")]

trait Visitor { fn visitstring(&self, s: &str); fn visitchar(&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:

rust,ignore 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: - Martin Larralde

The following people contributed to the project: - Alexander Linne - Naja Melan - Justin Lovinger

📋 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.

Owner

  • Name: Martin Larralde
  • Login: althonos
  • Kind: user
  • Location: Heidelberg, Germany
  • Company: EMBL / LUMC, @zellerlab

PhD candidate in Bioinformatics, passionate about programming, SIMD-enthusiast, Pythonista, Rustacean. I write poems, and sometimes they are executable.

GitHub Events

Total
  • Watch event: 3
  • Fork event: 1
Last Year
  • Watch event: 3
  • Fork event: 1

Committers

Last synced: over 1 year ago

All Time
  • Total Commits: 87
  • Total Committers: 5
  • Avg Commits per committer: 17.4
  • Development Distribution Score (DDS): 0.483
Past Year
  • Commits: 19
  • Committers: 1
  • Avg Commits per committer: 19.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Martin Larralde m****e@e****r 45
Martin Larralde m****e@e****e 35
Naja Melan n****n@a****g 4
Justin Lovinger g****t@j****m 2
Alexander Linne a****e@o****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 7
  • Total pull requests: 7
  • Average time to close issues: 9 days
  • Average time to close pull requests: 2 days
  • Total issue authors: 4
  • Total pull request authors: 3
  • Average comments per issue: 1.43
  • Average comments per pull request: 3.43
  • Merged pull requests: 7
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • nwalfield (4)
  • tmillr (1)
  • JustinLovinger (1)
  • najamelan (1)
Pull Request Authors
  • najamelan (4)
  • JustinLovinger (2)
  • alexanderlinne (1)
Top Labels
Issue Labels
bug (3) enhancement (2)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cargo 592,880 total
  • Total docker downloads: 18,714,625
  • Total dependent packages: 7
  • Total dependent repositories: 21
  • Total versions: 9
  • Total maintainers: 1
crates.io: blanket

A simple macro to derive blanket implementations for your traits.

  • Versions: 9
  • Dependent Packages: 7
  • Dependent Repositories: 21
  • Downloads: 592,880 Total
  • Docker Downloads: 18,714,625
Rankings
Downloads: 4.7%
Docker downloads count: 4.8%
Dependent repos count: 5.9%
Dependent packages count: 6.2%
Average: 12.0%
Stargazers count: 24.2%
Forks count: 26.3%
Maintainers (1)
Last synced: 10 months ago

Dependencies

Cargo.toml cargo
  • impls 1.0 development
  • static_assertions 1.1 development
  • syn 1.0 development
  • trybuild 1.0 development
  • proc-macro2 1.0
  • quote 1.0
  • syn 1.0
.github/workflows/test.yml actions
  • actions-rs/cargo v1 composite
  • actions-rs/tarpaulin v0.1 composite
  • actions-rs/toolchain v1 composite
  • actions/checkout v3 composite
  • codecov/codecov-action v3 composite
  • rasmus-saks/release-a-changelog-action v1.0.1 composite