1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-20 21:18:26 +02:00

Add slide for unsafe traits.

This commit is contained in:
Andrew Walbran 2023-01-17 16:41:51 +00:00
parent 8c2b3781bb
commit 3cadad4e0a
2 changed files with 38 additions and 0 deletions

View File

@ -166,6 +166,7 @@
- [Unions](unsafe/unions.md)
- [Calling Unsafe Functions](unsafe/unsafe-functions.md)
- [Extern Functions](unsafe/extern-functions.md)
- [Implementing Unsafe Traits](unsafe/unsafe-traits.md)
- [Exercises](exercises/day-3/afternoon.md)
- [Safe FFI Wrapper](exercises/day-3/safe-ffi-wrapper.md)

View File

@ -0,0 +1,37 @@
# Implementing Unsafe Traits
Like with functions, you can mark a trait as `unsafe` if the implementation must guarantee
particular conditions to avoid undefined behaviour.
For example, the `zerocopy` crate has an unsafe trait that looks
[something like this](https://docs.rs/zerocopy/latest/zerocopy/trait.AsBytes.html):
```rust,editable
use std::mem::size_of_val;
use std::slice;
/// ...
/// # Safety
/// The type must have a defined representation and no padding.
pub unsafe trait AsBytes {
fn as_bytes(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self as *const Self as *const u8, size_of_val(self))
}
}
}
// Safe because u32 has a defined representation and no padding.
unsafe impl AsBytes for u32 {}
```
<details>
There should be a `# Safety` section on the Rustdoc for the trait explaining the requirements for
the trait to be safely implemented.
The actual safety section for `AsBytes` is rather longer and more complicated.
The built-in `Send` and `Sync` traits are unsafe.
</details>