I’m interested in this but this section kinda threw me so I halted for the time being. I tried to give a little detailed feedback below.
Note: I don’t mind being perplexed by things that will be explained later if it’s noted where to look for answers or a brief explanation is given which assuages my perplexity.
As of Rust 1.0 there are exactly two unsafe traits:
- Send is a marker trait (it has no actual API) that promises implementors
are safe to send to another thread.
- Sync is a marker trait that promises that threads can safely share
implementors through a shared reference.
Okay so, there are unsafe traits so I get this.
All other traits that declare any kind of contract really can’t be trusted
to adhere to their contract when memory-safety is at stake.
Immediately this is strange. All other traits must be safe if those are the only 2 unsafe traits. So, memory safety seems a very weak promise? Not sure. Maybe the next section will elaborate.
For instance Rust has
PartialOrd and Ord to differentiate between types which can “just” be
compared and those that implement a total ordering. However you can’t actually
trust an implementor of Ord to actually provide a total ordering if failing to
do so causes you to e.g. index out of bounds. But if it just makes your program
do a stupid thing, then it’s “fine” to rely on Ord.
Whatever this is explaining isn’t obvious to me. Oh, sorta get it after more consideration but I don’t think what is written is very obvious. I’m not sure why index out of bounds is even a consideration. Seems sorta unsafe but it isn’t on that unsafe things list. How is a stupid thing any different from indexing out of bounds. Both seem bad…
The reason this is the case
This isn’t helpful as it relies on understanding of the previous paragraph.
is that Ord is safe to implement, and it should be
impossible for bad safe code to violate memory safety.
So safe seems bad now along with unsafe…
Rust has traditionally
avoided making traits unsafe because it makes unsafe pervasive in the language,
which is not desirable.
The only reason Send and Sync are unsafe is because
thread safety is a sort of fundamental thing that a program can’t really guard
against locally (even by-value message passing still requires a notion Send).
I don’t get the explanation for why Send and Sync must be unsafe…