Rust’s orphan rule prevents a crate from implementing a foreign trait for a foreign type, preserving coherence when multiple dependencies might otherwise provide conflicting implementations. The common alternatives are for the trait’s crate to add integrations for popular types or for users to wrap each type in a newtype. Built-in integrations cannot cover every library or accommodate every project’s preferred behavior, and can create version lock-in. Newtypes work but add boilerplate and clutter when a model uses many third-party types.
The proposed design is for a trait to include a defaulted generic context parameter, so existing users can continue using its default while a downstream crate defines a local marker type and uses it in implementations for foreign types. That local type makes the implementation distinct and coherence-safe; separate contexts can support different behaviors, such as formatting timestamps differently for APIs and internal services. The context’s position matters: Rust checks the self type and then trait parameters in order, so a bare generic parameter before the first local type can make an implementation invalid. Putting the context first is generally safest, and a type alias does not count as a local type. This pattern also lets a standalone models crate keep native field types and provide generic trait implementations, avoiding wrappers and heavy serialization or database dependencies in the models crate.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.