Skip to content

Commit e632902

Browse files
committed
feat: generalize restrict to all access types
Signed-off-by: Martin Kröning <[email protected]>
1 parent 7d6a147 commit e632902

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

src/volatile_ptr/operations.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::{
44
};
55

66
use crate::{
7-
access::{Access, ReadOnly, ReadWrite, Readable, Writable, WriteOnly},
7+
access::{Access, ReadOnly, ReadWrite, Readable, RestrictAccess, Writable, WriteOnly},
88
VolatilePtr,
99
};
1010

@@ -211,7 +211,7 @@ where
211211
}
212212

213213
/// Methods for restricting access.
214-
impl<'a, T> VolatilePtr<'a, T, ReadWrite>
214+
impl<'a, T, A> VolatilePtr<'a, T, A>
215215
where
216216
T: ?Sized,
217217
{
@@ -220,7 +220,7 @@ where
220220
/// ## Example
221221
///
222222
/// ```
223-
/// use volatile::access::ReadOnly;
223+
/// use volatile::access::{ReadOnly, WriteOnly};
224224
/// use volatile::VolatilePtr;
225225
///
226226
/// let mut value: i16 = -4;
@@ -229,14 +229,24 @@ where
229229
/// let read_only = volatile.restrict::<ReadOnly>();
230230
/// assert_eq!(read_only.read(), -4);
231231
/// // read_only.write(10); // compile-time error
232+
///
233+
/// let no_access = read_only.restrict::<WriteOnly>();
234+
/// // no_access.read(); // compile-time error
235+
/// // no_access.write(10); // compile-time error
232236
/// ```
233-
pub fn restrict<A>(self) -> VolatilePtr<'a, T, A>
237+
pub fn restrict<To>(self) -> VolatilePtr<'a, T, A::Restricted>
234238
where
235-
A: Access,
239+
A: RestrictAccess<To>,
236240
{
237241
unsafe { VolatilePtr::new_restricted(Default::default(), self.pointer) }
238242
}
243+
}
239244

245+
/// Methods for restricting access.
246+
impl<'a, T> VolatilePtr<'a, T, ReadWrite>
247+
where
248+
T: ?Sized,
249+
{
240250
/// Restricts access permissions to read-only.
241251
///
242252
/// ## Example

src/volatile_ref.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
access::{Access, Copyable, ReadOnly, ReadWrite, WriteOnly},
2+
access::{Access, Copyable, ReadOnly, ReadWrite, RestrictAccess, WriteOnly},
33
volatile_ptr::VolatilePtr,
44
};
55
use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};
@@ -194,7 +194,7 @@ where
194194
}
195195

196196
/// Methods for restricting access.
197-
impl<'a, T> VolatileRef<'a, T, ReadWrite>
197+
impl<'a, T, A> VolatileRef<'a, T, A>
198198
where
199199
T: ?Sized,
200200
{
@@ -203,7 +203,7 @@ where
203203
/// ## Example
204204
///
205205
/// ```
206-
/// use volatile::access::ReadOnly;
206+
/// use volatile::access::{ReadOnly, WriteOnly};
207207
/// use volatile::VolatileRef;
208208
///
209209
/// let mut value: i16 = -4;
@@ -212,14 +212,24 @@ where
212212
/// let read_only = volatile.restrict::<ReadOnly>();
213213
/// assert_eq!(read_only.as_ptr().read(), -4);
214214
/// // read_only.as_ptr().write(10); // compile-time error
215+
///
216+
/// let no_access = read_only.restrict::<WriteOnly>();
217+
/// // no_access.read(); // compile-time error
218+
/// // no_access.write(10); // compile-time error
215219
/// ```
216-
pub fn restrict<A>(self) -> VolatileRef<'a, T, A>
220+
pub fn restrict<To>(self) -> VolatileRef<'a, T, A::Restricted>
217221
where
218-
A: Access,
222+
A: RestrictAccess<To>,
219223
{
220224
unsafe { VolatileRef::new_restricted(Default::default(), self.pointer) }
221225
}
226+
}
222227

228+
/// Methods for restricting access.
229+
impl<'a, T> VolatileRef<'a, T, ReadWrite>
230+
where
231+
T: ?Sized,
232+
{
223233
/// Restricts access permissions to read-only.
224234
///
225235
/// ## Example

0 commit comments

Comments
 (0)