Skip to content

Commit

Permalink
Add std feature
Browse files Browse the repository at this point in the history
  • Loading branch information
diondokter committed Mar 1, 2024
1 parent e6613c9 commit 18bedb7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
## Unreleased

- *Breaking:* Corruption repair is automatic now! The repair functions have been made private.
- *Breaking:* The feature `defmt` has been renamed `defmt-03` to avoid a future breaking change.
- Added `std` feature that implements the error trait for the error enum

## 0.9.1 13-02-24

Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ futures = { version = "0.3.30", features = ["executor"] }
futures-test = "0.3.30"

[features]
defmt = ["dep:defmt"]
_test = ["dep:futures", "dep:approx"]
defmt-03 = ["dep:defmt"]
std = []
_test = ["dep:futures", "dep:approx", "std"]
34 changes: 31 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(not(any(test, doctest, feature = "_test")), no_std)]
#![cfg_attr(not(any(test, doctest, feature = "std")), no_std)]
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]

Expand Down Expand Up @@ -335,7 +335,7 @@ impl PageState {

#[non_exhaustive]
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
enum BasicError<S> {
/// An error in the storage (flash)
Storage {
Expand Down Expand Up @@ -364,7 +364,7 @@ enum BasicError<S> {
/// The main error type
#[non_exhaustive]
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum Error<I, S> {
/// An error in the storage (flash)
Storage {
Expand Down Expand Up @@ -431,6 +431,34 @@ impl<I, S> From<BasicError<S>> for Error<I, S> {
}
}

impl<I, S> core::fmt::Display for Error<I, S>
where
I: core::fmt::Display,
S: core::fmt::Display,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Storage { value, .. } => write!(f, "Storage error: {value}"),
Error::FullStorage => write!(f, "Storage is full"),
Error::Corrupted { .. } => write!(f, "Storage is corrupted"),
Error::BufferTooBig => write!(f, "A provided buffer was to big to be used"),
Error::BufferTooSmall(needed) => write!(
f,
"A provided buffer was to small to be used. Needed was {needed}"
),
Error::Item(value) => write!(f, "Item error: {value}"),
}
}
}

#[cfg(feature = "std")]
impl<I, S> std::error::Error for Error<I, S>
where
I: std::error::Error,
S: std::error::Error,
{
}

/// Round up the the given number to align with the wordsize of the flash.
/// If the number is already aligned, it is not changed.
const fn round_up_to_alignment<S: NorFlash>(value: u32) -> u32 {
Expand Down

0 comments on commit 18bedb7

Please sign in to comment.