From f6f729f333614540ca770de9a3dfbc7bf8a279ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 14 Nov 2023 11:14:35 +0100 Subject: [PATCH] Update heapless --- CHANGELOG.md | 1 + Cargo.toml | 4 ++-- src/http.rs | 4 +++- src/http/server.rs | 11 +++++++---- src/utils/http.rs | 11 ++++++----- src/wifi.rs | 9 +++++---- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 039106a..7f2efb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.??.?] - ? * Added the opt-out `asyncify` feature. Disabling this feature removes the `atomic-waker` dependencies and removes the `utils::asyncify` module. * The `serde` dependency is now optional. +* Update to `heapless` 0.8 ## [0.26.4] - 2023-11-12 * Updated changelog diff --git a/Cargo.toml b/Cargo.toml index 49702ef..4ade3a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,11 +22,11 @@ experimental = [] use_serde = ["dep:serde", "enumset/serde", "no-std-net/serde", "heapless/serde"] use_strum = ["strum", "strum_macros"] use_numenum = ["num_enum"] -defmt = ["dep:defmt", "heapless/defmt", "heapless/defmt-impl", "embedded-io/defmt-03", "embedded-io-async?/defmt-03", "embedded-hal-async?/defmt-03"] +defmt = ["dep:defmt", "heapless/defmt-03", "embedded-io/defmt-03", "embedded-io-async?/defmt-03", "embedded-hal-async?/defmt-03"] asyncify = ["dep:atomic-waker"] [dependencies] -heapless = { version = "0.7" } +heapless = { version = "0.8" } embedded-io = { version = "0.6", default-features = false } embedded-io-async = { version = "0.6", default-features = false, optional = true } embedded-hal-async = { version = "=1.0.0-rc.1", default-features = false, optional = true } diff --git a/src/http.rs b/src/http.rs index 06f60a8..fd1837e 100644 --- a/src/http.rs +++ b/src/http.rs @@ -170,6 +170,8 @@ where } pub mod headers { + use core::convert::TryFrom; + pub type ContentLenParseBuf = heapless::String<20>; pub fn content_type(ctype: &str) -> (&str, &str) { @@ -177,7 +179,7 @@ pub mod headers { } pub fn content_len(len: u64, buf: &mut ContentLenParseBuf) -> (&str, &str) { - *buf = ContentLenParseBuf::from(len); + *buf = ContentLenParseBuf::try_from(len).unwrap(); ("Content-Length", buf.as_str()) } diff --git a/src/http/server.rs b/src/http/server.rs index bda64d0..4eb8bdf 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -1,4 +1,7 @@ -use core::fmt::{self, Debug, Display, Write as _}; +use core::{ + convert::TryInto, + fmt::{self, Debug, Display, Write as _}, +}; use crate::io::{Error, Read, Write}; @@ -222,7 +225,7 @@ pub struct HandlerError(heapless::String<64>); impl HandlerError { pub fn new(message: &str) -> Self { - Self(message.into()) + Self(message.try_into().unwrap()) } pub fn message(&self) -> &str { @@ -239,10 +242,10 @@ where E: Debug, { fn from(e: E) -> Self { - let mut string: heapless::String<64> = "".into(); + let mut string = heapless::String::<64>::new(); if write!(&mut string, "{e:?}").is_err() { - string = "(Error string too big)".into(); + string = "(Error string too big)".try_into().unwrap(); } Self(string) diff --git a/src/utils/http.rs b/src/utils/http.rs index 77cb9b7..d256f6d 100644 --- a/src/utils/http.rs +++ b/src/utils/http.rs @@ -1,4 +1,4 @@ -use core::str; +use core::{convert::TryFrom, str}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] @@ -101,7 +101,7 @@ impl<'b, const N: usize> Headers<'b, N> { content_len: u64, buf: &'b mut heapless::String<20>, ) -> &mut Self { - *buf = heapless::String::<20>::from(content_len); + *buf = heapless::String::<20>::try_from(content_len).unwrap(); self.set("Content-Length", buf.as_str()) } @@ -372,6 +372,7 @@ pub mod server { } pub mod session { + use core::convert::TryInto; use core::fmt; use core::time::Duration; @@ -444,7 +445,7 @@ pub mod server { for entry in &mut *data { if entry.last_accessed + entry.timeout < current_time { - entry.id = "".into(); + entry.id = heapless::String::new(); } } } @@ -515,7 +516,7 @@ pub mod server { { Ok(f(&mut entry.data)) } else if let Some(entry) = data.iter_mut().find(|entry| entry.id == "") { - entry.id = session_id.into(); + entry.id = session_id.try_into().unwrap(); entry.data = Default::default(); entry.timeout = self.default_session_timeout; entry.last_accessed = current_time; @@ -537,7 +538,7 @@ pub mod server { .iter_mut() .find(|entry| entry.id.as_str() == session_id) { - entry.id = "".into(); + entry.id = heapless::String::new(); true } else { false diff --git a/src/wifi.rs b/src/wifi.rs index 7e58ead..b47ab75 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -1,3 +1,4 @@ +use core::convert::TryInto; use core::fmt::Debug; use core::mem; @@ -157,13 +158,13 @@ pub struct AccessPointConfiguration { impl Default for AccessPointConfiguration { fn default() -> Self { Self { - ssid: "iot-device".into(), + ssid: "iot-device".try_into().unwrap(), ssid_hidden: false, channel: 1, secondary_channel: None, protocols: Protocol::P802D11B | Protocol::P802D11BG | Protocol::P802D11BGN, auth_method: AuthMethod::None, - password: "".into(), + password: heapless::String::new(), max_connections: 255, } } @@ -195,10 +196,10 @@ impl Debug for ClientConfiguration { impl Default for ClientConfiguration { fn default() -> Self { ClientConfiguration { - ssid: "".into(), + ssid: heapless::String::new(), bssid: None, auth_method: Default::default(), - password: "".into(), + password: heapless::String::new(), channel: None, } }