Repository navigation
Releases
Released Version0.1.3
V0.1.2 Fixed async trait fn
Bug Fix
- Fixed a bug that caused the definition of traits with immutable references as receivers to fail.
Released Version0.1.0
Bug Fix
- Fixed a bug where declaring an enum with more than 4 variants could no longer compile.
- Fixed a bug that caused a compilation error when a method with generics that could not be inferred was defined; for example, something like below.
#[delegate]
trait Trait {
// Cannot guess the type of S at the time of delegation
fn sample<S>(&self);
}Features
Support for Unnamed Fields
You can now write the following for a field in the Tuple Struct.
#[derive(Delegate, Default)]
struct Parent(#[to(Increment)]Child);Support for delegation of static methods
Traits with static methods should not be used on Enums, as currently using them with Enums will not cause compilation errors, but will likely disable them in the future.
If used for Enum, the static method corresponding to the first variant is called.
use auto_delegate::{delegate, Delegate};
#[delegate]
trait StaticTrait {
fn static_method() -> usize;
}
struct Child1;
impl StaticTrait for Child1 {
fn static_method() -> usize {
1
}
}
struct Child2;
impl StaticTrait for Child2 {
fn static_method() -> usize {
2
}
}
#[derive(Delegate)]
#[to(StaticTrait)]
enum Parent {
Child1(Child1),
Child2(Child2),
}
fn main() {
// `Child1::static_method` is called
assert_eq!(Parent::static_method(), 1);
}V0.0.8 Supported self-receiver and fix bugs
Functions that take self as an argument are now available from this version like this:
#[delegate]
trait Calc {
fn calc(self, x1: usize, x2: usize) -> usize;
}In addition, a bug around lifetime has been fixed.
Fixed IntelliSense Working on Clion
V0.0.6 Supported Stable Channel
The previous version could only operate nightly, but from this version it will be able to operate in Stable.
V0.0.5 Super traits
Supported Supertrait.
use auto_delegate_macros::{delegate, Delegate};
#[delegate]
trait Calc {
fn calc(&self, x1: usize, x2: usize) -> usize;
}
#[delegate]
trait Label {
fn label(&self) -> String;
}
#[delegate]
trait Increment: Calc + Label {
fn increment(&mut self) -> usize;
}
#[derive(Default)]
struct Child {
num: usize,
}
impl Increment for Child {
fn increment(&mut self) -> usize {
self.num += 1;
self.num
}
}
impl Calc for Child {
fn calc(&self, x1: usize, x2: usize) -> usize {
x1 + x2
}
}
impl Label for Child {
fn label(&self) -> String {
String::from("add")
}
}
#[derive(Default, Delegate)]
struct Parent {
#[to(Calc, Increment, Label)]
child: Child,
}
fn main() {
let mut parent = Parent::default();
assert_eq!(parent.increment(), 1);
assert_eq!(parent.increment(), 2);
assert_eq!(parent.calc(3, 2), 5);
assert_eq!(parent.label(), "add");
}v0.0.4 async-trait
Support async traits
Correctly, it was fixed to properly handle lifetime bounds generated by async-trait.
By the way, v0.0.3 was published by mistake.
Example Heres:
#![feature(async_fn_in_trait)]
use async_trait::async_trait;
use auto_delegate::{Delegate, delegate};
#[async_trait]
#[delegate]
pub trait Calc {
async fn calc(&self, x1: usize, x2: usize) -> usize;
}
#[derive(Default)]
pub struct Child;
#[async_trait]
impl Calc for Child {
async fn calc(&self, x1: usize, x2: usize) -> usize {
x1 + x2
}
}
#[derive(Delegate, Default)]
#[to(Calc)]
pub struct Parent(Child);
#[tokio::main]
async fn main() {
let mut parent = Parent::default();
println!("{}", parent.calc(3, 2).await)
}v0.0.2
Supported New-Type Pattern
Example are as follows
#[delegate]
pub trait Calc {
fn calc(&self, x1: usize, x2: usize) -> usize;
}
#[derive(Default)]
pub struct Child;
impl Calc for Child {
fn calc(&self, x1: usize, x2: usize) -> usize {
x1 + x2
}
}
#[derive(Delegate, Default)]
#[to(Calc)]
pub struct Parent(Child);
fn main() {
let mut parent = Parent::default();
assert_eq!(parent.calc(2, 3), 5);
}