From cfe596bd3e56ad4f86c513c7669a5e6f5bf4e39f Mon Sep 17 00:00:00 2001 From: Andres Vahter Date: Sat, 9 May 2020 19:46:06 +0300 Subject: [PATCH 1/3] readme: add instructions for tests --- README.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a0f8592e..0fb6fef6 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,20 @@ > `static` friendly data structures that don't require dynamic memory allocation -# [Documentation](https://japaric.github.io/heapless/heapless/index.html) +## [Documentation](https://japaric.github.io/heapless/heapless/index.html) -# [Change log](CHANGELOG.md) +## [Change log](CHANGELOG.md) -# License +## Tests + +```bash +# run all +cargo test --features 'serde','x86-sync-pool' +# run only for example histbuf tests +cargo test histbuf --features 'serde','x86-sync-pool' +``` + +## License Licensed under either of From f5a26c04c4bdfe932928ef7a284b052cee66ff1d Mon Sep 17 00:00:00 2001 From: Andres Vahter Date: Sat, 9 May 2020 20:05:19 +0300 Subject: [PATCH 2/3] histbuf: replace slow modulo operatins on cortex m0 `%` is extremely costly --- src/histbuf.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/histbuf.rs b/src/histbuf.rs index 31d36cd9..85f0650c 100644 --- a/src/histbuf.rs +++ b/src/histbuf.rs @@ -122,7 +122,10 @@ where /// Writes an element to the buffer, overwriting the oldest value. pub fn write(&mut self, t: T) { self.data[self.write_at] = t; - self.write_at = (self.write_at + 1) % self.len(); + self.write_at = self.write_at + 1; + if self.write_at == self.len() { + self.write_at = 0; + } } /// Clones and writes all elements in a slice to the buffer. @@ -152,7 +155,11 @@ where /// assert_eq!(x.recent(), &10); /// ``` pub fn recent(&self) -> &T { - &self.data[(self.write_at + self.len() - 1) % self.len()] + if self.write_at == 0 { + &self.data[(self.len() - 1)] + } else { + &self.data[(self.write_at - 1)] + } } /// Returns the array slice backing the buffer, without keeping track From c6e4f5964029f0f83a372ffafaecfa4a67cd539b Mon Sep 17 00:00:00 2001 From: Andres Vahter Date: Sat, 9 May 2020 21:32:55 +0300 Subject: [PATCH 3/3] histbuf: fix style --- src/histbuf.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/histbuf.rs b/src/histbuf.rs index 85f0650c..3239f836 100644 --- a/src/histbuf.rs +++ b/src/histbuf.rs @@ -122,7 +122,7 @@ where /// Writes an element to the buffer, overwriting the oldest value. pub fn write(&mut self, t: T) { self.data[self.write_at] = t; - self.write_at = self.write_at + 1; + self.write_at += 1; if self.write_at == self.len() { self.write_at = 0; } @@ -156,9 +156,9 @@ where /// ``` pub fn recent(&self) -> &T { if self.write_at == 0 { - &self.data[(self.len() - 1)] + &self.data[self.len() - 1] } else { - &self.data[(self.write_at - 1)] + &self.data[self.write_at - 1] } }