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

Remove ptr-int transmute by converting UnsafeWeakPointer from usize to *mut T #127

Merged
merged 2 commits into from May 19, 2022
Merged

Conversation

saethlin
Copy link
Contributor

This transmute from a Weak<T> to a usize is shockingly common in both the ecosystem and the standard library. It's doubly bad because Weak<T> is a repr(Rust) type, so without manually inspecting its layout at runtime it is a mistake to assume it has any particular layout. But primarily, this is a round-trip of a pointer through an integer type via transmute, and it's pretty clear Rust cannot have a sound memory model that permits these alongside the usual suite of compiler optimizations (casts are different, they're annoying but possible to support).

If you're looking for other examples of this approach, rayon does a very similar thing to get pointers between threads: https://github.com/rayon-rs/rayon/blob/5b6adbbf5c6944ec42b54530820ee8c0ad0e35a9/src/lib.rs#L123-L147

Copy link
Member

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing and fixing the unsafe codes. Greatly appreciated!

I left a comment about reverting the crossbeam-epoch version. All other parts look great to me.

Cargo.toml Outdated Show resolved Hide resolved
@tatsuya6502 tatsuya6502 self-requested a review May 19, 2022 14:44
Copy link
Member

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. It looks good to me!

Cargo.toml Outdated Show resolved Hide resolved
@tatsuya6502 tatsuya6502 merged commit 90db6a8 into moka-rs:master May 19, 2022
@tatsuya6502 tatsuya6502 added this to the v0.8.4 milestone May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants