Skip to content

Commit

Permalink
fn CBox::as_ref: Add // SAFETY comment explaining why `data.point…
Browse files Browse the repository at this point in the history
…er.as_ref()` is safe.
  • Loading branch information
kkysen committed Jul 16, 2024
1 parent 3292eb3 commit 383b1c6
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/c_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl<T: ?Sized> AsRef<T> for CBox<T> {
fn as_ref(&self) -> &T {
match self {
Self::Rust(r#box) => r#box.as_ref(),
// SAFETY: `data` is a `Unique<T>`, which behaves as if it were a `T`,
// so we can take `&` references of it.
// Furthermore, `data` is never moved and is valid to dereference,
// so this reference can live as long as `CBox` and still be valid the whole time.
Self::C { data, .. } => unsafe { data.pointer.as_ref() },
}
}
Expand Down

0 comments on commit 383b1c6

Please sign in to comment.