Skip to content

Add method support to overload! macro - #11

Merged
teor2345 merged 5 commits into
rustfoundation:mainfrom
Ajay-singh1:methods
Aug 13, 2026
Merged

Add method support to overload! macro#11
teor2345 merged 5 commits into
rustfoundation:mainfrom
Ajay-singh1:methods

Conversation

@Ajay-singh1

Copy link
Copy Markdown
Collaborator

Add method support to overload! macro

Extends the overload! macro to support methods (functions taking
&self, &mut self, or owned self), not just free functions.

Usage:

struct Counter { value: i32 }

overload! {
    impl Counter {
        fn add(&mut self, x: i32) { self.value += x; }
        fn add(&mut self, x: i32, y: i32) { self.value += x + y; }
    }
}

Design problem encountered and resolved

Initially the macro pasted method bodies verbatim into a generated call method on the argument tuple type, the same pattern used for free functions. This breaks for methods, because inside call, self refers to the argument tuple, not the receiver , but the user's original body (e.g. self.value += x) expects self to mean the receiver. Rust doesn't allow rebinding the identifier self to anything other than a function's own receiver, so this couldn't be fixed by rearranging code inside call.

Fix: generate a hidden method directly on the receiver type per overload, with the user's original body pasted in completely
unmodified (so self there genuinely is the receiver), and have call just forward to it:

impl Counter {
    fn __add_impl_0(&mut self, x: i32) { self.value += x; }
}
impl AddArgs<Counter> for (i32,) {
    fn call(self, this: &mut Counter) -> Self::Output {
        let x = self.0;
        this.__add_impl_0(x)
    }
}

This works uniformly across all three receiver kinds (&self, &mut self, self) since the hidden method just uses whatever receiver form the overload declared, and the trait's call signature is generated to match, using a generic R in the trait declaration and the concrete type in each impl.

All overloads within a single overload! block must use the same receiver kind; mixing e.g. &self and &mut self across overloads of the same name produces a clear panic.

Tested cases

  • &self methods, single and multiple arguments
  • &self methods with return values, including mixed unit/non-unit
  • &mut self methods mutating state
  • Owned self methods (builder-style, consuming the receiver)
  • Zero-argument method mixed with multi-argument overloads
  • Early return inside a method body
  • Non-primitive return types (Vec) from a method
  • Non-Copy argument types (String) passed to a method

Includes 6 new example binaries with assert_eq! checks, added to the
CI runner list alongside the existing 9.

@teor2345 teor2345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The splat attribute has been renamed to avoid clashes with user-provided names on stable. This is caused by a compiler name lookup bug that hasn't been fixed yet.

If you download the latest nightly build, it should have the new name.

Comment thread splat-overload/src/lib.rs Outdated
Comment thread splat-overload/src/lib.rs Outdated
Co-authored-by: teor <teor@riseup.net>

@teor2345 teor2345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy has style suggestions, I'll apply them now

Comment thread splat-overload-test/src/bin/methods-complex.rs Outdated
Comment thread splat-overload-test/src/bin/methods-complex.rs Outdated
Co-authored-by: teor <teor@riseup.net>
teor2345
teor2345 previously approved these changes Aug 13, 2026

@teor2345 teor2345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think this is good enough to publish as an experiment. I'll get to work on that now.

@teor2345 teor2345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More clippy 😂

Comment thread splat-overload-test/src/bin/methods.rs Outdated
Comment thread splat-overload-test/src/bin/methods.rs Outdated
Co-authored-by: teor <teor@riseup.net>
Comment thread splat-overload-test/src/bin/methods.rs Outdated
Co-authored-by: teor <teor@riseup.net>

@teor2345 teor2345 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally 😅

@teor2345
teor2345 merged commit 1f84f5a into rustfoundation:main Aug 13, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants