Skip to content

Commit c56101f

Browse files
committed
Traits other than NoCell permit UnsafeCells
Previously, `FromZeros`, `FromBytes`, and `AsBytes` could not be implemented for types containing `UnsafeCell`s. This is a soundness precondition for some types of reference transmutations (notably transmuting either direction between `&[u8]` and `&T`). However, some of our machinery operates only on values (e.g. `transmute!`), and that machinery should in principle be able to support types which contain `UnsafeCell`s. In this commit, we remove the "no `UnsafeCell`" restriction from `FromZeros`, `FromBytes`, and `AsBytes`. We use the recently-added `NoCell` trait as a bound on individual functions and methods where `UnsafeCell`s would be unsound. This permits some APIs to support `UnsafeCell`s which could not previously support them. Closes #251
1 parent 62ee07e commit c56101f

File tree

82 files changed

+1152
-673
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1152
-673
lines changed

src/byteorder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
//!
3636
//! ```rust,edition2021
3737
//! # #[cfg(feature = "derive")] { // This example uses derives, and won't compile without them
38-
//! use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeros, Ref, Unaligned};
38+
//! use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeros, NoCell, Ref, Unaligned};
3939
//! use zerocopy::byteorder::network_endian::U16;
4040
//!
41-
//! #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
41+
//! #[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned)]
4242
//! #[repr(C)]
4343
//! struct UdpHeader {
4444
//! src_port: U16,
@@ -655,7 +655,7 @@ mod tests {
655655
use compatibility::*;
656656

657657
// A native integer type (u16, i32, etc).
658-
trait Native: Arbitrary + FromBytes + AsBytes + Copy + PartialEq + Debug {
658+
trait Native: Arbitrary + FromBytes + AsBytes + NoCell + Copy + PartialEq + Debug {
659659
const ZERO: Self;
660660
const MAX_VALUE: Self;
661661

@@ -692,7 +692,7 @@ mod tests {
692692
}
693693

694694
trait ByteArray:
695-
FromBytes + AsBytes + Copy + AsRef<[u8]> + AsMut<[u8]> + Debug + Default + Eq
695+
FromBytes + AsBytes + NoCell + Copy + AsRef<[u8]> + AsMut<[u8]> + Debug + Default + Eq
696696
{
697697
/// Invert the order of the bytes in the array.
698698
fn invert(self) -> Self;

0 commit comments

Comments
 (0)