@@ -43,19 +43,29 @@ pub struct Volatile<R, A = ReadWrite> {
43
43
access : PhantomData < A > ,
44
44
}
45
45
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.
47
51
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.
49
58
///
50
59
/// ## Example
51
60
///
52
61
/// ```rust
53
62
/// use volatile::Volatile;
54
63
///
55
- /// let value = 0u32;
64
+ /// let mut value = 0u32;
56
65
///
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);
59
69
/// ```
60
70
pub const fn new ( reference : R ) -> Volatile < R > {
61
71
Volatile {
@@ -64,13 +74,75 @@ impl<R> Volatile<R> {
64
74
}
65
75
}
66
76
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
+ /// ```
67
108
pub const fn new_read_only ( reference : R ) -> Volatile < R , ReadOnly > {
68
109
Volatile {
69
110
reference,
70
111
access : PhantomData ,
71
112
}
72
113
}
73
114
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
+ /// ```
74
146
pub const fn new_write_only ( reference : R ) -> Volatile < R , WriteOnly > {
75
147
Volatile {
76
148
reference,
0 commit comments