-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from vic1707/constant-delegation
Constant delegation
- Loading branch information
Showing
5 changed files
with
253 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
extern crate delegate; | ||
use delegate::delegate; | ||
|
||
#[test] | ||
fn test_delegate_constant() { | ||
trait WithConst { | ||
const TOTO: u8; | ||
} | ||
|
||
struct A; | ||
impl WithConst for A { | ||
const TOTO: u8 = 1; | ||
} | ||
|
||
struct B; | ||
impl WithConst for B { | ||
const TOTO: u8 = 2; | ||
} | ||
struct C; | ||
impl WithConst for C { | ||
const TOTO: u8 = 2; | ||
} | ||
|
||
enum Enum { | ||
A(A), | ||
B(B), | ||
C(C), | ||
} | ||
|
||
impl Enum { | ||
delegate! { | ||
to match self { | ||
Self::A(a) => a, | ||
Self::B(b) => b, | ||
Self::C(c) => { println!("hello from c"); c }, | ||
} { | ||
#[const(WithConst::TOTO)] | ||
fn get_toto(&self) -> u8; | ||
} | ||
} | ||
} | ||
|
||
let a = Enum::A(A); | ||
assert_eq!(a.get_toto(), <A as WithConst>::TOTO); | ||
let b = Enum::B(B); | ||
assert_eq!(b.get_toto(), <B as WithConst>::TOTO); | ||
let c = Enum::C(C); | ||
assert_eq!(c.get_toto(), <C as WithConst>::TOTO); | ||
} | ||
|
||
#[test] | ||
fn multiple_consts() { | ||
trait Foo { | ||
const A: u32; | ||
const B: u32; | ||
} | ||
|
||
struct A; | ||
impl Foo for A { | ||
const A: u32 = 1; | ||
const B: u32 = 2; | ||
} | ||
|
||
struct Wrapper(A); | ||
impl Wrapper { | ||
delegate! { | ||
to &self.0 { | ||
#[const(Foo::A)] | ||
fn a(&self) -> u32; | ||
|
||
#[const(Foo::B)] | ||
fn b(&self) -> u32; | ||
} | ||
} | ||
} | ||
|
||
let wrapper = Wrapper(A); | ||
assert_eq!(wrapper.a(), <A as Foo>::A); | ||
assert_eq!(wrapper.b(), <A as Foo>::B); | ||
} |