-
Notifications
You must be signed in to change notification settings - Fork 1
Add method support to overload! macro #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } |
| 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); | ||
| } |
| 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); | ||
| } |
| 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()); | ||
| } |
| 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); | ||
| } |
| 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); | ||
| } |
| 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)] | ||
|
Check failure on line 4 in splat-overload-test/src/bin/methods.rs
|
||
|
|
||
| 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); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.