Sealed traits in oauth2-rs — closing the typestate loophole
The first sealed-trait post showed the mechanic in isolation. Here is why it shows up in oauth2-rs: endpoint state in the client API. That design only works if callers cannot add their own state types.
Reuse the previous example, wrapped in mod oauth to mimic a library surface:
HackEndpointSet can no longer implement EndpointState. The compiler error includes the sealed-trait note:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
error[E0277]: the trait bound `HackEndpointSet: EndpointStateSealed` is not satisfied --> src/main.rs:71:28 | 71 | impl EndpointState for HackEndpointSet {} | ^^^^^^^^^^^^^^^ the trait `EndpointStateSealed` is not implemented for `HackEndpointSet` | = help: the following other types implement trait `EndpointStateSealed`: oauth::EndointSet EndpointNotSet note: required by a bound in `oauth::EndpointState` --> src/main.rs:12:30 | 12 | pub trait EndpointState: private::EndpointStateSealed {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `EndpointState` = note: `EndpointState` is a "sealed trait", because to implement it you also need to implement `oauth::private::EndpointStateSealed`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it = help: the following types implement the trait: oauth::EndointSet oauth::EndpointNotSet