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
//! Proc-macro implementation for the fieldwork crate.
//!
//! This crate provides the implementation of the [`with_!`](with_) macro.
//! Users should depend on the `fieldwork` crate instead of this one directly.
use TokenStream;
/// Field-level borrow splitting macro.
///
/// See the [`fieldwork`](https://docs.rs/fieldwork) crate documentation for
/// detailed usage information.
///
/// # Syntax
///
/// ```text
/// with_!(receiver, {
/// methods {
/// method_name(&field1, mut field2, copy field3, arg: Type) -> ReturnType {
/// // method body
/// }
/// }
/// fields {
/// field_a
/// mut field_b
/// }
/// body {
/// // code that uses receiver.method_name() and receiver.field_x
/// }
/// });
/// ```
///
/// # Field Modes
///
/// - `field` or `&field` — shared reference (`&receiver.field`)
/// - `mut field` — mutable reference (`&mut receiver.field`)
/// - `copy field` — by value, requires `Copy` trait
///
/// # How It Works
///
/// The macro:
/// 1. Extracts fields from the receiver as separate local bindings
/// 2. Replaces `receiver.method()` calls with inlined method bodies
/// 3. Replaces `receiver.field` accesses with the local bindings
///
/// This allows the borrow checker to see that the borrows are disjoint.