Skip to content

Commit

Permalink
[pointer] Support Box and Arc
Browse files Browse the repository at this point in the history
gherrit-pr-id: I7fe90063e148d89e2f75b6fa63a960ad8b1dd432
  • Loading branch information
joshlf committed Oct 15, 2024
1 parent 99a8250 commit c098b73
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 65 deletions.
8 changes: 4 additions & 4 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ unsafe impl<T: TryFromBytes + ?Sized> TryFromBytes for UnsafeCell<T> {
}

#[inline]
fn is_bit_valid<A: invariant::Reference>(candidate: Maybe<'_, Self, A>) -> bool {
fn is_bit_valid<A: invariant::ReadFoo>(candidate: Maybe<'_, Self, A>) -> bool {
// The only way to implement this function is using an exclusive-aliased
// pointer. `UnsafeCell`s cannot be read via shared-aliased pointers
// (other than by using `unsafe` code, which we can't use since we can't
Expand Down Expand Up @@ -1124,15 +1124,15 @@ mod tests {

pub(super) trait TestIsBitValidShared<T: ?Sized> {
#[allow(clippy::needless_lifetimes)]
fn test_is_bit_valid_shared<'ptr, A: invariant::Reference>(
fn test_is_bit_valid_shared<'ptr, A: invariant::ReadFoo>(
&self,
candidate: Maybe<'ptr, T, A>,
) -> Option<bool>;
}

impl<T: TryFromBytes + Immutable + ?Sized> TestIsBitValidShared<T> for AutorefWrapper<T> {
#[allow(clippy::needless_lifetimes)]
fn test_is_bit_valid_shared<'ptr, A: invariant::Reference>(
fn test_is_bit_valid_shared<'ptr, A: invariant::ReadFoo>(
&self,
candidate: Maybe<'ptr, T, A>,
) -> Option<bool> {
Expand Down Expand Up @@ -1222,7 +1222,7 @@ mod tests {
#[allow(unused, non_local_definitions)]
impl AutorefWrapper<$ty> {
#[allow(clippy::needless_lifetimes)]
fn test_is_bit_valid_shared<'ptr, A: invariant::Reference>(
fn test_is_bit_valid_shared<'ptr, A: invariant::ReadFoo>(
&mut self,
candidate: Maybe<'ptr, $ty, A>,
) -> Option<bool> {
Expand Down
27 changes: 17 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ pub unsafe trait TryFromBytes {
/// [`UnsafeCell`]: core::cell::UnsafeCell
/// [`Shared`]: invariant::Shared
#[doc(hidden)]
fn is_bit_valid<A: invariant::Reference>(candidate: Maybe<'_, Self, A>) -> bool;
fn is_bit_valid<A: invariant::ReadFoo>(candidate: Maybe<'_, Self, A>) -> bool;

/// Attempts to interpret the given `source` as a `&Self`.
///
Expand Down Expand Up @@ -3469,10 +3469,21 @@ pub unsafe trait FromBytes: FromZeros {
where
Self: KnownLayout + Immutable,
{
static_assert_dst_is_not_zst!(Self);
match Ptr::from_ref(source).try_cast_into_no_leftover::<_, BecauseImmutable>(None) {
Ok(ptr) => Ok(ptr.bikeshed_recall_valid().as_ref()),
Err(err) => Err(err.map_src(|src| src.as_ref())),
Self::from_bytes::<invariant::Shared, BecauseImmutable>(source)
}

#[must_use = "has no side effects"]
#[inline]
fn from_bytes<'a, A: invariant::ReadFoo, R>(
source: A::Applied<'a, [u8]>,
) -> Result<A::Applied<'a, Self>, CastError<A::Applied<'a, [u8]>, Self>>
where
R: invariant::ReadReason,
Self: KnownLayout + invariant::Read<A, R>,
{
match Ptr::<'_, _, (A, _, _)>::from(source).try_cast_into_no_leftover::<_, R>(None) {
Ok(ptr) => Ok(ptr.bikeshed_recall_valid().into()),
Err(err) => Err(err.map_src(|src| src.into())),
}
}

Expand Down Expand Up @@ -3705,11 +3716,7 @@ pub unsafe trait FromBytes: FromZeros {
where
Self: IntoBytes + KnownLayout,
{
static_assert_dst_is_not_zst!(Self);
match Ptr::from_mut(source).try_cast_into_no_leftover::<_, BecauseExclusive>(None) {
Ok(ptr) => Ok(ptr.bikeshed_recall_valid().as_mut()),
Err(err) => Err(err.map_src(|src| src.as_mut())),
}
Self::from_bytes::<invariant::Exclusive, BecauseExclusive>(source)
}

/// Interprets the prefix of the given `source` as a `&mut Self` without
Expand Down
3 changes: 2 additions & 1 deletion src/pointer/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ mod _def {
/// `Ptr<'a, T>` is [covariant] in `'a` and `T`.
///
/// [covariant]: https://doc.rust-lang.org/reference/subtyping.html
pub(crate) struct PtrInner<'a, T>
#[allow(missing_debug_implementations)]
pub struct PtrInner<'a, T>
where
T: ?Sized,
{
Expand Down
Loading

0 comments on commit c098b73

Please sign in to comment.