Skip to content
Open
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 21 additions & 37 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,28 +916,29 @@ mod _casts {

/// Attempts to transform the pointer, restoring the original on
/// failure.
///
/// # Safety
///
/// If `I::Aliasing != Shared`, then if `f` returns `Err(err)`, no copy
/// of `f`'s argument must exist outside of `err`.
#[inline(always)]
pub(crate) unsafe fn try_with_unchecked<U, J, E, F>(
self,
f: F,
) -> Result<Ptr<'a, U, J>, E::Mapped>
pub(crate) fn try_with<U, J, E, F>(self, f: F) -> Result<Ptr<'a, U, J>, E::Mapped>
where
U: 'a + ?Sized,
J: Invariants<Aliasing = I::Aliasing>,
E: TryWithError<Self>,
F: FnOnce(Ptr<'a, T, I>) -> Result<Ptr<'a, U, J>, E>,
// TODO: The invariance issue is caused by `E` not being bound by
// `'b`, which thus requires it to be `'static`. If we can somehow
// polyfill a GAT so that it's actually `E<'b>` here, then we should
// be able to solve the `'static` issue. That will, in turn, remove
// the need for us to add `.map_err(|err| err.map_src(drop))` to
// many `f` closures.
F: for<'b> FnOnce(Ptr<'b, T, I>) -> Result<Ptr<'b, U, J>, E>,
{
let old_inner = self.as_inner();
#[rustfmt::skip]
let res = f(self).map_err(#[inline(always)] move |err: E| {
err.map(#[inline(always)] |src| {
drop(src);

// TODO: Mention the `for<'b>` variance on `F` to prove that
// `f` cannot make another copy of `self`.

// SAFETY:
// 0. Aliasing is either `Shared` or `Exclusive`:
// - If aliasing is `Shared`, then it cannot violate
Expand Down Expand Up @@ -968,21 +969,6 @@ mod _casts {
});
res
}

/// Attempts to transform the pointer, restoring the original on
/// failure.
pub(crate) fn try_with<U, J, E, F>(self, f: F) -> Result<Ptr<'a, U, J>, E::Mapped>
where
U: 'a + ?Sized,
J: Invariants<Aliasing = I::Aliasing>,
E: TryWithError<Self>,
F: FnOnce(Ptr<'a, T, I>) -> Result<Ptr<'a, U, J>, E>,
I: Invariants<Aliasing = Shared>,
{
// SAFETY: `I::Aliasing = Shared`, so the safety condition does not
// apply.
unsafe { self.try_with_unchecked(f) }
}
}

/// # Safety
Expand Down Expand Up @@ -1162,19 +1148,17 @@ mod _casts {
U: 'a + ?Sized + KnownLayout + Read<I::Aliasing, R>,
[u8]: Read<I::Aliasing, R>,
{
// SAFETY: The provided closure returns the only copy of `slf`.
unsafe {
self.try_with_unchecked(|slf| match slf.try_cast_into(CastType::Prefix, meta) {
Ok((slf, remainder)) => {
if remainder.len() == 0 {
Ok(slf)
} else {
Err(CastError::Size(SizeError::<_, U>::new(())))
}
#[rustfmt::skip]
return self.try_with(#[inline(always)] |slf| match slf.try_cast_into(CastType::Prefix, meta) {
Ok((slf, remainder)) => {
if remainder.len() == 0 {
Ok(slf)
} else {
Err(CastError::Size(SizeError::<_, U>::new(())))
}
Err(err) => Err(err.map_src(|_slf| ())),
})
}
}
Err(err) => Err(err.map_src(|_slf| ())),
});
}
}

Expand Down
14 changes: 6 additions & 8 deletions src/util/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ where
let res = ptr.try_with(#[inline(always)] |ptr| {
let ptr = ptr.recall_validity::<Initialized, _>();
let ptr = ptr.cast::<_, crate::layout::CastFrom<Dst>, _>();
ptr.try_into_valid()
ptr.try_into_valid().map_err(|err| err.map_src(drop))
});
match res {
Ok(ptr) => {
Expand Down Expand Up @@ -643,13 +643,11 @@ where
let ptr = Ptr::from_mut(self.0);
// SAFETY: The provided closure returns the only copy of `ptr`.
#[rustfmt::skip]
let res = unsafe {
ptr.try_with_unchecked(#[inline(always)] |ptr| {
let ptr = ptr.recall_validity::<Initialized, (_, (_, _))>();
let ptr = ptr.cast::<_, crate::layout::CastFrom<Dst>, _>();
ptr.try_into_valid()
})
};
let res = ptr.try_with(#[inline(always)] |ptr| {
let ptr = ptr.recall_validity::<Initialized, (_, (_, _))>();
let ptr = ptr.cast::<_, crate::layout::CastFrom<Dst>, _>();
ptr.try_into_valid().map_err(|err| err.map_src(drop))
});
match res {
Ok(ptr) => {
static_assert!(Src: ?Sized + KnownLayout, Dst: ?Sized + KnownLayout => {
Expand Down
Loading