Skip to content

Commit 1536f4e

Browse files
author
Jorge Aparicio
committed
.modify(): give access to readable bits of the register
closes #3
1 parent 65da1fd commit 1536f4e

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ This is signature of each of these methods:
134134
``` rust
135135
impl Cr2 {
136136
pub fn modify<F>(&mut self, f: F)
137-
where F: FnOnce(&mut Cr2W) -> &mut Cr2W
137+
where for<'w> F: FnOnce(&Cr2R, &'w mut Cr2W) -> &'w mut Cr2W
138138
{
139139
..
140140
}
@@ -198,15 +198,15 @@ Usage looks like this:
198198
i2c1.cr2.write(*Cr2W::reset_value().sadd0(true).sadd1(0b0011110));
199199
```
200200

201-
Finally, the `modify` method performs a read-modify-write operation that involves at least a `LDR`
202-
instruction, a `STR` instruction plus extra instructions to modify the fetched value of the `CR2`
201+
Finally, the `modify` method performs a read-modify-write operation that involves at least one `LDR`
202+
instruction, one `STR` instruction plus extra instructions to modify the fetched value of the `CR2`
203203
register. This method accepts a closure that specifies how the `CR2` register will be modified.
204204

205205
Usage looks like this:
206206

207207
``` rust
208-
// Sets the STOP bit of the CR2 register
209-
i2c1.cr2.modify(|r| r.stop(true));
208+
// Toggle the STOP bit of the CR2 register and set the START bit
209+
i2c1.cr2.modify(|r, w| w.stop(!r.stop()).start(true));
210210
```
211211

212212
## License

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,13 @@ pub fn gen_register(cx: &ExtCtxt, r: &Register, d: &Defaults) -> Vec<P<Item>> {
156156
items.push(quote_item!(cx,
157157
impl $name {
158158
pub fn modify<F>(&mut self, f: F)
159-
where F: FnOnce(&mut $name_w) -> &mut $name_w,
159+
where for<'w> F: FnOnce(&$name_r, &'w mut $name_w) -> &'w mut $name_w,
160160
{
161-
let mut rw = $name_w { bits: self.register.read() };
162-
f(&mut rw);
163-
self.register.write(rw.bits);
161+
let bits = self.register.read();
162+
let r = $name_r { bits: bits };
163+
let mut w = $name_w { bits: bits };
164+
f(&r, &mut w);
165+
self.register.write(w.bits);
164166
}
165167

166168
pub fn read(&self) -> $name_r {

0 commit comments

Comments
 (0)