Skip to content

Commit e1f8ad5

Browse files
committed
Add constructor methods for read/write-only types
Also: minor improvements
1 parent d4c03f1 commit e1f8ad5

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

src/lib.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ pub mod access;
3030
/// TODO: read/write permissions
3131
#[derive(Debug, Default, Clone)]
3232
#[repr(transparent)]
33-
pub struct Volatile<T, A = ReadWrite> {
34-
value: T,
33+
pub struct Volatile<R, A = ReadWrite> {
34+
reference: R,
3535
access: PhantomData<A>,
3636
}
3737

38-
impl<T> Volatile<T> {
39-
/// Construct a new volatile instance wrapping the given value reference.
38+
impl<R> Volatile<R> {
39+
/// Construct a new volatile instance wrapping the given reference.
4040
///
4141
/// ## Example
4242
///
@@ -48,15 +48,32 @@ impl<T> Volatile<T> {
4848
/// let volatile = Volatile::new(&value);
4949
/// assert_eq!(volatile.read(), 0);
5050
/// ```
51-
pub const fn new(value: T) -> Volatile<T> {
51+
pub const fn new(reference: R) -> Volatile<R> {
5252
Volatile {
53-
value,
53+
reference,
54+
access: PhantomData,
55+
}
56+
}
57+
58+
pub const fn new_read_only(reference: R) -> Volatile<R, ReadOnly> {
59+
Volatile {
60+
reference,
61+
access: PhantomData,
62+
}
63+
}
64+
65+
pub const fn new_write_only(reference: R) -> Volatile<R, WriteOnly> {
66+
Volatile {
67+
reference,
5468
access: PhantomData,
5569
}
5670
}
5771
}
5872

59-
impl<T: Copy, A> Volatile<&T, A> {
73+
impl<T, A> Volatile<&T, A>
74+
where
75+
T: Copy,
76+
{
6077
/// Performs a volatile read of the contained value.
6178
///
6279
/// Returns a copy of the read value. Volatile reads are guaranteed not to be optimized
@@ -78,7 +95,7 @@ impl<T: Copy, A> Volatile<&T, A> {
7895
A: Readable,
7996
{
8097
// UNSAFE: Safe, as we know that our internal value exists.
81-
unsafe { ptr::read_volatile(self.value) }
98+
unsafe { ptr::read_volatile(self.reference) }
8299
}
83100
}
84101

@@ -94,17 +111,17 @@ impl<T: Copy, A> Volatile<&mut T, A> {
94111
/// ```rust
95112
/// use volatile::Volatile;
96113
///
97-
/// let mut value = 42;
114+
/// let mut value = 10;
98115
/// let volatile = Volatile::new(&mut value);
99116
///
100-
/// assert_eq!(volatile.read(), 42);
117+
/// assert_eq!(volatile.read(), 10);
101118
/// ```
102119
pub fn read(&self) -> T
103120
where
104121
A: Readable,
105122
{
106123
// UNSAFE: Safe, as we know that our internal value exists.
107-
unsafe { ptr::read_volatile(self.value) }
124+
unsafe { ptr::read_volatile(self.reference) }
108125
}
109126

110127
/// Performs a volatile write, setting the contained value to the given `value`.
@@ -129,7 +146,7 @@ impl<T: Copy, A> Volatile<&mut T, A> {
129146
A: Writable,
130147
{
131148
// UNSAFE: Safe, as we know that our internal value exists.
132-
unsafe { ptr::write_volatile(self.value, value) };
149+
unsafe { ptr::write_volatile(self.reference, value) };
133150
}
134151

135152
/// Updates the contained value using the given closure and volatile instructions.
@@ -158,25 +175,25 @@ impl<T: Copy, A> Volatile<&mut T, A> {
158175
}
159176
}
160177

161-
impl<T: Copy, A> Volatile<&[T], A> {
178+
impl<T, A> Volatile<&[T], A> {
162179
pub fn index<I>(&self, index: I) -> Volatile<&I::Output, A>
163180
where
164181
I: SliceIndex<[T]>,
165182
{
166183
Volatile {
167-
value: self.value.index(index),
184+
reference: self.reference.index(index),
168185
access: self.access,
169186
}
170187
}
171188
}
172189

173-
impl<T: Copy, A> Volatile<&mut [T], A> {
190+
impl<T, A> Volatile<&mut [T], A> {
174191
pub fn index<I>(&self, index: I) -> Volatile<&I::Output, A>
175192
where
176193
I: SliceIndex<[T]>,
177194
{
178195
Volatile {
179-
value: self.value.index(index),
196+
reference: self.reference.index(index),
180197
access: self.access,
181198
}
182199
}
@@ -186,7 +203,7 @@ impl<T: Copy, A> Volatile<&mut [T], A> {
186203
I: SliceIndex<[T]>,
187204
{
188205
Volatile {
189-
value: self.value.index_mut(index),
206+
reference: self.reference.index_mut(index),
190207
access: self.access,
191208
}
192209
}

0 commit comments

Comments
 (0)