Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fat pointer trait implementations #28579

Closed
Diggsey opened this issue Sep 21, 2015 · 7 comments
Closed

Fat pointer trait implementations #28579

Diggsey opened this issue Sep 21, 2015 · 7 comments
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Diggsey
Copy link
Contributor

Diggsey commented Sep 21, 2015

Currently Hash is only implemented for thin pointers, and Eq/PartialEq aren't implemented for any pointers (but == works for all pointers).

All of these traits should be implemented for all pointers.

Without this, it's impossible to implement generic reference-equality semantics that support dynamically sized types, even in unsafe code, thanks to #27570. Even with #27570 it will still be impossible to do safely on stable rust, since raw::TraitObject is unstable, and even if it were stable, the type system isn't powerful enough to express when to use raw::TraitObject vs a thin pointer.

Fixing this properly will likely require improvements to the type system. Any improvement which solves rust-lang/rfcs#1279 should be enough.

@arielb1
Copy link
Contributor

arielb1 commented Sep 22, 2015

You can "dynamically" mem::transmute by using copy_memory (and size_of).

@Diggsey
Copy link
Contributor Author

Diggsey commented Sep 22, 2015

That's still unstable, and it's still nontrivial to implement Hash. You can do it with unsafe code and size_of, but it's not ideal - this sort of thing should be handled by the standard library so that user code doesn't have to depend on implementation details like the layout of fat pointers.

Without sufficiently smart optimizations, it will also generate sub-optimal code.

When it's possible to write a generic ReferenceEquality<T: Deref> wrapper that can be used to key a HashMap without requiring unsafe code, then this can be closed.

@arielb1
Copy link
Contributor

arielb1 commented Sep 22, 2015

Eq is implemented for all pointers, including fat ones:

fn check_eq<T: Eq>(t: T) { t == t; }
fn main() {
    let x: *mut [u8] = &mut [0,1];
    check_eq(x);
}

Anyway, why is

@Diggsey
Copy link
Contributor Author

Diggsey commented Sep 22, 2015

Eq is implemented for all pointers, including fat ones

Ah, it doesn't show up in rustdoc. I guess it's implemented internally in the compiler or something?

Anyway, why is

A truly deep question 💭

@arielb1
Copy link
Contributor

arielb1 commented Sep 25, 2015

http://doc.rust-lang.org/std/cmp/trait.Eq.html

has

impl<T> Eq for *const T where T: ?Sized

The impl (at https://github.com/rust-lang/rust/blob/master/src/libcore/ptr.rs#L279) just does *self == *other - the real implementation is in trans.

@arielb1
Copy link
Contributor

arielb1 commented Sep 25, 2015

It would be nice to have better primitives for accessing the fields of fat pointers, and at least implement Hash for them (not in the compiler, obviously, just using memcpy).

@steveklabnik
Copy link
Member

Triage: 18 months with no comments. It seems that the original issue is in fact not an issue. Closing!

@steveklabnik steveklabnik added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants