Skip to content

Commit

Permalink
Use #[marker] on nightly in CI (#1925)
Browse files Browse the repository at this point in the history
We eventually hope to make use of `#[marker]` traits once they're
stable. This permits us to test to make sure the feature is as we expect
and that our intended usage works.

gherrit-pr-id: I3a111bf5647fdcc9805cbadf36f729ac69b28509
  • Loading branch information
joshlf authored Oct 17, 2024
1 parent f04c344 commit 70e5ef4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![cfg_attr(
__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS,
feature(layout_for_ptr, strict_provenance, coverage_attribute)
feature(layout_for_ptr, strict_provenance, coverage_attribute, marker_trait_attr)
)]

// This is a hack to allow zerocopy-derive derives to work in this crate. They
Expand Down
43 changes: 20 additions & 23 deletions src/pointer/invariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,27 @@ impl Validity for Valid {
///
/// As a consequence, if `T: Read<A, R>`, then any `Ptr<T, (A, ...)>` is
/// permitted to perform unsynchronized reads from its referent.
pub trait Read<A: Aliasing, R: ReadReason> {}
#[cfg_attr(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS, marker)]
pub unsafe trait Read<A: Aliasing, R> {}

impl<A: Reference, T: ?Sized + crate::Immutable> Read<A, BecauseImmutable> for T {}
impl<T: ?Sized> Read<Exclusive, BecauseExclusive> for T {}

/// Used to disambiguate [`Read`] impls.
pub trait ReadReason: Sealed {}

/// Unsynchronized reads are permitted because only one live [`Ptr`](crate::Ptr)
/// or reference may exist to the referent bytes at a time.
#[derive(Copy, Clone, Debug)]
#[doc(hidden)]
pub enum BecauseExclusive {}
impl ReadReason for BecauseExclusive {}

/// Unsynchronized reads are permitted because no live [`Ptr`](crate::Ptr)s or
/// references permit interior mutation.
#[derive(Copy, Clone, Debug)]
#[doc(hidden)]
pub enum BecauseImmutable {}
impl ReadReason for BecauseImmutable {}
define_because!(
/// Unsynchronized reads are permitted because only one live
/// [`Ptr`](crate::Ptr) or reference may exist to the referent bytes at a
/// time.
#[doc(hidden)]
pub BecauseExclusive
);
// SAFETY: The aliasing parameter is `Exclusive`.
unsafe impl<T: ?Sized> Read<Exclusive, BecauseExclusive> for T {}

define_because!(
/// Unsynchronized reads are permitted because no live [`Ptr`](crate::Ptr)s
/// or references permit interior mutation.
#[doc(hidden)]
pub BecauseImmutable
);
// SAFETY: `T: Immutable`.
unsafe impl<A: Reference, T: ?Sized + crate::Immutable> Read<A, BecauseImmutable> for T {}

use sealed::Sealed;
mod sealed {
Expand All @@ -251,9 +251,6 @@ mod sealed {
impl Sealed for Valid {}

impl<A: Sealed, AA: Sealed, V: Sealed> Sealed for (A, AA, V) {}

impl Sealed for BecauseImmutable {}
impl Sealed for BecauseExclusive {}
}

pub use mapping::*;
Expand Down
3 changes: 1 addition & 2 deletions src/pointer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod invariant;
mod ptr;

#[doc(hidden)]
pub use invariant::{BecauseExclusive, BecauseImmutable, Read, ReadReason};
pub use invariant::{BecauseExclusive, BecauseImmutable, Read};
#[doc(hidden)]
pub use ptr::Ptr;

Expand Down Expand Up @@ -48,7 +48,6 @@ where
pub fn read_unaligned<R>(self) -> T
where
T: Copy,
R: invariant::ReadReason,
T: invariant::Read<Aliasing, R>,
{
// SAFETY: By invariant on `MaybeAligned`, `raw` contains
Expand Down
6 changes: 0 additions & 6 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,6 @@ mod _transitions {
T: TryFromBytes + Read<I::Aliasing, R>,
I::Aliasing: Reference,
I: Invariants<Validity = Initialized>,
R: crate::pointer::ReadReason,
{
// This call may panic. If that happens, it doesn't cause any soundness
// issues, as we have not generated any invalid state which we need to
Expand Down Expand Up @@ -805,8 +804,6 @@ mod _casts {
where
T: Read<I::Aliasing, R>,
U: 'a + ?Sized + Read<I::Aliasing, S>,
R: ReadReason,
S: ReadReason,
F: FnOnce(*mut T) -> *mut U,
{
// SAFETY: Because `T` and `U` both implement `Read<I::Aliasing, _>`,
Expand All @@ -829,7 +826,6 @@ mod _casts {
#[allow(clippy::wrong_self_convention)]
pub(crate) fn as_bytes<R>(self) -> Ptr<'a, [u8], (I::Aliasing, Aligned, Valid)>
where
R: ReadReason,
T: Read<I::Aliasing, R>,
I::Aliasing: Reference,
{
Expand Down Expand Up @@ -919,7 +915,6 @@ mod _casts {
CastError<Self, U>,
>
where
R: ReadReason,
I::Aliasing: Reference,
U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
{
Expand Down Expand Up @@ -983,7 +978,6 @@ mod _casts {
where
I::Aliasing: Reference,
U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
R: ReadReason,
{
match self.try_cast_into(CastType::Prefix, meta) {
Ok((slf, remainder)) => {
Expand Down
3 changes: 1 addition & 2 deletions src/util/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use core::mem::{self, ManuallyDrop};
use core::ptr::{self, NonNull};

use crate::{
pointer::invariant::{self, BecauseExclusive, BecauseImmutable, Invariants, ReadReason},
pointer::invariant::{self, BecauseExclusive, BecauseImmutable, Invariants},
Immutable, IntoBytes, Ptr, TryFromBytes, Unalign, ValidityError,
};

Expand Down Expand Up @@ -528,7 +528,6 @@ where
Dst: TryFromBytes + invariant::Read<I::Aliasing, R>,
I: Invariants<Validity = invariant::Valid>,
I::Aliasing: invariant::Reference,
R: ReadReason,
{
static_assert!(Src, Dst => mem::size_of::<Dst>() == mem::size_of::<Src>());

Expand Down
11 changes: 11 additions & 0 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,14 @@ macro_rules! static_assert_dst_is_not_zst {
}, "cannot call this method on a dynamically-sized type whose trailing slice element is zero-sized");
}}
}

macro_rules! define_because {
($(#[$attr:meta])* $vis:vis $name:ident) => {
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)]
$(#[$attr])*
$vis type $name = ();
#[cfg(not(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS))]
$(#[$attr])*
$vis enum $name {}
};
}

0 comments on commit 70e5ef4

Please sign in to comment.