Skip to content

Conventions for the use of Deref and DerefMut #279

@rust-highfive

Description

@rust-highfive

This was discussed previously in rust-lang/rust#13126.

Tracking issue for postponed PR #25

Activity

added
postponedRFCs that have been postponed and may be revisited at a later time.
on Sep 24, 2014
nrc

nrc commented on Dec 25, 2014

@nrc
Member

I am keen to see some guidance here. In particular, I find deref'ing to an unrelated type for the sake of convenience a confusing anti-pattern. My preference would be for deref to only be used for getting the contents of a smart pointer and for taking a slice of a collection, but I'm sure there are other similar cases I haven't thought of. However, using deref for polymorphism using method calls seems pretty bad.

cc @aturon

huonw

huonw commented on Dec 26, 2014

@huonw
Member

What is 'unrelated'?

aturon

aturon commented on Dec 27, 2014

@aturon
Member

@nick29581 Yes, I am also anxious to establish some guidelines here; it's coming up increasingly often in std API designs.

nrc

nrc commented on Mar 6, 2015

@nrc
Member

Also InternedString in syntax::parse::token this is a wrapper around a wrapper around an Rc<str> and so it sort of makes sense for it to be Deref. However, to me that seems like an implementation detail, because it is advertised as a kind of string, rather than a smart pointer. So, it seems that Deref is wrong in this case. But perhaps it should be deref for the same reason String is. In any case it seems borderline and thus an interesting case to consider.

brson

brson commented on Mar 6, 2015

@brson
Contributor

Derefing an InternedString in rustc can panic if the TLS invariants are violated, unlike with String.

added
T-docRelevant to the documentation team, which will review and decide on the RFC.
on Aug 17, 2016
added
A-conventionProposals relating to documentation conventions.
on Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-conventionProposals relating to documentation conventions.T-docRelevant to the documentation team, which will review and decide on the RFC.postponedRFCs that have been postponed and may be revisited at a later time.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @whitequark@brson@aturon@nrc@Centril

        Issue actions

          Conventions for the use of `Deref` and `DerefMut` · Issue #279 · rust-lang/rfcs