Skip to content

Commit

Permalink
Merge pull request #46 from mkroening/borrow-ref
Browse files Browse the repository at this point in the history
Add `VolatileRef::borrow` and `VolatileRef::borrow_mut`
  • Loading branch information
phil-opp authored Apr 21, 2024
2 parents 6546070 + 721df19 commit 47660af
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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 @@ impl<'a, T, A> VolatileRef<'a, T, A>
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

0 comments on commit 47660af

Please sign in to comment.