1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
# Method delegation with less boilerplate
[](https://github.com/kobzol/rust-delegate/actions)
[](https://crates.io/crates/delegate)
This crate removes some boilerplate for structs that simply delegate some of
their methods to one or more of their fields.
It gives you the `delegate!` macro, which delegates method calls to selected
expressions (usually inner fields).
## Example:
A Stack data structure implemented using an inner Vec via delegation.
```rust
use delegate::delegate;
#[derive(Clone, Debug)]
struct Stack<T> {
inner: Vec<T>,
}
impl<T> Stack<T> {
pub fn new() -> Self <T> {
Self { inner: vec![] }
}
delegate! {
to self.inner {
pub fn is_empty(&self) -> bool;
pub fn push(&mut self, value: T);
pub fn pop(&mut self) -> Option<T>;
pub fn clear(&mut self);
#[call(len)]
pub fn size(&self) -> usize;
#[call(last)]
pub fn peek(&self) -> Option<&T>;
}
}
}
```
## Features
### Delegate to a method with a different name
```rust
struct Stack {
inner: Vec<u32>
}
impl Stack {
delegate! {
to self.inner {
#[call(push)]
pub fn add(&mut self, value: u32);
}
}
}
```
### Use an arbitrary inner field expression
```rust
struct Wrapper {
inner: Rc<RefCell<Vec<u32>>>
}
impl Wrapper {
delegate! {
to self.inner.deref().borrow_mut() {
pub fn push(&mut self, val: u32);
}
}
}
```
### Delegate to enum variants
```rust
use delegate::delegate;
enum Enum {
A(A),
B(B),
C { v: C },
}
struct A {
val: usize,
}
impl A {
fn dbg_inner(&self) -> usize {
dbg!(self.val);
1
}
}
struct B {
val_a: String,
}
impl B {
fn dbg_inner(&self) -> usize {
dbg!(self.val_a.clone());
2
}
}
struct C {
val_c: f64,
}
impl C {
fn dbg_inner(&self) -> usize {
dbg!(self.val_c);
3
}
}
impl Enum {
delegate! {
// transformed to
//
// ```rust
// match self {
// Enum::A(a) => a.dbg_inner(),
// Enum::B(b) => { println!("i am b"); b }.dbg_inner(),
// Enum::C { v: c } => { c }.dbg_inner(),
// }
// ```
to match self {
Enum::A(a) => a,
Enum::B(b) => { println!("i am b"); b },
Enum::C { v: c } => { c },
} {
fn dbg_inner(&self) -> usize;
}
}
}
```
### Use modifiers that alter the generated method body
```rust
use delegate::delegate;
struct Inner;
impl Inner {
pub fn method(&self, num: u32) -> u32 { num }
pub fn method_res(&self, num: u32) -> Result<u32, ()> { Ok(num) }
}
struct Wrapper {
inner: Inner
}
impl Wrapper {
delegate! {
to self.inner {
// calls method, converts result to u64 using `From`
#[into]
pub fn method(&self, num: u32) -> u64;
// calls method, returns ()
#[call(method)]
pub fn method_noreturn(&self, num: u32);
// calls method, converts result to i6 using `TryFrom`
#[try_into]
#[call(method)]
pub fn method2(&self, num: u32) -> Result<u16, std::num::TryFromIntError>;
// calls method_res, unwraps the result
#[unwrap]
pub fn method_res(&self, num: u32) -> u32;
// calls method_res, unwraps the result, then calls into
#[unwrap]
#[into]
#[call(method_res)]
pub fn method_res_into(&self, num: u32) -> u64;
// specify explicit type for into
#[into(u64)]
#[call(method)]
pub fn method_into_explicit(&self, num: u32) -> u64;
}
}
}
```
### Custom called expression
The `#[expr()]` attribute can be used to modify the delegated call. You can use the `$` sigil as a placeholder for what delegate would normally expand to, and wrap that expression with custom code.
_Note:_ the `$` placeholder isn't required and can be present multiple times if you want.
```rust
struct A(Vec<u8>);
impl A {
delegate! {
to self.0 {
#[expr(*$.unwrap())]
/// Here `$` == `self.0.get(idx)`
/// Will expand to `*self.0.get(idx).unwrap()`
fn get(&self, idx: usize) -> u8;
#[call(get)]
#[expr($?.checked_pow(2))]
/// Here `$` == `self.0.get(idx)`
/// Will expand to `self.0.get(idx)?.checked_pow(2)`
fn get_checked_pow_2(&self, idx: usize) -> Option<u8>;
}
}
}
```
### Add additional arguments to method
```rust
struct Inner(u32);
impl Inner {
pub fn new(m: u32) -> Self {
// some "very complex" constructing work
Self(m)
}
pub fn method(&self, n: u32) -> u32 {
self.0 + n
}
}
struct Wrapper {
inner: OnceCell<Inner>,
}
impl Wrapper {
pub fn new() -> Self {
Self {
inner: OnceCell::new(),
}
}
fn content(&self, val: u32) -> &Inner {
self.inner.get_or_init(|| Inner(val))
}
delegate! {
to |k: u32| self.content(k) {
// `wrapper.method(k, num)` will call `self.content(k).method(num)`
pub fn method(&self, num: u32) -> u32;
}
}
}
```
### Call `await` on async functions
```rust
struct Inner;
impl Inner {
pub async fn method(&self, num: u32) -> u32 { num }
}
struct Wrapper {
inner: Inner
}
impl Wrapper {
delegate! {
to self.inner {
// calls method(num).await, returns impl Future<Output = u32>
pub async fn method(&self, num: u32) -> u32;
// calls method(num).await.into(), returns impl Future<Output = u64>
#[into]
#[call(method)]
pub async fn method_into(&self, num: u32) -> u64;
}
}
}
```
You can use the `#[await(true/false)]` attribute on delegated methods to specify
if `.await` should be generated after the delegated expression. It will be
generated by default if the delegated method is `async`.
### Delegate to multiple fields
```rust
struct MultiStack {
left: Vec<u32>,
right: Vec<u32>,
}
impl MultiStack {
delegate! {
to self.left {
/// Push an item to the top of the left stack
#[call(push)]
pub fn push_left(&mut self, value: u32);
}
to self.right {
/// Push an item to the top of the right stack
#[call(push)]
pub fn push_right(&mut self, value: u32);
}
}
}
```
### Inline attributes
`rust-delegate` inserts `#[inline(always)]` automatically. You can override that decision by specifying `#[inline]`
manually on the delegated method.
### Segment attributes
You can use an attribute on a whole delegation segment to automatically apply it to all methods in that segment:
```rust
struct Wrapper {
inner: Inner
}
impl Wrapper {
delegate! {
#[unwrap]
to self.inner {
fn foo(&self) -> u32; // calls self.inner.foo().unwrap()
fn bar(&self) -> u32; // calls self.inner.bar().unwrap()
}
}
}
```
### Adding additional arguments
You can specify expressions in the signature that will be used as delegated arguments:
```rust
use delegate::delegate;
struct Inner;
impl Inner {
pub fn polynomial(&self, a: i32, x: i32, b: i32, y: i32, c: i32) -> i32 {
a + x * x + b * y + c
}
}
struct Wrapper {
inner: Inner,
a: i32,
b: i32,
c: i32
}
impl Wrapper {
delegate! {
to self.inner {
// Calls `polynomial` on `inner` with `self.a`, `self.b` and
// `self.c` passed as arguments `a`, `b`, and `c`, effectively
// calling `polynomial(self.a, x, self.b, y, self.c)`.
pub fn polynomial(&self, [ self.a ], x: i32, [ self.b ], y: i32, [ self.c ]) -> i32 ;
// Calls `polynomial` on `inner` with `0`s passed for arguments
// `a` and `x`, and `self.b` and `self.c` for `b` and `c`,
// effectively calling `polynomial(0, 0, self.b, y, self.c)`.
#[call(polynomial)]
pub fn linear(&self, [ 0 ], [ 0 ], [ self.b ], y: i32, [ self.c ]) -> i32 ;
}
}
}
```
### Parameter modifiers
You can modify how will an input parameter be passed to the delegated method with parameter attribute modifiers. Currently, the following modifiers are supported:
- `#[into]`: Calls `.into()` on the parameter passed to the delegated method.
- `#[as_ref]`: Calls `.as_ref()` on the parameter passed to the delegated method.
- `#[newtype]`: Accesses the first tuple element (`.0`) of the parameter passed to the delegated method.
> Note that these modifiers might be removed in the future, try to use the more general `#[expr]` mechanism to achieve this functionality.
```rust
use delegate::delegate;
struct InnerType {}
impl InnerType {
fn foo(&self, other: Self) {}
}
impl From<Wrapper> for InnerType {
fn from(wrapper: Wrapper) -> Self {
wrapper.0
}
}
struct Wrapper(InnerType);
impl Wrapper {
delegate! {
to self.0 {
// Calls `self.0.foo(other.into());`
pub fn foo(&self, #[into] other: Self);
// Calls `self.0.bar(other.0);`
pub fn bar(&self, #[newtype] other: Self);
}
}
}
```
### 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);
```
### Delegate associated constants
```rust
use delegate::delegate;
trait WithConst {
const TOTO: u8;
}
struct A;
impl WithConst for A {
const TOTO: u8 = 1;
}
struct B;
impl WithConst for B {
const TOTO: u8 = 2;
}
struct C;
impl WithConst for C {
const TOTO: u8 = 2;
}
enum Enum {
A(A),
B(B),
C(C),
}
impl Enum {
delegate! {
to match self {
Self::A(a) => a,
Self::B(b) => b,
Self::C(c) => { println!("hello from c"); c },
} {
#[const(WithConst::TOTO)]
fn get_toto(&self) -> u8;
}
}
}
assert_eq!(Enum::A(A).get_toto(), <A as WithConst>::TOTO);
```
### Delegate to fields
```rust
use delegate::delegate;
struct Datum {
value: u32,
error: u32,
}
struct DatumWrapper(Datum);
impl DatumWrapper {
delegate! {
to self.0 {
/// Get the value of a nested field with the same name
#[field]
fn value(&self) -> u32;
/// Get the value of a nested field with a different name
#[field(value)]
fn renamed_value(&self) -> u32;
/// Get shared reference to a nested field
#[field(&value)]
fn value_ref(&self) -> &u32;
/// Get mutable reference to a nested field
#[field(&mut value)]
fn value_ref_mut(&mut self) -> &mut u32;
/// Get mutable reference to a nested field with the same name
#[field(&)]
fn error(&self) -> &u32;
}
}
}
```
## Development
This project uses a standard test suite for quality control, as well as a set of
"expansion" tests that utilize the `macrotest` crate to ensure the macro expands
as expected. PRs implementing new features should add both standard and expansion
tests where appropriate.
To add an expansion test, place a Rust source file in the `tests/expand/` directory
with methods demonstrating the new feature. Next, run `cargo test` to run the test
suite and generate a `*.expanded.rs` file in the same directory. Next, carefully
inspect the contents of the generated file to confirm that all methods expanded as
expected. Finally, commit both files to the git repository. Future test suite runs
will now include expanding the source file and comparing it to the expanded file.
## License
Licensed under either of
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
## Conduct
Please follow the [Rust Code of Conduct]. For escalation or moderation issues
please contact the crate author(s) listed in [`Cargo.toml`](./Cargo.toml).
[Rust Code of Conduct]: https://www.rust-lang.org/conduct.html
|