Skip to content

Instantly share code, notes, and snippets.

@sahandevs
Last active August 22, 2022 13:55
Show Gist options
  • Save sahandevs/00ac326829c83926e78fd91e3a37e883 to your computer and use it in GitHub Desktop.
Save sahandevs/00ac326829c83926e78fd91e3a37e883 to your computer and use it in GitHub Desktop.
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