Skip to content
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

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

Merged
merged 1 commit into from
Apr 21, 2024
Merged
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
@@ -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
@@ -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.
@@ -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
@@ -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
@@ -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.
@@ -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()
}
}

@@ -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

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

GitHub Actions / Test Suite (unstable)

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

@@ -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

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

GitHub Actions / Test Suite (unstable)

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


Unchanged files with check annotations Beta

//! [in the `unsafe-code-guidelines` repository](https://github.com/rust-lang/unsafe-code-guidelines/issues/411).
#![no_std]
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]

Check warning on line 35 in src/lib.rs

GitHub Actions / Test Suite (very_unstable)

the feature `core_intrinsics` is internal to the compiler or standard library

Check warning on line 35 in src/lib.rs

GitHub Actions / Test Suite (very_unstable)

the feature `core_intrinsics` is internal to the compiler or standard library

Check warning on line 35 in src/lib.rs

GitHub Actions / Test Suite (unstable)

the feature `core_intrinsics` is internal to the compiler or standard library

Check warning on line 35 in src/lib.rs

GitHub Actions / Test Suite (unstable)

the feature `core_intrinsics` is internal to the compiler or standard library
#![cfg_attr(feature = "unstable", feature(slice_range))]
#![cfg_attr(feature = "unstable", feature(slice_ptr_get))]
#![cfg_attr(feature = "very_unstable", feature(const_trait_impl))]
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 82 in src/volatile_ptr/mod.rs

GitHub Actions / Test Suite (very_unstable)

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

Check warning on line 82 in src/volatile_ptr/mod.rs

GitHub Actions / Test Suite (unstable)

ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
}
}
T: ?Sized,
{
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())

Check warning on line 91 in src/volatile_ptr/mod.rs

GitHub Actions / Test Suite (very_unstable)

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

Check warning on line 91 in src/volatile_ptr/mod.rs

GitHub Actions / Test Suite (unstable)

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