Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
resolver = "3"
members = [
"splat-overload",
"splat-overload-test"
]
default-members = [
"splat-overload",
"splat-overload-test"
]

[workspace.package]
Expand All @@ -24,7 +26,7 @@ readme = "README.md"
repository = "https://github.com/rustfoundation/overloading-macros"
Comment thread
teor2345 marked this conversation as resolved.
# Requires #[splat] which was introduced just before 1.99 branched
rust-version = "1.99"

[workspace.dependencies]
# Delete this package and replace it with the actual dependencies
example = "1.1.0"
syn = { version = "2.0.119", features = ["full"] }
quote = "1.0.46"
proc-macro2 = "1.0.106"
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
Comment thread
Ajay-singh1 marked this conversation as resolved.
channel = "nightly"
7 changes: 7 additions & 0 deletions splat-overload-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "splat-overload-test"
version = "0.1.0"
edition = "2021"

[dependencies]
splat-overload = { path = "../splat-overload" }
2 changes: 2 additions & 0 deletions splat-overload-test/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
23 changes: 23 additions & 0 deletions splat-overload-test/src/bin/multiple-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features, clippy::approx_constant)]

use splat_overload::overload;
Comment thread
Ajay-singh1 marked this conversation as resolved.

overload! {
fn foo(x: i32, y: f64) {
println!("i32: {}, f64: {}", x, y);
}
fn foo(x: bool, y: i32, z: f64) {
println!("bool: {}, i32: {}, f64: {}", x, y, z);
}
fn foo(a: i32, b: f64, c: bool, d: u8) {
println!("i32: {}, f64: {}, bool: {}, u8: {}", a, b, c, d);
}
}

fn main() {
foo(42, 3.14);
foo(true, 42, 3.14);
foo(42, 3.14, true, 255);
}
27 changes: 27 additions & 0 deletions splat-overload-test/src/bin/multiple-mixed-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]

use splat_overload::overload;

overload! {
fn calculate(a: i32, b: i32) {
println!("sum: {}", a + b);
}
fn calculate(a: f64, b: f64, c: f64) {
println!("average: {}", (a + b + c) / 3.0);
}
fn calculate(x: i32, y: i32, z: i32, w: i32) {
println!("product: {}", x * y * z * w);
}
fn calculate(a: f64, b: f64, c: f64, d: f64, e: f64) {
println!("max would need std: {} {} {} {} {}", a, b, c, d, e);
}
}

fn main() {
calculate(10, 20);
calculate(1.0, 2.0, 3.0);
calculate(2, 3, 4, 5);
calculate(1.0, 2.0, 3.0, 4.0, 5.0);
}
9 changes: 6 additions & 3 deletions splat-overload/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Modify the workspace Cargo.toml instead of this file (if possible)
[package]
name = "splat-overload"
version.workspace = true
Expand All @@ -11,6 +10,10 @@ readme.workspace = true
repository.workspace = true
rust-version.workspace = true

[lib]
proc-macro = true

[dependencies]
# Delete this package and replace it with the actual dependencies
example.workspace = true
syn.workspace = true
quote.workspace = true
proc-macro2.workspace = true
90 changes: 89 additions & 1 deletion splat-overload/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
//! Delete this comment and add the actual code here
use proc_macro::TokenStream;
use quote::quote;
use syn::{
FnArg, ItemFn, Pat, Result,
parse::{Parse, ParseStream},
parse_macro_input,
};

struct OverloadInput {
functions: Vec<ItemFn>,
}

impl Parse for OverloadInput {
fn parse(input: ParseStream) -> Result<Self> {
let mut functions = Vec::new();
while !input.is_empty() {
functions.push(input.parse::<ItemFn>()?);
}
Ok(OverloadInput { functions })
}
}

#[proc_macro]
pub fn overload(input: TokenStream) -> TokenStream {
let OverloadInput { functions } = parse_macro_input!(input as OverloadInput);

let fn_name = &functions[0].sig.ident;

let trait_name = quote::format_ident!(
"{}Args",
fn_name
.to_string()
.chars()
.enumerate()
.map(|(i, c)| if i == 0 {
c.to_uppercase().next().unwrap()
} else {
c
})
.collect::<String>()
);

let mut impls = Vec::new();
for func in &functions {
// Collect All arguments and names
let mut arg_types = Vec::new();
let mut arg_names = Vec::new();
let mut arg_indices = Vec::new();
let block = &func.block;
for (i, arg) in func.sig.inputs.iter().enumerate() {
if let FnArg::Typed(pat_type) = arg {
let ty = &pat_type.ty;
arg_types.push(quote! { #ty });

let arg_name = if let Pat::Ident(pat_ident) = &*pat_type.pat {
let ident = &pat_ident.ident;
quote! { #ident }
} else {
quote! { _arg }
};
arg_names.push(arg_name);

let index = syn::Index::from(i);
arg_indices.push(quote! { self.#index });
}
}
impls.push(quote! {
impl #trait_name for (#(#arg_types),*,) {
fn call(self) {
#(let #arg_names = #arg_indices;)*
#block
}
}
});
}
let generated = quote! {
trait #trait_name: std::marker::Tuple {
fn call(self);
}

#(#impls)*

fn #fn_name<T: #trait_name>(#[splat] args: T) {
args.call()
}
};

generated.into()
}