Skip to content

[WIP]: Add overload proc macro for #[splat] function overloading#5

Draft
Ajay-singh1 wants to merge 5 commits into
rustfoundation:mainfrom
Ajay-singh1:overloading-macro-for-splat
Draft

[WIP]: Add overload proc macro for #[splat] function overloading#5
Ajay-singh1 wants to merge 5 commits into
rustfoundation:mainfrom
Ajay-singh1:overloading-macro-for-splat

Conversation

@Ajay-singh1

Copy link
Copy Markdown
Collaborator

This is a proof of concept for the overload! macro that makes #[splat]
easier to use for function overloading.

What it does

Turns this:

overload! {
    fn foo(x: i32) { println!("i32: {}", x); }
    fn foo(x: f64) { println!("f64: {}", x); }
}

Into this (generated automatically):

trait FooArgs: std::marker::Tuple {
    fn call(self);
}
impl FooArgs for (i32,) {
    fn call(self) { let x = self.0; println!("i32: {}", x); }
}
impl FooArgs for (f64,) {
    fn call(self) { let x = self.0; println!("f64: {}", x); }
}
fn foo<T: FooArgs>(#[splat] args: T) {
    args.call()
}

Current limitations

  • Only handles 2 free functions (not methods)
  • Only handles single argument
  • No return values yet

Next steps

  • Handle multiple arguments
  • Handle return values
  • Handle methods

Signed-off-by: Ajay Singh <ajaykripa8736968359@gmail.com>
@Ajay-singh1 Ajay-singh1 marked this pull request as draft July 12, 2026 17:32
@teor2345 teor2345 self-requested a review July 13, 2026 03:36
@teor2345 teor2345 added the enhancement New feature or request label Jul 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.

Looks like you need to edit the Cargo.toml files to fix these CI errors, do you want some help with that?

error: the #[proc_macro] attribute is only usable with crates of the proc-macro crate type

[lib]
proc-macro = true

https://doc.rust-lang.org/reference/procedural-macros.html#r-macro.proc.def

error[E0432]: unresolved import proc_macro
error[E0433]: cannot find module or crate syn in this scope
error[E0432]: unresolved import quote

The dependency versions are in the base Cargo.toml, then they can get inherited by this crate's Cargo.toml.

@Ajay-singh1

Ajay-singh1 commented Jul 13, 2026 via email

Copy link
Copy Markdown
Collaborator Author

Signed-off-by: Ajay Singh <ajaykripa8736968359@gmail.com>
Signed-off-by: Ajay Singh <ajaykripa8736968359@gmail.com>
Signed-off-by: Ajay Singh <ajaykripa8736968359@gmail.com>

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

Let's fix and merge what we have now, then open other PRs for further changes?

Comment thread rust-toolchain.toml
@@ -0,0 +1,2 @@
[toolchain]

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.

Good idea, I should do this in the blog post examples as well

Comment thread splat-overload-test/src/bin/multiple-args.rs Outdated
#![feature(tuple_trait)]
#![allow(incomplete_features)]

use splat_overload::overload;

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.

Looks like there's trailing whitespace on a bunch of these lines, running cargo fmt --all should get rid of them and pass CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants