Skip to content

Add VolatileRef::restrict and VolatilePtr::restrict #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2024
Merged
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
26 changes: 24 additions & 2 deletions src/volatile_ptr/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,28 @@ impl<'a, T> VolatilePtr<'a, T, ReadWrite>
where
T: ?Sized,
{
/// Restricts access permissions to `A`.
///
/// ## Example
///
/// ```
/// use volatile::access::ReadOnly;
/// use volatile::VolatilePtr;
///
/// let mut value: i16 = -4;
/// let mut volatile = unsafe { VolatilePtr::new((&mut value).into()) };
///
/// let read_only = volatile.restrict::<ReadOnly>();
/// assert_eq!(read_only.read(), -4);
/// // read_only.write(10); // compile-time error
/// ```
pub fn restrict<A>(self) -> VolatilePtr<'a, T, A>
where
A: Access,
{
unsafe { VolatilePtr::new_restricted(Default::default(), self.pointer) }
}

/// Restricts access permissions to read-only.
///
/// ## Example
Expand All @@ -235,7 +257,7 @@ where
/// // read_only.write(10); // compile-time error
/// ```
pub fn read_only(self) -> VolatilePtr<'a, T, ReadOnly> {
unsafe { VolatilePtr::new_restricted(ReadOnly, self.pointer) }
self.restrict()
}

/// Restricts access permissions to write-only.
Expand All @@ -258,6 +280,6 @@ where
/// // field_2.read(); // compile-time error
/// ```
pub fn write_only(self) -> VolatilePtr<'a, T, WriteOnly> {
unsafe { VolatilePtr::new_restricted(WriteOnly, self.pointer) }
self.restrict()
}
}
26 changes: 24 additions & 2 deletions src/volatile_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@
where
T: ?Sized,
{
/// Restricts access permissions to `A`.
///
/// ## Example
///
/// ```
/// use volatile::access::ReadOnly;
/// use volatile::VolatileRef;
///
/// let mut value: i16 = -4;
/// let mut volatile = VolatileRef::from_mut_ref(&mut value);
///
/// let read_only = volatile.restrict::<ReadOnly>();
/// assert_eq!(read_only.as_ptr().read(), -4);
/// // read_only.as_ptr().write(10); // compile-time error
/// ```
pub fn restrict<A>(self) -> VolatileRef<'a, T, A>
where
A: Access,
{
unsafe { VolatileRef::new_restricted(Default::default(), self.pointer) }
}

/// Restricts access permissions to read-only.
///
/// ## Example
Expand All @@ -190,7 +212,7 @@
/// // read_only.as_ptr().write(10); // compile-time error
/// ```
pub fn read_only(self) -> VolatileRef<'a, T, ReadOnly> {
unsafe { VolatileRef::new_restricted(ReadOnly, self.pointer) }
self.restrict()
}

/// Restricts access permissions to write-only.
Expand All @@ -212,7 +234,7 @@
/// // write_only.as_ptr().read(); // compile-time error
/// ```
pub fn write_only(self) -> VolatileRef<'a, T, WriteOnly> {
unsafe { VolatileRef::new_restricted(WriteOnly, self.pointer) }
self.restrict()
}
}

Expand Down Expand Up @@ -270,7 +292,7 @@
T: ?Sized,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr()))

Check warning on line 295 in src/volatile_ref.rs

View workflow job for this annotation

GitHub Actions / Test Suite (very_unstable)

ambiguous wide pointer comparison, the comparison includes metadata which may not be expected

Check warning on line 295 in src/volatile_ref.rs

View workflow job for this annotation

GitHub Actions / Test Suite (unstable)

ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
}
}

Expand All @@ -279,7 +301,7 @@
T: ?Sized,
{
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())

Check warning on line 304 in src/volatile_ref.rs

View workflow job for this annotation

GitHub Actions / Test Suite (very_unstable)

ambiguous wide pointer comparison, the comparison includes metadata which may not be expected

Check warning on line 304 in src/volatile_ref.rs

View workflow job for this annotation

GitHub Actions / Test Suite (unstable)

ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
}
}

Expand Down
Loading