Skip to content

Deref implementation for newtypes #18

@pip25

Description

@pip25

First of all, thanks for the great summary of the orphan rule and its surrounding issues, it's been very helpful for me as I tried to understand the overall topic.

One thing the text and linked issues do not seem to mention is using Deref for newtypes to avoid boilerplate. Now I understand that this topic might be controversial, the unofficial rust pattern repo for instance mentions implementing Deref in this case as an anti-pattern, but on the other hand the much more recent second edition of the Rust introductory book now mentions using Deref on newtypes as a practical solution.

So, what is the current consensus on this subject? Is it such a non-starter that it's not even worth mentioning, or was it not included here for some other reason? Because, while I'm definitely a beginner in Rust, to me the whole thing seems pretty tempting: you do not need to write delegate methods pretty much at all, you can implement new traits on the newtype as normal, and you can also use the trait implementations of the wrapped type with the dereference operator (*).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions