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::borrow and VolatileRef::borrow_mut #46

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
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Add `VolatileRef::borrow` and `VolatileRef::borrow_mut`

# 0.5.2 – 2024-03-22

- Add implementations for `fmt::Pointer`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`.
Expand Down
24 changes: 24 additions & 0 deletions src/volatile_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@
where
T: ?Sized,
{
/// Immutably borrows from this `VolatileRef`.
///
/// This method creates a `VolatileRef` tied to the lifetime of the `&VolatileRef` it is created from.
/// This is useful for providing a volatile reference without moving the original `VolatileRef`.
/// In comparison with creating a `&VolatileRef<'a, T>`, this avoids the additional indirection and lifetime.
pub fn borrow(&self) -> VolatileRef<'_, T, A::RestrictShared>
where
A: Access,
{
unsafe { VolatileRef::new_restricted(Default::default(), self.pointer) }
}

/// Mutably borrows from this `VolatileRef`.
///
/// This method creates a `VolatileRef` tied to the lifetime of the `&mut VolatileRef` it is created from.
/// This is useful for providing a volatile reference without moving the original `VolatileRef`.
/// In comparison with creating a `&mut VolatileRef<'a, T>`, this avoids the additional indirection and lifetime.
pub fn borrow_mut(&mut self) -> VolatileRef<'_, T, A>
where
A: Access,
{
unsafe { VolatileRef::new_restricted(Default::default(), self.pointer) }
}

/// Borrows this `VolatileRef` as a read-only [`VolatilePtr`].
///
/// Use this method to do (partial) volatile reads of the referenced data.
Expand Down Expand Up @@ -270,7 +294,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 297 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 297 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 +303,7 @@
T: ?Sized,
{
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(&self.pointer.as_ptr(), &other.pointer.as_ptr())

Check warning on line 306 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 306 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