4.6 Interfaces
pub(api) interface Storage {
fn get(k: str) -> bytes? io
var fn put(k: str, v: bytes) io
fn has(k: str) -> bool io => get(k) != none // a default method
}
interface ReadWrite { embed Reader; embed Writer }
interface Container<T> { type Item = T; fn items() -> iter<T> }
@explicit interface Persistent { fn key() -> str }
assert FileStorage: Storage // static check- Interfaces are structural: a type
TsatisfiesIif for every method ofIwithout a default implementationThas a method with the same name, a compatible receiver (&self⊂&var self), the same parameter types, the same or a narrower return type and effects that are included in the effects of the interface method. - Default methods are added to a type when it is used through the interface; the type's own method takes priority.
- An
@explicit interfaceis satisfied only by an explicitimpl I for Tor@derive(I). - Use: as a generic constraint (
T: Storage, static dispatch) or as the typedyn Storage(dynamic, via a vtable).
Standard interfaces:
| Interface | Explicit | Purpose | derive |
|---|---|---|---|
Copy | yes | copying instead of moving (§3.9) | yes |
Clone | no | clone() | yes |
Drop | yes | destructor | no |
Eq | no | ==, != | yes |
Ord | no | <=> and comparisons | yes |
Hash | no | keys of Map/Set | yes |
Show | no | interpolation, logs | yes |
Default | no | T.default() | yes |
Serializable | yes | serialisation (§9) | automatic for data/enum |
Iterable<T> | no | source for for | — |
Streamable<T> | no | source for for await | — |
Middleware | yes | handler of rpc attributes (§10.2) | — |
Related diagnostics
explicit-interface, interface-not-satisfied
See also
Source: docs/spec/oxl-standard.en.md