▲ | filleduchaos 2 days ago | |
> If you want to interact with your newtypes, you need to either unwrap it or reimplement each typeclass/trait ...or you could just e.g. implement Deref in Rust? In my experience that solves almost all use cases (with the edge case being when something wants to take ownership of the wrapped value, at which point I don't see the problem with unwrapping) | ||
▲ | itishappy 2 days ago | parent [-] | |
That gets us halfway there. It makes unwrapping easy, but you still need to remember to rewrap if you've implemented anything.
Also using newtypes to reimplement methods on the base type is frowned upon. I believe that this is why #[derive(Deref)] isn't included in the standard library. See below (emphasis mine):> So, as a simple, first-order takeaway: if the wrapper is a trivial marker, then it can implement Deref. If the wrapper's entire purpose is to manage its inner type, without modifying the extant semantics of that type, it should implement Deref. If T behaves differently than Target when Target would compile with that usage, it shouldn't implement Deref. https://users.rust-lang.org/t/should-you-implement-deref-for... |