auto_impl
A simple compiler plugin for automatically implementing a trait for some common smart pointers and closures.
This library requires the nightly
channel.
Add auto_impl
to your Cargo.toml
:
auto_impl = "*"
Reference in your crate root:
```rust
extern crate auto_impl;
use autoimpl::autoimpl; ```
The following types are supported:
Add the #[auto_impl]
attribute to traits to automatically implement them for wrapper types:
```rust
trait MyTrait<'a, T>
where T: AsRef
fn execute1<'b>(&'a self, arg1: &'b T) -> Result<Self::Type1, String>;
fn execute2(&self) -> Self::Type2;
} ```
Will expand to:
```rust
impl<'a, T, TAutoImpl> MyTrait<'a, T> for ::std::sync::Arc
fn execute1<'b>(&'a self, arg1: &'b T) -> Result<Self::Type1, String> {
self.as_ref().execute1(arg1)
}
fn execute2(&self) -> Self::Type2 {
self.as_ref().execute2()
}
}
impl<'a, T, TAutoImpl> MyTrait<'a, T> for Box
fn execute1<'b>(&'a self, arg1: &'b T) -> Result<Self::Type1, String> {
self.as_ref().execute1(arg1)
}
fn execute2(&self) -> Self::Type2 {
self.as_ref().execute2()
}
}
impl<'a, T, TAutoImpl> MyTrait<'a, T> for ::std::rc::Rc
fn execute1<'b>(&'a self, arg1: &'b T) -> Result<Self::Type1, String> {
self.as_ref().execute1(arg1)
}
fn execute2(&self) -> Self::Type2 {
self.as_ref().execute2()
}
} ```
There are a few restrictions on #[auto_impl]
for smart pointers. The trait must:
&self
```rust
trait MyTrait<'a, T> { fn execute<'b>(&'a self, arg1: &'b T, arg2: &'static str) -> Result<(), String>; } ```
Will expand to:
rust
impl<'a, T, TAutoImpl> MyTrait<'a, T> for TAutoImpl
where TAutoImpl: Fn(&T, &'static str) -> Result<(), String>
{
fn execute<'b>(&'a self, arg1: &'b T, arg1: &'static str) -> Result<(), String> {
self(arg1, arg2)
}
}
There are a few restrictions on #[auto_impl]
for closures. The trait must:
```rust
trait MyTrait<'a, T> { fn execute<'b>(&'a self, arg1: &'b T, arg2: &'static str) -> Result<(), String>; } ```
Will expand to:
```rust impl<'auto, 'a, T, TAutoImpl> MyTrait<'a, T> for &'auto TAutoImpl { fn execute<'b>(&'a self, arg1: &'b T, arg1: &'static str) -> Result<(), String> { (**self).execute(arg1, arg2) } }
impl<'auto, 'a, T, TAutoImpl> MyTrait<'a, T> for &'auto mut TAutoImpl { fn execute<'b>(&'a self, arg1: &'b T, arg1: &'static str) -> Result<(), String> { (**self).execute(arg1, arg2) } } ```
There are a few restrictions on #[auto_impl]
for immutably borrowed references. The trait must:
&self
There are no restrictions on #[auto_impl]
for mutably borrowed references.