#delegates #attributes #field #proc-macro #attr #block

macro delegate-attr

Attribute proc-macro to delegate method to a field

13 releases

0.3.0 Sep 17, 2023
0.2.9 Aug 11, 2020
0.2.7 Jun 16, 2020
0.2.6 May 24, 2020
0.1.1 May 19, 2020

#899 in Rust patterns

Download history 16077/week @ 2023-11-28 16790/week @ 2023-12-05 14625/week @ 2023-12-12 18708/week @ 2023-12-19 15598/week @ 2023-12-26 20400/week @ 2024-01-02 23318/week @ 2024-01-09 19313/week @ 2024-01-16 15366/week @ 2024-01-23 12184/week @ 2024-01-30 16608/week @ 2024-02-06 13483/week @ 2024-02-13 12795/week @ 2024-02-20 11638/week @ 2024-02-27 13404/week @ 2024-03-05 2894/week @ 2024-03-12

42,242 downloads per month
Used in 39 crates (6 directly)

MIT license

14KB
192 lines

delegate-attr

CI Crates.io

Attribute proc-macro to delegate method to a field.

Examples

Delegate impl block

use delegate_attr::delegate;

struct Foo(String);

#[delegate(self.0)]
impl Foo {
    fn as_str(&self) -> &str {}
    fn into_bytes(self) -> Vec<u8> {}
}

let foo = Foo("hello".to_owned());
assert_eq!(foo.as_str(), "hello");
assert_eq!(foo.into_bytes(), b"hello");

Delegate trait impl


struct Iter(std::vec::IntoIter<u8>);

#[delegate(self.0)]
impl Iterator for Iter {
    type Item = u8;
    fn next(&mut self) -> Option<u8> {}
    fn count(self) -> usize {}
    fn size_hint(&self) -> (usize, Option<usize>) {}
    fn last(self) -> Option<u8> {}
}

let iter = Iter(vec![1, 2, 4, 8].into_iter());
assert_eq!(iter.count(), 4);
let iter = Iter(vec![1, 2, 4, 8].into_iter());
assert_eq!(iter.last(), Some(8));
let iter = Iter(vec![1, 2, 4, 8].into_iter());
assert_eq!(iter.sum::<u8>(), 15);

With more complicated target

struct Foo<T> {
    inner: RefCell<Vec<T>>,
}

#[delegate(self.inner.borrow())]
impl<T> Foo<T> {
    fn len(&self) -> usize {}
}

#[delegate(self.inner.borrow_mut())]
impl<T> Foo<T> {
    fn push(&self, value: T) {}
}

#[delegate(self.inner.into_inner())]
impl<T> Foo<T> {
    fn into_boxed_slice(self) -> Box<[T]> {}
}

let foo = Foo { inner: RefCell::new(vec![1]) };
assert_eq!(foo.len(), 1);
foo.push(2);
assert_eq!(foo.len(), 2);
assert_eq!(foo.into_boxed_slice().as_ref(), &[1, 2]);

into and call attribute

struct Inner;
impl Inner {
    pub fn method(&self, num: u32) -> u32 { num }
}

struct Wrapper { inner: Inner }

#[delegate(self.inner)]
impl Wrapper {
    // calls method, converts result to u64
    #[into]
    pub fn method(&self, num: u32) -> u64 {}

    // calls method, returns ()
    #[call(method)]
    pub fn method_noreturn(&self, num: u32) {}
}

Delegate single method

struct Foo<T>(Vec<T>);

impl<T> Foo<T> {
    #[delegate(self.0)]
    fn len(&self) -> usize {}
}

let foo = Foo(vec![1]);
assert_eq!(foo.len(), 1);

Dependencies

~0.4–0.8MB
~19K SLoC