rust-lang / nomicon

The Dark Arts of Advanced and Unsafe Rust Programming
https://doc.rust-lang.org/nomicon/
Apache License 2.0
1.79k stars 261 forks source link

Add example of thinking about Send/Sync's soundness #259

Closed dzfranklin closed 3 years ago

dzfranklin commented 3 years ago

Add an example of thinking through whether it is sound to implement Send + Sync for a custom type that wraps a raw pointer.

I read the existing docs and was confused about whether I could implement Send + Sync for a type I wrote that wraps a c-style array. Kixiron, InfernoDeity, Talchas, and HeroicKatora on #black-magic helped me understand Send and Sync better. This example is based on the advice they gave me. I've made lots of changes, so any errors are probably mine.

dzfranklin commented 3 years ago

I think my example would be improved by an example of a change that would make Carton Send but not Sync and neither Send nor Sync, but I can't think of any simples ones that don't violate the safety invarients of casting pointers.

dzfranklin commented 3 years ago

Thank you everyone. I appreciate you all taking the time to point out my mistakes.

I have one question. I used rust,ignore because I got a build error that libc wasn't available and I'd otherwise a lot of hidden duplicate lines to make each example compile on its own. I saw a lot of other examples do the same. Is there a different solution I should do?

JohnTitor commented 3 years ago

I have one question. I used rust,ignore because I got a build error that libc wasn't available and I'd otherwise a lot of hidden duplicate lines to make each example compile on its own. I saw a lot of other examples do the same. Is there a different solution I should do?

I'd like to avoid using rust,ignore so that we can notice if it's stalled. And note that I don't put suggestions for the snippet that uses libc but for the snippets that don't have any external deps. Indeed, we have a bunch of rust,ignore snippets but I'm planning to re-evaluate and clean-up it once I get some time to it.

dzfranklin commented 3 years ago

I believe all the issues are addressed.

dzfranklin commented 3 years ago

I think this is ready @JohnTitor . Thank you everyone for helping me fix my code!

dzfranklin commented 3 years ago

Thanks for catching those issues. All fixed.

JohnTitor commented 3 years ago

Thanks again for the great work!

dzfranklin commented 3 years ago

Thank you for holding my hand so much! I've learned a lot.