Skip to content

Commit 845c5e7

Browse files
committed
[WIP] Use ReadOnly in is_bit_valid
gherrit-pr-id: G7691845b6b02e9f3d9578435d732bacfa6ca674f
1 parent 45ff87c commit 845c5e7

14 files changed

Lines changed: 514 additions & 298 deletions

File tree

src/impls.rs

Lines changed: 110 additions & 88 deletions
Large diffs are not rendered by default.

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ pub unsafe trait TryFromBytes {
15661566
/// [`UnsafeCell`]: core::cell::UnsafeCell
15671567
/// [`Shared`]: invariant::Shared
15681568
#[doc(hidden)]
1569-
fn is_bit_valid<A: invariant::Reference>(candidate: Maybe<'_, Self, A>) -> bool;
1569+
fn is_bit_valid(candidate: Maybe<'_, Self>) -> bool;
15701570

15711571
/// Attempts to interpret the given `source` as a `&Self`.
15721572
///
@@ -2974,7 +2974,7 @@ unsafe fn try_read_from<S, T: TryFromBytes>(
29742974
// via `c_ptr` so long as it is live, so we don't need to worry about the
29752975
// fact that `c_ptr` may have more restricted validity than `candidate`.
29762976
let c_ptr = unsafe { c_ptr.assume_validity::<invariant::Initialized>() };
2977-
let c_ptr = c_ptr.transmute();
2977+
let mut c_ptr = c_ptr.cast::<_, crate::pointer::cast::CastSized, _>();
29782978

29792979
// Since we don't have `T: KnownLayout`, we hack around that by using
29802980
// `Wrapping<T>`, which implements `KnownLayout` even if `T` doesn't.
@@ -2987,7 +2987,7 @@ unsafe fn try_read_from<S, T: TryFromBytes>(
29872987
// `try_into_valid` (and thus `is_bit_valid`) with a shared pointer when
29882988
// `Self: !Immutable`. Since `Self: Immutable`, this panic condition will
29892989
// not happen.
2990-
if !Wrapping::<T>::is_bit_valid(c_ptr.forget_aligned()) {
2990+
if !Wrapping::<T>::is_bit_valid(c_ptr.reborrow_shared().forget_aligned()) {
29912991
return Err(ValidityError::new(source).into());
29922992
}
29932993

src/macros.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -895,10 +895,8 @@ macro_rules! cryptocorrosion_derive_traits {
895895
$($field_ty: $crate::FromBytes,)*
896896
)?
897897
{
898-
fn is_bit_valid<A>(_c: $crate::Maybe<'_, Self, A>) -> bool
899-
where
900-
A: $crate::pointer::invariant::Reference
901-
{
898+
#[inline]
899+
fn is_bit_valid(_c: $crate::Maybe<'_, Self>) -> bool {
902900
// SAFETY: This macro only accepts `#[repr(C)]` and
903901
// `#[repr(transparent)]` structs, and this `impl` block
904902
// requires all field types to be `FromBytes`. Thus, all
@@ -1038,10 +1036,8 @@ macro_rules! cryptocorrosion_derive_traits {
10381036
$field_ty: $crate::FromBytes,
10391037
)*
10401038
{
1041-
fn is_bit_valid<A>(_c: $crate::Maybe<'_, Self, A>) -> bool
1042-
where
1043-
A: $crate::pointer::invariant::Reference
1044-
{
1039+
#[inline]
1040+
fn is_bit_valid(_c: $crate::Maybe<'_, Self>) -> bool {
10451041
// SAFETY: This macro only accepts `#[repr(C)]` unions, and this
10461042
// `impl` block requires all field types to be `FromBytes`.
10471043
// Thus, all initialized byte sequences constitutes valid

src/pointer/mod.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ pub use {
2222
ptr::Ptr,
2323
};
2424

25+
use crate::wrappers::ReadOnly;
26+
2527
/// A shorthand for a maybe-valid, maybe-aligned reference. Used as the argument
2628
/// to [`TryFromBytes::is_bit_valid`].
2729
///
2830
/// [`TryFromBytes::is_bit_valid`]: crate::TryFromBytes::is_bit_valid
2931
pub type Maybe<'a, T, Aliasing = invariant::Shared, Alignment = invariant::Unaligned> =
30-
Ptr<'a, T, (Aliasing, Alignment, invariant::Initialized)>;
32+
Ptr<'a, ReadOnly<T>, (Aliasing, Alignment, invariant::Initialized)>;
3133

3234
/// Checks if the referent is zeroed.
3335
pub(crate) fn is_zeroed<T, I>(ptr: Ptr<'_, T, I>) -> bool
@@ -338,28 +340,18 @@ pub mod cast {
338340
type WrappedField = Wrapping<F>;
339341
}
340342

341-
#[allow(missing_debug_implementations, missing_copy_implementations)]
342-
pub struct WrappedProjection<W: ?Sized, F, const VARIANT_ID: i128, const FIELD_ID: i128> {
343-
_never: core::convert::Infallible,
344-
_phantom: PhantomData<(F, W)>,
345-
}
346-
347-
// SAFETY: TODO
348-
unsafe impl<W: ?Sized, F, const VARIANT_ID: i128, const FIELD_ID: i128>
349-
Project<W, W::WrappedField> for WrappedProjection<W, F, VARIANT_ID, FIELD_ID>
350-
where
351-
W: Wrapped
352-
+ HasWrappedField<<<W as Wrapped>::Unwrapped as HasField<F, VARIANT_ID, FIELD_ID>>::Type>,
353-
W::Unwrapped: HasField<F, VARIANT_ID, FIELD_ID>,
354-
{
355-
#[inline(always)]
356-
fn project_inner(src: PtrInner<'_, W>) -> *mut W::WrappedField {
357-
src.project::<_, W::CastToUnwrapped>()
358-
.project::<_, Projection<F, VARIANT_ID, FIELD_ID>>()
359-
.project::<_, <W::WrappedField as Wrapped>::CastFromUnwrapped>()
360-
.as_ptr()
361-
}
362-
}
343+
pub type WrappedProjection<W, F, const VARIANT_ID: i128, const FIELD_ID: i128> =
344+
TransitiveProject<
345+
<<W as Wrapped>::Unwrapped as HasField<F, VARIANT_ID, FIELD_ID>>::Type,
346+
TransitiveProject<
347+
<W as Wrapped>::Unwrapped,
348+
<W as Wrapped>::CastToUnwrapped,
349+
Projection<F, VARIANT_ID, FIELD_ID>,
350+
>,
351+
<<W as HasWrappedField<
352+
<<W as Wrapped>::Unwrapped as HasField<F, VARIANT_ID, FIELD_ID>>::Type,
353+
>>::WrappedField as Wrapped>::CastFromUnwrapped,
354+
>;
363355

364356
/// A transitive sequence of projections.
365357
///

0 commit comments

Comments
 (0)