Skip to content

Add return value support to overload! macro - #7

Merged
teor2345 merged 2 commits into
rustfoundation:mainfrom
Ajay-singh1:return-values
Jul 27, 2026
Merged

Add return value support to overload! macro#7
teor2345 merged 2 commits into
rustfoundation:mainfrom
Ajay-singh1:return-values

Conversation

@Ajay-singh1

Copy link
Copy Markdown
Collaborator

Add return value support to overload! macro

Extends the overload! macro to support return values. Each overload
declares its own return type independently, via an associated type
Output on the generated trait:

trait FooArgs: std::marker::Tuple {
    type Output;
    fn call(self) -> Self::Output;
}

For example, this input:

overload! {
    fn foo(x: i32) -> i32 { x * 2 }
    fn foo(x: f64) -> f64 { x * 2.0 }
}

now generates:

trait FooArgs: std::marker::Tuple {
    type Output;
    fn call(self) -> Self::Output;
}

impl FooArgs for (i32,) {
    type Output = i32;
    fn call(self) -> i32 {
        let x = self.0;
        x * 2
    }
}

impl FooArgs for (f64,) {
    type Output = f64;
    fn call(self) -> f64 {
        let x = self.0;
        x * 2.0
    }
}

fn foo<T: FooArgs>(#[splat] args: T) -> T::Output {
    args.call()
}

Tested cases:

  • Different return types per overload (i32 vs f64)
  • Mixed argument shapes with the same return type
  • Overloads with no return type mixed with overloads that do return
  • Early return inside the function body
  • Non-primitive return types (Vec, String)
  • Non-Copy types moved out of the argument tuple
  • Zero-argument overloads

Includes 8 example binaries with assert_eq! checks, added to the CI
runner list.

@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.

This looks good, I'm just going to tweak a comment then merge. We can follow up the lint allows later.

Comment thread splat-overload/src/lib.rs Outdated
Comment thread splat-overload-test/src/bin/return-values.rs
@teor2345
teor2345 enabled auto-merge July 27, 2026 06:51
@teor2345
teor2345 added this pull request to the merge queue Jul 27, 2026
Merged via the queue into rustfoundation:main with commit 40e83c9 Jul 27, 2026
7 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