Last active
August 22, 2022 13:55
-
-
Save sahandevs/00ac326829c83926e78fd91e3a37e883 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
struct ItemBuilder<const A: bool, const B: bool>; | |
impl<const A: bool, const B: bool> ItemBuilder<A, B> { | |
fn build(self) -> usize | |
where | |
Self: HasA + HasB, | |
{ | |
0 | |
} | |
} | |
#[rustc_on_unimplemented( | |
message = "missing the required a field", | |
note = "use `builder.a(value)` to add the field", | |
)] | |
trait HasA {} | |
impl<const B: bool> HasA for ItemBuilder<true, B> {} | |
#[rustc_on_unimplemented( | |
message = "missing the required `b` field", | |
note = "use `builder.b(value)` to add the field", | |
)] | |
trait HasB {} | |
impl<const A: bool> HasB for ItemBuilder<A, true> {} | |
fn test() { | |
let builder = ItemBuilder::<false, false>; | |
builder.build(); | |
let builder = ItemBuilder::<true, false>; | |
builder.build(); | |
let builder = ItemBuilder::<false, true>; | |
builder.build(); | |
let builder = ItemBuilder::<true, true>; | |
builder.build(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment