Skip to content

Commit ed91bf5

Browse files
committed
Document constructor functions
1 parent b204c93 commit ed91bf5

File tree

1 file changed

+77
-5
lines changed

1 file changed

+77
-5
lines changed

src/lib.rs

+77-5
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,29 @@ pub struct Volatile<R, A = ReadWrite> {
4343
access: PhantomData<A>,
4444
}
4545

46-
/// Construction functions
46+
/// Constructor functions
47+
///
48+
/// These functions allow to construct a new `Volatile` instance from a reference type. While
49+
/// the `new` function creates a `Volatile` instance with unrestricted access, there are also
50+
/// functions for creating read-only or write-only instances.
4751
impl<R> Volatile<R> {
48-
/// Construct a new volatile instance wrapping the given reference.
52+
/// Constructs a new volatile instance wrapping the given reference.
53+
///
54+
/// While it is possible to construct `Volatile` instances from arbitrary values (including
55+
/// non-reference values), most of the methods are only available when the wrapped type is
56+
/// a reference. There are also special methods for some valus, for example slicing methods
57+
/// if the wrapped value is a slice.
4958
///
5059
/// ## Example
5160
///
5261
/// ```rust
5362
/// use volatile::Volatile;
5463
///
55-
/// let value = 0u32;
64+
/// let mut value = 0u32;
5665
///
57-
/// let volatile = Volatile::new(&value);
58-
/// assert_eq!(volatile.read(), 0);
66+
/// let mut volatile = Volatile::new(&mut value);
67+
/// volatile.write(1);
68+
/// assert_eq!(volatile.read(), 1);
5969
/// ```
6070
pub const fn new(reference: R) -> Volatile<R> {
6171
Volatile {
@@ -64,13 +74,75 @@ impl<R> Volatile<R> {
6474
}
6575
}
6676

77+
/// Constructs a new read-only volatile instance wrapping the given reference.
78+
///
79+
/// This is equivalent to the `new` function with the difference that the returned
80+
/// `Volatile` instance does not permit write operations. This is for example useful
81+
/// with memory-mapped hardware registers that are defined as read-only by the hardware.
82+
///
83+
/// ## Example
84+
///
85+
/// Reading is allowed:
86+
///
87+
/// ```rust
88+
/// use volatile::Volatile;
89+
///
90+
/// let value = 0u32;
91+
///
92+
/// let volatile = Volatile::new_read_only(&value);
93+
/// assert_eq!(volatile.read(), 0);
94+
/// ```
95+
///
96+
/// But writing is not:
97+
///
98+
/// ```compile_fail
99+
/// use volatile::Volatile;
100+
///
101+
/// let mut value = 0u32;
102+
///
103+
/// let mut volatile = Volatile::new_read_only(&mut value);
104+
/// volatile.write(1);
105+
/// //ERROR: ^^^^^ the trait `volatile::access::Writable` is not implemented
106+
/// // for `volatile::access::ReadOnly`
107+
/// ```
67108
pub const fn new_read_only(reference: R) -> Volatile<R, ReadOnly> {
68109
Volatile {
69110
reference,
70111
access: PhantomData,
71112
}
72113
}
73114

115+
/// Constructs a new write-only volatile instance wrapping the given reference.
116+
///
117+
/// This is equivalent to the `new` function with the difference that the returned
118+
/// `Volatile` instance does not permit read operations. This is for example useful
119+
/// with memory-mapped hardware registers that are defined as write-only by the hardware.
120+
///
121+
/// ## Example
122+
///
123+
/// Writing is allowed:
124+
///
125+
/// ```rust
126+
/// use volatile::Volatile;
127+
///
128+
/// let mut value = 0u32;
129+
///
130+
/// let mut volatile = Volatile::new_write_only(&mut value);
131+
/// volatile.write(1);
132+
/// ```
133+
///
134+
/// But reading is not:
135+
///
136+
/// ```compile_fail
137+
/// use volatile::Volatile;
138+
///
139+
/// let value = 0u32;
140+
///
141+
/// let volatile = Volatile::new_write_only(&value);
142+
/// volatile.read();
143+
/// //ERROR: ^^^^ the trait `volatile::access::Readable` is not implemented
144+
/// // for `volatile::access::WriteOnly`
145+
/// ```
74146
pub const fn new_write_only(reference: R) -> Volatile<R, WriteOnly> {
75147
Volatile {
76148
reference,

0 commit comments

Comments
 (0)