Skip to content

Commit 306c3d8

Browse files
bors[bot]Dirbaio
andauthored
Merge #286
286: blocking/spi: Don't return the same buffer back. r=therealprof a=Dirbaio This is a rather minor improvement to the blocking SPI Transfer trait, but that IMO is still worth it. Old: `fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], Self::Error>` New: `fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error>` It is redundant (and confusing IMO) to return back the same buffer the user has passed in. It allows for 2 ways of writing the same thing: use the returned buffer, or ignore it and use the buffer that was passed in. It's not obvious which is better, and from the docs it's not obvious that both are really the same either. Also, with the old signature, it is possible for implementors to return a subslice of the buffer instead of the whole buffer. This would be arguably wrong and would break user code that assumes the entire buffer is returned. Not re-returning the buffer makes this mistake impossible. Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents 921a81e + 849801a commit 306c3d8

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2020
- `blocking::Serial`: renamed `bwrite_all` to `write`, `bflush` to `flush.
2121
- Removed `prelude` to avoid method name conflicts between different flavors (blocking, nb) of the same trait. Traits must now be manually imported.
2222
- Removed the various `Default` marker traits.
23+
- Removed `&[W]` returned slice in `spi::blocking::Transfer`.
2324

2425
### Removed
2526
- Removed random number generation (`rng`) traits in favor of [rand_core](https://crates.io/crates/rand_core).

src/blocking/spi.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ pub trait Transfer<W> {
55
/// Error type
66
type Error;
77

8-
/// Writes `words` to the slave. Returns the `words` received from the slave
9-
fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], Self::Error>;
8+
/// Writes and reads simultaneously. The contents of `words` are
9+
/// written to the slave, and the received words are stored into the same
10+
/// `words` buffer, overwriting it.
11+
fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
1012
}
1113

1214
/// Blocking write

0 commit comments

Comments
 (0)