From 295e115542b5f3bf931c943d32d7a9c444e18b3c Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Wed, 30 Jun 2021 16:57:01 +0200 Subject: [PATCH] Preparing v0.7.2 --- CHANGELOG.md | 10 +++++++++- Cargo.toml | 2 +- src/histbuf.rs | 3 +++ src/lib.rs | 1 + src/mpmc.rs | 3 +++ src/sealed.rs | 37 +++++++++++++++++++++++++++++++++++++ src/spsc.rs | 3 +++ src/vec.rs | 3 +++ 8 files changed, 60 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29b5663f6a..e15aeb8454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +## [v0.7.2] - 2021-06-30 + +### Added + +- Added new `Vec::into_array` method +- Added const-asserts to all data structures + ## [v0.7.1] - 2021-05-23 ### Changed @@ -358,7 +365,8 @@ architecture. - Initial release -[Unreleased]: https://github.com/japaric/heapless/compare/v0.7.1...HEAD +[Unreleased]: https://github.com/japaric/heapless/compare/v0.7.2...HEAD +[v0.7.2]: https://github.com/japaric/heapless/compare/v0.7.1...v0.7.2 [v0.7.1]: https://github.com/japaric/heapless/compare/v0.7.0...v0.7.1 [v0.7.0]: https://github.com/japaric/heapless/compare/v0.6.1...v0.7.0 [v0.6.1]: https://github.com/japaric/heapless/compare/v0.6.0...v0.6.1 diff --git a/Cargo.toml b/Cargo.toml index c3eda46e95..9f470c88e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["static", "no-heap"] license = "MIT OR Apache-2.0" name = "heapless" repository = "https://github.com/japaric/heapless" -version = "0.7.1" +version = "0.7.2" [features] default = ["cas"] diff --git a/src/histbuf.rs b/src/histbuf.rs index 95c0f41816..3a70d8fa0c 100644 --- a/src/histbuf.rs +++ b/src/histbuf.rs @@ -58,6 +58,9 @@ impl HistoryBuffer { /// ``` #[inline] pub const fn new() -> Self { + // Const assert + crate::sealed::greater_than_0::(); + Self { data: [Self::INIT; N], write_at: 0, diff --git a/src/lib.rs b/src/lib.rs index c52e854287..005be00810 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,7 @@ #![deny(rust_2018_compatibility)] #![deny(rust_2018_idioms)] #![deny(warnings)] +#![deny(const_err)] pub use binary_heap::BinaryHeap; pub use histbuf::HistoryBuffer; diff --git a/src/mpmc.rs b/src/mpmc.rs index 7b1acdcd1b..45519a9192 100644 --- a/src/mpmc.rs +++ b/src/mpmc.rs @@ -137,6 +137,9 @@ impl MpMcQueue { /// Creates an empty queue pub const fn new() -> Self { + // Const assert + crate::sealed::greater_than_0::(); + // Const assert on size. Self::ASSERT[!(N < (IntSize::MAX as usize)) as usize]; diff --git a/src/sealed.rs b/src/sealed.rs index 5dfc2f9a46..b963bb8bb6 100644 --- a/src/sealed.rs +++ b/src/sealed.rs @@ -21,3 +21,40 @@ pub mod binary_heap { } } } + +#[allow(dead_code)] +#[allow(path_statements)] +pub(crate) const fn greater_than_0() { + Assert::::GREATER; +} + +#[allow(dead_code)] +#[allow(path_statements)] +pub(crate) const fn greater_than_1() { + Assert::::GREATER; +} + +#[allow(dead_code)] +/// Const assert hack +pub struct Assert; + +#[allow(dead_code)] +impl Assert { + /// Const assert hack + pub const GREATER_EQ: usize = L - R; + + /// Const assert hack + pub const LESS_EQ: usize = R - L; + + /// Const assert hack + pub const NOT_EQ: isize = 0 / (R as isize - L as isize); + + /// Const assert hack + pub const EQ: usize = (R - L) + (L - R); + + /// Const assert hack + pub const GREATER: usize = L - R - 1; + + /// Const assert hack + pub const LESS: usize = R - L - 1; +} diff --git a/src/spsc.rs b/src/spsc.rs index df46ef69c7..b473bf7cb1 100644 --- a/src/spsc.rs +++ b/src/spsc.rs @@ -116,6 +116,9 @@ impl Queue { /// Creates an empty queue with a fixed capacity of `N - 1` pub const fn new() -> Self { + // Const assert N > 1 + crate::sealed::greater_than_1::(); + Queue { head: AtomicUsize::new(0), tail: AtomicUsize::new(0), diff --git a/src/vec.rs b/src/vec.rs index 1a6d2f83b4..3dee4b46a7 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -53,6 +53,9 @@ impl Vec { /// ``` /// `Vec` `const` constructor; wrap the returned value in [`Vec`](../struct.Vec.html) pub const fn new() -> Self { + // Const assert N > 0 + crate::sealed::greater_than_0::(); + Self { buffer: [Self::INIT; N], len: 0,