3 releases

0.1.2 Jul 22, 2024
0.1.1 Jul 22, 2024
0.1.0 Jul 22, 2024

#370 in Procedural macros

MIT license

6KB
71 lines

Arc Trait

arc_trait is a procedural macro that simplifies the process of implementing traits for std::sync::Arc<T>. By applying this macro to a trait, you enable Arc<T> to implement it whenever the inner type T implements the trait, saving you from writing repetitive boilerplate code.

Features

  • Automatically implements a trait for std::sync::Arc<T> types.
  • Simplifies code and reduces boilerplate.
  • Easy to use with simple attribute-based syntax.

Installation

Add this to your Cargo.toml:

[dependencies]
arc_trait = "0.1.0"

And this to your crate root:

extern crate arc_trait;
use arc_trait::arc_trait;

Usage

Apply the arc_trait attribute to your traits as shown in the example:

extern crate arc_trait;
use arc_trait::arc_trait;
use std::sync::Arc;

#[arc_trait]
trait ExampleTrait {
    fn example_method(&self, value: i32) -> i32;
}

struct Example;

impl ExampleTrait for Example {
    fn example_method(&self, value: i32) -> i32 {
        value + 1
    }
}

fn main() {
    let example = Arc::new(Example);
    assert_eq!(example.example_method(1), 2);
}

How It Works

arc_trait works by:

  • Parsing the given trait to identify all methods within it.
  • Generating an implementation of the trait for std::sync::Arc<T> that forwards method calls to the inner value wrapped by the Arc.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Dependencies

~2.6–8.5MB
~74K SLoC