Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no_alloc compatibility #3

Merged
merged 6 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/*
target/*
Cargo.lock
Cargo.lock
.DS_Store
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ embedded-hal = { version = "0.2.7", features = ["unproven"] }
bitmaps = { version = "3.1.0", default-features = false }
cortex-m = { version = "0.7.4", optional = true }
spin = { version = "0.9.8", optional = true }
heapless = "0.8.0"

[dev-dependencies]
mockall = "0.11.0"
Expand Down
43 changes: 19 additions & 24 deletions src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@ use crate::guard::LockFreeGuard;
#[cfg(feature = "spin")]
use crate::guard::SpinGuard;
use crate::pins::Pins;
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use bitmaps::Bitmap;
use core::cell::RefCell;
use core::fmt::{Debug, Formatter};
#[cfg(feature = "cortex-m")]
use cortex_m::interrupt::Mutex as CsMutex;
use embedded_hal::blocking::i2c::{Read, SevenBitAddress, Write};
use heapless::String;
#[cfg(feature = "spin")]
use spin::Mutex as SpinMutex;

Expand Down Expand Up @@ -335,14 +334,12 @@ where
/// Writes the configuration register of the given bank
fn write_conf(&mut self, bank: Bank) -> Result<(), <B as Write>::Error> {
match bank {
Bank::Bank0 => self.bus.write(
self.address,
&[COMMAND_CONF_0, self.configuration_0.as_value().to_owned()],
),
Bank::Bank1 => self.bus.write(
self.address,
&[COMMAND_CONF_1, self.configuration_1.as_value().to_owned()],
),
Bank::Bank0 => self
.bus
.write(self.address, &[COMMAND_CONF_0, self.configuration_0.as_value().clone()]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As u8 implements the Copy trait, no cloning is required, as also suggested by Clippy.
Please try to dereference/copy instead:

*self.configuration_0.as_value()

This also applies to lines 342, 351, 354, 363 and 366.

Bank::Bank1 => self
.bus
.write(self.address, &[COMMAND_CONF_1, self.configuration_1.as_value().clone()]),
}
}

Expand All @@ -351,24 +348,22 @@ where
match bank {
Bank::Bank0 => self
.bus
.write(self.address, &[COMMAND_OUTPUT_0, self.output_0.as_value().to_owned()]),
.write(self.address, &[COMMAND_OUTPUT_0, self.output_0.as_value().clone()]),
Bank::Bank1 => self
.bus
.write(self.address, &[COMMAND_OUTPUT_1, self.output_1.as_value().to_owned()]),
.write(self.address, &[COMMAND_OUTPUT_1, self.output_1.as_value().clone()]),
}
}

/// Writes the polarity register of the given bank
fn write_polarity(&mut self, bank: Bank) -> Result<(), <B as Write>::Error> {
match bank {
Bank::Bank0 => self.bus.write(
self.address,
&[COMMAND_POLARITY_0, self.polarity_0.as_value().to_owned()],
),
Bank::Bank1 => self.bus.write(
self.address,
&[COMMAND_POLARITY_1, self.polarity_1.as_value().to_owned()],
),
Bank::Bank0 => self
.bus
.write(self.address, &[COMMAND_POLARITY_0, self.polarity_0.as_value().clone()]),
Bank::Bank1 => self
.bus
.write(self.address, &[COMMAND_POLARITY_1, self.polarity_1.as_value().clone()]),
}
}
}
Expand All @@ -391,11 +386,11 @@ impl<B: Read<u8> + Write> Debug for RefreshInputError<B> {
}
}

impl<B: Read<u8> + Write> ToString for RefreshInputError<B> {
fn to_string(&self) -> String {
impl<B: Read<u8> + Write> RefreshInputError<B> {
pub fn to_string(&self) -> String<10> {
match self {
RefreshInputError::WriteError(_) => "WriteError".to_string(),
RefreshInputError::ReadError(_) => "ReadError".to_string(),
RefreshInputError::WriteError(_) => String::try_from("WriteError").unwrap(),
RefreshInputError::ReadError(_) => String::try_from("ReadError").unwrap(),
}
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "strict", deny(warnings))]

extern crate alloc;
extern crate embedded_hal;

#[cfg(feature = "example")]
Expand Down
3 changes: 1 addition & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::guard::SpinGuard;
use crate::mocks::{BusMockBuilder, MockI2CBus, WriteError};
use crate::pin_refreshable::{RefreshableInputPin, RefreshableOutputPin};
use crate::pins::Pins;
use alloc::string::ToString;
use embedded_hal::digital::v2::{InputPin, IoPin, OutputPin, PinState, StatefulOutputPin, ToggleableOutputPin};

#[test]
Expand Down Expand Up @@ -247,7 +246,7 @@ fn test_refresh_input_state_write_error() {
let mut expander = PCA9539::new(i2c_bus, 0x74);
let result = expander.refresh_input_state(Bank0);

assert_eq!("WriteError", result.unwrap_err().to_string());
assert_eq!("WriteError", result.unwrap_err().to_string().as_str());
}

#[test]
Expand Down