Skip to content

Commit

Permalink
Add example of delegating associated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Jul 7, 2024
1 parent 556adea commit eb03baf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,30 @@ impl<T> Stack<T> {
}
```

- Delegate associated functions
```rust
use delegate::delegate;

struct A {}
impl A {
fn foo(a: u32) -> u32 {
a + 1
}
}

struct B;

impl B {
delegate! {
to A {
fn foo(a: u32) -> u32;
}
}
}

assert_eq!(B::foo(1), 2);
```

## License

Licensed under either of
Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,29 @@
//! }
//! }
//! ```
//! - Delegate associated functions
//! ```rust
//! use delegate::delegate;
//!
//! struct A {}
//! impl A {
//! fn foo(a: u32) -> u32 {
//! a + 1
//! }
//! }
//!
//! struct B;
//!
//! impl B {
//! delegate! {
//! to A {
//! fn foo(a: u32) -> u32;
//! }
//! }
//! }
//!
//! assert_eq!(B::foo(1), 2);
//! ```
extern crate proc_macro;
use std::mem;
Expand Down

0 comments on commit eb03baf

Please sign in to comment.