Skip to main content

topcoat_view/
props.rs

1/// A props struct with a generated typestate builder.
2///
3/// Implemented by [`#[derive(Props)]`][derive]. The derive also generates an
4/// inherent `builder()` function on the struct, so this trait only needs to be
5/// in scope when the builder is constructed generically.
6///
7/// [derive]: derive.Props.html
8pub trait Props {
9    /// The builder type in its initial state, with no properties set.
10    type Builder;
11
12    /// Returns a builder with no properties set.
13    fn builder() -> Self::Builder;
14}
15
16/// Typestate marker for a required property that has been set.
17///
18/// Builders generated by [`#[derive(Props)]`][derive] track each required
19/// property in a generic argument. Calling the property's setter flips that
20/// argument to [`Set`]; before that it is a generated marker type named after
21/// the property.
22///
23/// [derive]: derive.Props.html
24pub struct Set;
25
26/// Implemented by typestate markers of properties that have been set.
27///
28/// `build()` on a generated builder requires every required property's marker
29/// to implement this trait. Markers of unset properties are types named after
30/// the property, so the compile error names exactly which property is missing.
31#[diagnostic::on_unimplemented(
32    message = "missing required property `{Self}`",
33    label = "the property `{Self}` was not set",
34    note = "every property without `#[default]` must be set before calling `build()`"
35)]
36pub trait IsSet {}
37
38impl IsSet for Set {}