Skip to main content

with_

Macro with_ 

Source
with_!() { /* proc-macro */ }
Expand description

Field-level borrow splitting macro.

See the fieldwork crate documentation for detailed usage information.

§Syntax

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.