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

Update unsafe trait example to zerocopy version 0.8 #2434

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/unsafe-rust/unsafe-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,35 @@ 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):
[something like this](https://docs.rs/zerocopy/latest/zerocopy/trait.IntoBytes.html):

```rust,editable
use std::mem::size_of_val;
use std::slice;
use std::{mem, 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),
)
}
pub unsafe trait IntoBytes {
fn as_bytes(&self) -> &[u8]
where
Self: Immutable,
{
let len = mem::size_of_val(self);
let slf: *const Self = self;
unsafe { slice::from_raw_parts(slf.cast::<u8>(), len) }
}
}

// SAFETY: `u32` has a defined representation and no padding.
unsafe impl AsBytes for u32 {}
/// Types which are free from interior mutability.
pub unsafe trait Immutable {}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mgeisler, I think it had to be added to make the tests pass.

Copy link
Collaborator

@fw-immunant fw-immunant Oct 18, 2024

Choose a reason for hiding this comment

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

Shouldn't we still have a SAFETY comment here, given the unsafe impl?

EDIT: Oh, I misread this change. It would be ideal for this slide to still actually demonstrate an unsafe impl as that is the unsafe capability being discussed here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

EDIT: Oh, I misread this change. It would be ideal for this slide to still actually demonstrate an unsafe impl as that is the unsafe capability being discussed here.

Yeah, I agree we should keep the unsafe impl.

@semihbkgr, could you simplify the IntoBytes trait and remove the where Self: Immutable bound? The code has to be super simple and just demonstrate the concept of an unsafe trait (it should not be 100% like what zerocopy has).

```

<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 actual safety section for `IntoBytes` is rather longer and more complicated.

The built-in `Send` and `Sync` traits are unsafe.

Expand Down