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