Skip to content

Commit 721df19

Browse files
committed
Add VolatileRef::borrow and VolatileRef::borrow_mut
Signed-off-by: Martin Kröning <[email protected]>
1 parent 2252916 commit 721df19

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Add `VolatileRef::borrow` and `VolatileRef::borrow_mut`
4+
35
# 0.5.2 – 2024-03-22
46

57
- Add implementations for `fmt::Pointer`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`.

src/volatile_ref.rs

+24
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,30 @@ impl<'a, T, A> VolatileRef<'a, T, A>
134134
where
135135
T: ?Sized,
136136
{
137+
/// Immutably borrows from this `VolatileRef`.
138+
///
139+
/// This method creates a `VolatileRef` tied to the lifetime of the `&VolatileRef` it is created from.
140+
/// This is useful for providing a volatile reference without moving the original `VolatileRef`.
141+
/// In comparison with creating a `&VolatileRef<'a, T>`, this avoids the additional indirection and lifetime.
142+
pub fn borrow(&self) -> VolatileRef<'_, T, A::RestrictShared>
143+
where
144+
A: Access,
145+
{
146+
unsafe { VolatileRef::new_restricted(Default::default(), self.pointer) }
147+
}
148+
149+
/// Mutably borrows from this `VolatileRef`.
150+
///
151+
/// This method creates a `VolatileRef` tied to the lifetime of the `&mut VolatileRef` it is created from.
152+
/// This is useful for providing a volatile reference without moving the original `VolatileRef`.
153+
/// In comparison with creating a `&mut VolatileRef<'a, T>`, this avoids the additional indirection and lifetime.
154+
pub fn borrow_mut(&mut self) -> VolatileRef<'_, T, A>
155+
where
156+
A: Access,
157+
{
158+
unsafe { VolatileRef::new_restricted(Default::default(), self.pointer) }
159+
}
160+
137161
/// Borrows this `VolatileRef` as a read-only [`VolatilePtr`].
138162
///
139163
/// Use this method to do (partial) volatile reads of the referenced data.

0 commit comments

Comments
 (0)