Skip to content

Provide a function to compare pointers and references for equality #1155

@RalfJung

Description

@RalfJung

Currently, Rust lacks a nice way to compare two values of type &T or Cell<T> for identity of references (as opposed to identity of the stored values). With a, b having type &T, one has to write a as *const _ == a as *const _, which is not exactly discoverable nor nice to look at.

We (eddyb, kimundi and me) discussed this on IRC, kimundi made the following proposal:

 fn ptr_eq<T>(a: *const T, b: *const T) -> bool { a == b }

Thanks to coercions, in the above scenario, we can now write "ptr_eq(a, b)". eddyb was opposed to a function providing "reference" equality on &T, due to the difference of references in Rust and other languages (in particular, involving zero-sized types).

I think some nice way of comparing pointers (references) for equality should be provided, that does not force the user to manually write down a cast to a "dangerous" raw pointer (though of course the usage above is completely safe).

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions