Skip to content

Commit af23c45

Browse files
committed
Invariant-parameterize Ptr and make is_bit_valid safe
Closes #715.
1 parent 5948354 commit af23c45

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/pointer/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ where
2626
T: 'a + ?Sized + TryFromBytes,
2727
Alignment: invariant::Alignment,
2828
{
29-
/// Checks that `Ptr`'s referent is validly initialized for `T`.
29+
/// Check that `Ptr`'s referent is validly initialized for `T`.
3030
///
3131
/// # Panics
3232
///
3333
/// This method will panic if
3434
/// [`T::is_bit_valid`][TryFromBytes::is_bit_valid] panics.
3535
#[inline]
3636
pub(crate) fn check_valid(self) -> Option<MaybeAligned<'a, T, Alignment>> {
37+
let candidate = self;
3738
// This call may panic. If that happens, it doesn't cause any soundness
3839
// issues, as we have not generated any invalid state which we need to
3940
// fix before returning.
40-
if T::is_bit_valid(self.forget_aligned()) {
41-
// SAFETY: If `T::is_bit_valid`, code may assume that `self`
41+
if T::is_bit_valid(candidate.forget_aligned()) {
42+
// SAFETY: If `Self::is_bit_valid`, code may assume that `candidate`
4243
// contains a bit-valid instance of `Self`.
43-
Some(unsafe { self.assume_valid() })
44+
let candidate = unsafe { candidate.assume_valid() };
45+
Some(candidate)
4446
} else {
4547
None
4648
}
@@ -60,6 +62,8 @@ where
6062
Alignment: invariant::Alignment,
6163
{
6264
/// Reads the value from `MaybeAligned`.
65+
///
66+
/// This is only available if `T` is [`Copy`].
6367
#[inline]
6468
pub fn read_unaligned(self) -> T
6569
where

0 commit comments

Comments
 (0)