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
6 changes: 6 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ jobs:
run: |
cargo run --all-features --bin multiple-args
cargo run --all-features --bin multiple-mixed-args
cargo run --all-features --bin return-values
cargo run --all-features --bin return-values-mixed-args
cargo run --all-features --bin return-values-mixed-unit
cargo run --all-features --bin return-values-complex
cargo run --all-features --bin return-values-non-copy
cargo run --all-features --bin return-values-zero-args

clippy-rust:
name: 4. Clippy lints on Rust crates
Expand Down
35 changes: 35 additions & 0 deletions splat-overload-test/src/bin/return-values-complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

overload! {
fn foo(x: i32) -> i32 {
if x < 0 {
return 0;
}
x * 2
}
fn foo(x: i32, y: i32) -> Vec<i32> { vec![x, y] }
fn foo(x: i32, y: i32, z: i32) -> String { format!("{}-{}-{}", x, y, z) }
}

fn main() {
let a = foo(-5);
assert_eq!(a, 0);
println!("early return result: {}", a);

let b = foo(10);
assert_eq!(b, 20);
println!("normal path result: {}", b);

let c = foo(1i32, 2);
assert_eq!(c, vec![1, 2]);
println!("vec result: {:?}", c);

let d = foo(1i32, 2, 3);
assert_eq!(d, "1-2-3");
println!("string result: {}", d);
}
21 changes: 21 additions & 0 deletions splat-overload-test/src/bin/return-values-mixed-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

overload! {
fn foo(x: f64, y: i32) -> i32 { (x as i32) + y }
fn foo(x: i32) -> i32 { x * 2 }
}

fn main() {
let a = foo(3.7, 10);
assert_eq!(a, 13);
println!("mixed args result: {}", a);

let b = foo(21);
assert_eq!(b, 42);
println!("single arg result: {}", b);
}
19 changes: 19 additions & 0 deletions splat-overload-test/src/bin/return-values-mixed-unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

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

fn main() {
let a = foo(21);
assert_eq!(a, 42);
println!("i32 result: {}", a);
foo(2.5);
println!("f64 call completed");
}
21 changes: 21 additions & 0 deletions splat-overload-test/src/bin/return-values-non-copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

overload! {
fn foo(x: String) -> String { x }
fn foo(x: String, y: String) -> String { format!("{}{}", x, y) }
}

fn main() {
let a = foo("We love Rust!".to_string());
assert_eq!(a, "We love Rust!");
println!("single string result: {}", a);

let b = foo("We love ".to_string(), "Rust!".to_string());
assert_eq!(b, "We love Rust!");
println!("concatenated result: {}", b);
}
21 changes: 21 additions & 0 deletions splat-overload-test/src/bin/return-values-zero-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

overload! {
fn foo() -> i32 { 42 }
fn foo(x: i32) -> i32 { x }
}

fn main() {
let a = foo();
assert_eq!(a, 42);
println!("zero args result: {}", a);

let b = foo(10i32);
assert_eq!(b, 10);
println!("one arg result: {}", b);
}
21 changes: 21 additions & 0 deletions splat-overload-test/src/bin/return-values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]
Comment thread
teor2345 marked this conversation as resolved.

use splat_overload::overload;

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

fn main() {
let a = foo(21);
assert_eq!(a, 42);
println!("i32 result: {}", a);

let b = foo(1.5);
assert_eq!(b, 3.0);
println!("f64 result: {}", b);
}
23 changes: 19 additions & 4 deletions splat-overload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,23 @@ pub fn overload(input: TokenStream) -> TokenStream {
arg_indices.push(quote! { self.#index });
}
}

let output_ty = match &func.sig.output {
syn::ReturnType::Default => quote! { () },
syn::ReturnType::Type(_, ty) => quote! { #ty },
};

// Check if there are no arguments
let tuple_ty = if arg_types.is_empty() {
quote! { () }
} else {
quote! { (#(#arg_types),*,) }
};

impls.push(quote! {
impl #trait_name for (#(#arg_types),*,) {
fn call(self) {
impl #trait_name for #tuple_ty {
type Output = #output_ty;
fn call(self) -> Self::Output{
#(let #arg_names = #arg_indices;)*
#block
}
Expand All @@ -75,12 +89,13 @@ pub fn overload(input: TokenStream) -> TokenStream {
}
let generated = quote! {
trait #trait_name: std::marker::Tuple {
fn call(self);
type Output;
fn call(self) -> Self::Output;
}

#(#impls)*

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