Skip to content

Commit

Permalink
Merge pull request #47 from mkroening/restrict
Browse files Browse the repository at this point in the history
Add `VolatileRef::restrict` and `VolatilePtr::restrict`
  • Loading branch information
phil-opp committed Apr 21, 2024
2 parents 2252916 + d0c3e9e commit 6546070
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
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 @@ impl<'a, T> VolatileRef<'a, T, ReadWrite>
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 @@ where
/// // 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 @@ where
/// // 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

0 comments on commit 6546070

Please sign in to comment.