Skip to content

Commit d48e8ec

Browse files
committed
Auto merge of #6 - japaric:modify, r=japaric
`.modify()`: give access to readable bits of the register closes #3
2 parents 83175e5 + 1536f4e commit d48e8ec

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
}
@@ -209,15 +209,15 @@ Usage looks like this:
209209
i2c1.cr2.write(|w| w.sadd0(true).sadd1(0b0011110));
210210
```
211211

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

216216
Usage looks like this:
217217

218218
``` rust
219-
// Sets the STOP bit of the CR2 register
220-
i2c1.cr2.modify(|r| r.stop(true));
219+
// Toggle the STOP bit of the CR2 register and set the START bit
220+
i2c1.cr2.modify(|r, w| w.stop(!r.stop()).start(true));
221221
```
222222

223223
## 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)