Skip to content

Commit

Permalink
rename use_.. features
Browse files Browse the repository at this point in the history
this resolves the [C-FEATURE] finding from the API guidelines which
states:
> Do not include words in the name of a Cargo feature that convey zero
> meaning, as in `use-abc` or `with-abc`. Name the feature `abc`
> directly.

note that this is a breaking change for existing consumers which must
rename their usage of the feature.

[C-FEATURE]: https://rust-lang.github.io/api-guidelines/naming.html#c-feature
  • Loading branch information
rursprung committed Sep 30, 2024
1 parent abe2f20 commit 4dab854
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
fail-fast: false
matrix:
rust: [1.81.0, stable]
features: ['use_alloc', 'use_alloc,defmt', 'use_heapless', 'use_heapless,defmt']
features: ['alloc', 'alloc,defmt', 'heapless', 'heapless,defmt']
exclude:
- rust: 1.81.0
features: 'use_alloc,defmt'
features: 'alloc,defmt'
- rust: 1.81.0
features: 'use_heapless,defmt'
features: 'heapless,defmt'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] - ReleaseDate
### Changed
* The MSRV has been updated to 1.81.0 due to `core::error::Error` being implemented
* **BREAKING**: the features `use_alloc` and `use_heapless` have been renamed to `alloc` and `heapless` respectively.

## [0.2.0] - 2023-11-14
### Added
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ serde = { version = "1.0", features = ["derive"], optional = true }
[features]
default = ["accelerometer_event", "button_event", "color_event", "gyro_event", "location_event", "magnetometer_event", "quaternion_event"]

use_heapless = ["dep:heapless"]
use_alloc = []
heapless = ["dep:heapless"]
alloc = []

defmt = ["dep:defmt", "heapless?/defmt-03"]

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Note that this work is not affiliated with Adafruit.

## Mandatory Features
This crate is `no_std` and you can choose whether you want to use
[`heapless::Vec`](https://docs.rs/heapless/0.8.0/heapless/struct.Vec.html) by selecting the feature `use_heapless` or
[`alloc::vec::Vec`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html) by selecting the feature `use_alloc`.
[`heapless::Vec`](https://docs.rs/heapless/0.8.0/heapless/struct.Vec.html) by selecting the feature `heapless` or
[`alloc::vec::Vec`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html) by selecting the feature `alloc`.
If you select neither or both you'll get a compile error.

## Optional Features
Expand Down
2 changes: 1 addition & 1 deletion examples/stm32f4-event-printer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmt = "0.3.6"
defmt-rtt = "0.4"

# use `adafruit-bluefruit-protocol = "0.1"` in reality; path used here to ensure that the example always compiles against the latest master
adafruit-bluefruit-protocol = { path = "../..", features = ["defmt", "use_heapless"] }
adafruit-bluefruit-protocol = { path = "../..", features = ["defmt", "heapless"] }

[profile.release]
codegen-units = 1
Expand Down
34 changes: 17 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
)))]
compile_error!("at least one event type must be selected in the features!");

#[cfg(not(any(feature = "use_alloc", feature = "use_heapless")))]
compile_error!("you must choose either 'use_alloc' or 'use_heapless' as a feature!");
#[cfg(not(any(feature = "alloc", feature = "heapless")))]
compile_error!("you must choose either 'alloc' or 'heapless' as a feature!");

#[cfg(all(feature = "use_alloc", feature = "use_heapless"))]
compile_error!("you must choose either 'use_alloc' or 'use_heapless' as a feature but not both!");
#[cfg(all(feature = "alloc", feature = "heapless"))]
compile_error!("you must choose either 'alloc' or 'heapless' as a feature but not both!");

#[cfg(feature = "accelerometer_event")]
pub mod accelerometer_event;
Expand All @@ -59,12 +59,12 @@ use color_event::ColorEvent;
use core::cmp::min;
#[cfg(feature = "gyro_event")]
use gyro_event::GyroEvent;
#[cfg(feature = "use_heapless")]
#[cfg(feature = "heapless")]
use heapless::Vec;

#[cfg(feature = "use_alloc")]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "use_alloc")]
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::error::Error;
use core::fmt::{Display, Formatter};
Expand Down Expand Up @@ -207,17 +207,17 @@ impl TryFrom<u8> for ControllerDataPackageType {
}
}

#[cfg(feature = "use_heapless")]
#[cfg(feature = "heapless")]
type ParseResult<const MAX_RESULTS: usize> =
Vec<Result<ControllerEvent, ProtocolParseError>, MAX_RESULTS>;

#[cfg(feature = "use_alloc")]
#[cfg(feature = "alloc")]
type ParseResult<const MAX_RESULTS: usize> = Vec<Result<ControllerEvent, ProtocolParseError>>;
#[cfg(feature = "use_alloc")]
#[cfg(feature = "alloc")]
const MAX_RESULTS: usize = 0;

/// Parse the input string for commands. Unexpected content will be ignored if it's not formatted like a command!
pub fn parse<#[cfg(feature = "use_heapless")] const MAX_RESULTS: usize>(
pub fn parse<#[cfg(feature = "heapless")] const MAX_RESULTS: usize>(
input: &[u8],
) -> ParseResult<MAX_RESULTS> {
/// Simple state machine for the parser, represents whether the parser is seeking a start or has found it.
Expand All @@ -239,11 +239,11 @@ pub fn parse<#[cfg(feature = "use_heapless")] const MAX_RESULTS: usize>(
}
ParserState::ParseCommand => {
let data_package = extract_and_parse_command(&input[(pos - 1)..]);
#[cfg(feature = "use_alloc")]
#[cfg(feature = "alloc")]
result.push(data_package);
#[cfg(feature = "use_heapless")]
#[cfg(feature = "heapless")]
result.push(data_package).ok();
#[cfg(feature = "use_heapless")]
#[cfg(feature = "heapless")]
if result.len() == MAX_RESULTS {
return result;
}
Expand Down Expand Up @@ -399,9 +399,9 @@ mod tests {
#[test]
fn test_parse() {
let input = b"\x00!B11:!B10;\x00\x00!\x00\x00\x00\x00!B138";
#[cfg(feature = "use_heapless")]
#[cfg(feature = "heapless")]
let result = parse::<4>(input);
#[cfg(feature = "use_alloc")]
#[cfg(feature = "alloc")]
let result = parse(input);

assert_eq!(result.len(), 4);
Expand All @@ -413,7 +413,7 @@ mod tests {
e,
&ProtocolParseError::ButtonParseError(ButtonParseError::UnknownButtonState(b'3'))
);
#[cfg(feature = "use_alloc")]
#[cfg(feature = "alloc")]
{
use alloc::string::ToString;
use core::error::Error;
Expand Down

0 comments on commit 4dab854

Please sign in to comment.