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

Use #[marker] on nightly in CI #1925

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 {}
};
}
Loading