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
7 changes: 7 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ jobs:
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
cargo run --all-features --bin methods
cargo run --all-features --bin methods-multiple-args
cargo run --all-features --bin methods-return-values
cargo run --all-features --bin methods-mut-self
cargo run --all-features --bin methods-owned-self
cargo run --all-features --bin methods-complex
cargo run --all-features --bin methods-non-copy-arg

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

use splat_overload::overload;

struct Validator {
threshold: i32,
}

overload! {
impl Validator {
fn check(&self) -> bool { self.threshold > 0 }
fn check(&self, x: i32) -> bool {
if x < 0 {
return false;
}
x >= self.threshold
}
fn check(&self, x: i32, y: i32) -> Vec<bool> {
vec![x >= self.threshold, y >= self.threshold]
}
}
}

fn main() {
let v = Validator { threshold: 10 };

assert!(v.check());
println!("zero-arg check: {}", v.check());

assert!(!v.check(-5i32), "the check must return false");
assert!(v.check(15i32));
println!("one-arg checks passed");

let results = v.check(5i32, 20i32);
assert_eq!(results, vec![false, true]);
println!("two-arg check: {:?}", results);
}
21 changes: 21 additions & 0 deletions splat-overload-test/src/bin/methods-multiple-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;

struct Calculator;

overload! {
impl Calculator {
fn compute(&self, x: i32, y: i32) { println!("sum: {}", x + y); }
fn compute(&self, x: f64, y: f64, z: f64) { println!("average: {}", (x + y + z) / 3.0); }
}
}

fn main() {
let calc = Calculator;
calc.compute(10i32, 20i32);
calc.compute(1.0f64, 2.0f64, 3.0f64);
}
29 changes: 29 additions & 0 deletions splat-overload-test/src/bin/methods-mut-self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

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; }
}
}

fn main() {
let mut counter = Counter { value: 0 };

counter.add(5i32);
assert_eq!(counter.value, 5);
println!("after add(5): {}", counter.value);

counter.add(3i32, 4i32);
assert_eq!(counter.value, 12);
println!("after add(3, 4): {}", counter.value);
}
25 changes: 25 additions & 0 deletions splat-overload-test/src/bin/methods-non-copy-arg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

struct Logger {
prefix: String,
}

overload! {
impl Logger {
fn log(&self, message: String) { println!("{}: {}", self.prefix, message); }
fn log(&self, message: String, level: String) { println!("{} [{}]: {}", self.prefix, level, message); }
}
}

fn main() {
let logger = Logger {
prefix: "APP".to_string(),
};
logger.log("starting up".to_string());
logger.log("something happened".to_string(), "WARN".to_string());
}
33 changes: 33 additions & 0 deletions splat-overload-test/src/bin/methods-owned-self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

struct Builder {
parts: Vec<String>,
}

overload! {
impl Builder {
fn build(self) -> String { self.parts.join("") }
fn build(self, sep: String) -> String { self.parts.join(&sep) }
}
}

fn main() {
let b = Builder {
parts: vec!["a".to_string(), "b".to_string(), "c".to_string()],
};
let result = b.build();
assert_eq!(result, "abc");
println!("joined: {}", result);

let b2 = Builder {
parts: vec!["x".to_string(), "y".to_string(), "z".to_string()],
};
let result2 = b2.build(", ".to_string());
assert_eq!(result2, "x, y, z");
println!("joined with sep: {}", result2);
}
27 changes: 27 additions & 0 deletions splat-overload-test/src/bin/methods-return-values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#![feature(splat)]
#![feature(tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

struct Calculator;

overload! {
impl Calculator {
fn compute(&self, x: i32) -> i32 { x * 2 }
fn compute(&self, x: i32, y: i32) -> i32 { x + y }
}
}

fn main() {
let calc = Calculator;

let a = calc.compute(21i32);
assert_eq!(a, 42);
println!("single arg result: {}", a);

let b = calc.compute(10i32, 32i32);
assert_eq!(b, 42);
println!("two arg result: {}", b);
}
21 changes: 21 additions & 0 deletions splat-overload-test/src/bin/methods.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, clippy::disallowed_names)]

use splat_overload::overload;

struct Foo;

overload! {
impl Foo {
fn method(&self, x: i32) { println!("i32: {}", x); }
fn method(&self, x: f64) { println!("f64: {}", x); }
}
}

fn main() {
let foo = Foo;
foo.method(42i32);
foo.method(3.1f64);
}
Loading