-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invariant-parameterize
Ptr
and make is_bit_valid
safe
Closes #715.
- Loading branch information
Showing
7 changed files
with
1,223 additions
and
884 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! Abstractions over raw pointers. | ||
|
||
mod ptr; | ||
|
||
pub use maybe_aligned::MaybeAligned; | ||
pub use ptr::{invariant, Ptr}; | ||
|
||
/// A shorthand for a maybe-valid, maybe-aligned reference. Used as the argument | ||
/// to [`TryFromBytes::is_bit_valid`][crate::TryFromBytes::is_bit_valid]. | ||
pub type Maybe<'a, T> = | ||
Ptr<'a, T, (invariant::Shared, invariant::AnyAlignment, invariant::AsInitialized)>; | ||
|
||
/// A semi-user-facing wrapper type representing a maybe-aligned reference, for | ||
/// use in [`TryFromBytes::is_bit_valid`][crate::TryFromBytes::is_bit_valid]. | ||
mod maybe_aligned { | ||
use super::{ptr::invariant, Ptr}; | ||
use crate::Unaligned; | ||
use core::fmt::{Debug, Formatter}; | ||
|
||
/// A reference to a validly-initialized `T` of lifetime `'a` that might | ||
/// *not* be validly aligned for `T`. | ||
#[doc(hidden)] | ||
pub struct MaybeAligned<'a, T: ?Sized> { | ||
ptr: Ptr<'a, T, (invariant::Shared, invariant::AnyAlignment, invariant::Valid)>, | ||
} | ||
|
||
impl<'a, T> MaybeAligned<'a, T> | ||
where | ||
T: 'a + ?Sized, | ||
{ | ||
/// Constructs a `MaybeAligned` from a [`Ptr`]. | ||
/// | ||
/// The `Ptr`'s referent must be validly-initialized for `T`. | ||
#[doc(hidden)] | ||
#[inline] | ||
pub const fn from_ptr( | ||
ptr: Ptr<'a, T, (invariant::Shared, invariant::AnyAlignment, invariant::Valid)>, | ||
) -> Self { | ||
Self { ptr } | ||
} | ||
|
||
/// Reads the value from `MaybeAligned`. | ||
/// | ||
/// This is only available if `T` is [`Copy`]. | ||
#[inline] | ||
pub fn read_unaligned(self) -> T | ||
where | ||
T: Copy, | ||
{ | ||
let raw = self.ptr.as_non_null().as_ptr(); | ||
// SAFETY: By invariant on `MaybeAligned`, `raw` contains | ||
// validly-initialized data for `T`. The value is safe to read and | ||
// return, because `T` is copy. | ||
unsafe { core::ptr::read_unaligned(raw) } | ||
} | ||
|
||
/// Views the value as an aligned reference. | ||
/// | ||
/// This is only available if `T` is [`Unaligned`]. | ||
#[inline] | ||
pub fn as_ref(self) -> &'a T | ||
where | ||
T: Unaligned, | ||
{ | ||
// SAFETY: The alignment of `T` is 1 and thus is always aligned | ||
// because `T: Unaligned`. | ||
let ptr = unsafe { self.ptr.assume_aligned() }; | ||
ptr.as_ref() | ||
} | ||
} | ||
|
||
impl<'a, T: 'a + ?Sized> Debug for MaybeAligned<'a, T> { | ||
#[inline] | ||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
self.ptr.fmt(f) | ||
} | ||
} | ||
} |
Oops, something went wrong.