From 1752956de2b1c84bb47876b9120f6eadca0e93c9 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Fri, 4 Jun 2021 13:41:31 +0200 Subject: [PATCH] move to heapless 0.7 --- advanced/firmware/Cargo.toml | 5 ++++- advanced/firmware/src/bin/usb-4-solution.rs | 3 +-- advanced/firmware/src/bin/usb-5-solution.rs | 2 +- advanced/firmware/src/bin/vec.rs | 15 ++++++--------- beginner/apps/Cargo.toml | 4 ++++ beginner/apps/src/bin/radio-puzzle-2.rs | 7 ++++--- beginner/apps/src/bin/radio-puzzle-3.rs | 4 ++-- beginner/apps/src/bin/radio-puzzle-4.rs | 10 ++++------ beginner/apps/src/bin/radio-puzzle-5.rs | 4 ++-- beginner/apps/src/bin/radio-puzzle-6.rs | 6 +++--- beginner/apps/src/bin/radio-puzzle-7.rs | 6 +++--- beginner/apps/src/bin/radio-puzzle-solution-2.rs | 4 ++-- beginner/apps/src/bin/radio-puzzle-solution.rs | 8 ++++---- 13 files changed, 40 insertions(+), 38 deletions(-) diff --git a/advanced/firmware/Cargo.toml b/advanced/firmware/Cargo.toml index e8fb0cd..cf2f9dd 100644 --- a/advanced/firmware/Cargo.toml +++ b/advanced/firmware/Cargo.toml @@ -18,11 +18,14 @@ cortex-m-rtic = "0.5.1" defmt = "0.2.1" defmt-rtt = "0.2.0" dk = { path = "../../boards/dk", features = ["advanced"] } -heapless = "0.5.5" panic-probe = { version = "0.2.0", features = ["print-defmt"] } usb = { path = "../common/usb" } usb2 = "0.0.1" +[dependencies.heapless] +version = "0.7.1" +features = ["defmt-impl"] + # optimize code in both profiles [profile.dev] codegen-units = 1 diff --git a/advanced/firmware/src/bin/usb-4-solution.rs b/advanced/firmware/src/bin/usb-4-solution.rs index 49ef01a..53c30e2 100644 --- a/advanced/firmware/src/bin/usb-4-solution.rs +++ b/advanced/firmware/src/bin/usb-4-solution.rs @@ -91,7 +91,6 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> defmt::info!("EP0: {:?}", defmt::Debug2Format(&request)); // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log // `StandardRequest` with `defmt` - match request { // section 9.4.3 // this request is valid in any state @@ -116,7 +115,7 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> Descriptor::Configuration { index } => { if index == 0 { - let mut resp = heapless::Vec::::new(); + let mut resp = heapless::Vec::::new(); let conf_desc = usb2::configuration::Descriptor { wTotalLength: (usb2::configuration::Descriptor::SIZE diff --git a/advanced/firmware/src/bin/usb-5-solution.rs b/advanced/firmware/src/bin/usb-5-solution.rs index 853bda1..bf12ddf 100644 --- a/advanced/firmware/src/bin/usb-5-solution.rs +++ b/advanced/firmware/src/bin/usb-5-solution.rs @@ -116,7 +116,7 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> Descriptor::Configuration { index } => { if index == 0 { - let mut resp = heapless::Vec::::new(); + let mut resp = heapless::Vec::::new(); let conf_desc = usb2::configuration::Descriptor { wTotalLength: (usb2::configuration::Descriptor::SIZE diff --git a/advanced/firmware/src/bin/vec.rs b/advanced/firmware/src/bin/vec.rs index f8b42b2..f009ee6 100644 --- a/advanced/firmware/src/bin/vec.rs +++ b/advanced/firmware/src/bin/vec.rs @@ -3,7 +3,7 @@ #![no_std] use cortex_m_rt::entry; -use heapless::{consts, Vec}; +use heapless::Vec; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use firmware as _; @@ -12,21 +12,18 @@ fn main() -> ! { dk::init().unwrap(); // a stack-allocated `Vec` with capacity for 6 bytes - let mut buffer = Vec::::new(); - // ^^ capacity; this is a type not a value + let mut buffer = Vec::::new(); + // content type ^^ ^ capacity // `heapless::Vec` exposes the same API surface as `std::Vec` but some of its methods return a // `Result` to indicate whether the operation failed due to the `heapless::Vec` being full - defmt::info!("start: {:?}", defmt::Debug2Format(&buffer)); - // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log - // `heapless` data structures (like `Vec` here) - // with `defmt` + defmt::info!("start: {:?}", buffer); buffer.push(0).expect("buffer full"); - defmt::info!("after `push`: {:?}", defmt::Debug2Format(&buffer)); + defmt::info!("after `push`: {:?}", buffer); buffer.extend_from_slice(&[1, 2, 3]).expect("buffer full"); - defmt::info!("after `extend`: {:?}", defmt::Debug2Format(&buffer)); + defmt::info!("after `extend`: {:?}", &buffer); // TODO try this operation // buffer.extend_from_slice(&[4, 5, 6, 7]).expect("buffer full"); diff --git a/beginner/apps/Cargo.toml b/beginner/apps/Cargo.toml index 633f9b2..7716992 100644 --- a/beginner/apps/Cargo.toml +++ b/beginner/apps/Cargo.toml @@ -14,6 +14,10 @@ panic-probe = { version = "0.3.0", features = ["print-defmt"] } defmt = "0.3.0" defmt-rtt = "0.3.1" +[dependencies.heapless] +version = "0.7.1" +features = ["defmt-impl"] + # optimize code in both profiles [profile.dev] codegen-units = 1 diff --git a/beginner/apps/src/bin/radio-puzzle-2.rs b/beginner/apps/src/bin/radio-puzzle-2.rs index 083ed35..edab631 100644 --- a/beginner/apps/src/bin/radio-puzzle-2.rs +++ b/beginner/apps/src/bin/radio-puzzle-2.rs @@ -6,7 +6,7 @@ use cortex_m_rt::entry; // NOTE you can use `FnvIndexMap` instead of `LinearMap`; the former may have better // lookup performance when the dictionary contains a large number of items but performance is // not important for this exercise -use heapless::{consts, LinearMap}; +use heapless::LinearMap; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -15,8 +15,9 @@ fn main() -> ! { dk::init().unwrap(); // a dictionary with capacity for 2 elements - let mut dict = LinearMap::<_, _, consts::U2>::new(); - // ^^ capacity; this is a type not a value + let mut dict = LinearMap::<_, _, 2>::new(); + // content types ^^ ^^ ^ capacity + // (inferred by rust) // do some insertions dict.insert(b'A', b'*').expect("dictionary full"); diff --git a/beginner/apps/src/bin/radio-puzzle-3.rs b/beginner/apps/src/bin/radio-puzzle-3.rs index dea6b6d..96f9e39 100644 --- a/beginner/apps/src/bin/radio-puzzle-3.rs +++ b/beginner/apps/src/bin/radio-puzzle-3.rs @@ -4,7 +4,7 @@ use cortex_m_rt::entry; use dk::ieee802154::{Channel, Packet}; -use heapless::{consts, LinearMap}; +use heapless::LinearMap; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -20,7 +20,7 @@ fn main() -> ! { radio.set_channel(Channel::_25); // capacity (128) should be large enough for the ASCII range - let dict = LinearMap::::new(); + let mut dict = LinearMap::::new(); let mut packet = Packet::new(); // TODO do the whole ASCII range [0, 127] diff --git a/beginner/apps/src/bin/radio-puzzle-4.rs b/beginner/apps/src/bin/radio-puzzle-4.rs index 7b5950d..e4a8c69 100644 --- a/beginner/apps/src/bin/radio-puzzle-4.rs +++ b/beginner/apps/src/bin/radio-puzzle-4.rs @@ -5,7 +5,7 @@ use core::str; use cortex_m_rt::entry; -use heapless::{consts, Vec}; +use heapless::Vec; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -14,17 +14,15 @@ fn main() -> ! { dk::init().unwrap(); // a buffer with capacity for 2 bytes - let mut buffer = Vec::::new(); - // ^^ capacity; this is a type not a value + let mut buffer = Vec::::new(); + // content type ^^ ^ capacity // do some insertions buffer.push(b'H').expect("buffer full"); buffer.push(b'i').expect("buffer full"); // look into the contents so far - defmt::info!("{:?}", defmt::Debug2Format(&buffer)); - // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log `heapless` - // data structures (like `Vec` here) with `defmt` + defmt::info!("{:?}", buffer); // or more readable // NOTE utf-8 conversion works as long as you only push bytes in the ASCII range (0..=127) diff --git a/beginner/apps/src/bin/radio-puzzle-5.rs b/beginner/apps/src/bin/radio-puzzle-5.rs index 80be341..e46643a 100644 --- a/beginner/apps/src/bin/radio-puzzle-5.rs +++ b/beginner/apps/src/bin/radio-puzzle-5.rs @@ -6,7 +6,7 @@ use core::str; use cortex_m_rt::entry; use dk::ieee802154::{Channel, Packet}; -use heapless::{consts, Vec}; +use heapless::Vec; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -38,7 +38,7 @@ fn main() -> ! { ); /* # Decrypt the string */ - let mut buf = Vec::::new(); + let mut buf = Vec::::new(); // iterate over the bytes for input in packet.iter() { diff --git a/beginner/apps/src/bin/radio-puzzle-6.rs b/beginner/apps/src/bin/radio-puzzle-6.rs index 9ff925b..d1c326c 100644 --- a/beginner/apps/src/bin/radio-puzzle-6.rs +++ b/beginner/apps/src/bin/radio-puzzle-6.rs @@ -6,7 +6,7 @@ use core::str; use cortex_m_rt::entry; use dk::ieee802154::{Channel, Packet}; -use heapless::{consts, LinearMap, Vec}; +use heapless::{LinearMap, Vec}; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -22,7 +22,7 @@ fn main() -> ! { radio.set_channel(Channel::_25); /* # Build a dictionary */ - let dict = LinearMap::::new(); + let dict = LinearMap::::new(); let mut packet = Packet::new(); for source in 0..=127 { @@ -62,7 +62,7 @@ fn main() -> ! { ); /* # Decrypt the string */ - let mut buffer = Vec::::new(); + let mut buffer = Vec::::new(); // iterate over the bytes for byte in packet.iter() { diff --git a/beginner/apps/src/bin/radio-puzzle-7.rs b/beginner/apps/src/bin/radio-puzzle-7.rs index be25024..9116db8 100644 --- a/beginner/apps/src/bin/radio-puzzle-7.rs +++ b/beginner/apps/src/bin/radio-puzzle-7.rs @@ -6,7 +6,7 @@ use core::str; use cortex_m_rt::entry; use dk::ieee802154::{Channel, Packet}; -use heapless::{consts, LinearMap, Vec}; +use heapless::{LinearMap, Vec}; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -22,7 +22,7 @@ fn main() -> ! { radio.set_channel(Channel::_25); /* # Build a dictionary */ - let dict = LinearMap::::new(); + let dict = LinearMap::::new(); let mut packet = Packet::new(); for source in 0..=127 { @@ -62,7 +62,7 @@ fn main() -> ! { ); /* # Decrypt the string */ - let mut buffer = Vec::::new(); + let mut buffer = Vec::::new(); // iterate over the bytes for byte in packet.iter() { diff --git a/beginner/apps/src/bin/radio-puzzle-solution-2.rs b/beginner/apps/src/bin/radio-puzzle-solution-2.rs index 75205d0..6007f0f 100644 --- a/beginner/apps/src/bin/radio-puzzle-solution-2.rs +++ b/beginner/apps/src/bin/radio-puzzle-solution-2.rs @@ -6,7 +6,7 @@ use core::str; use cortex_m_rt::entry; use dk::ieee802154::{Channel, Packet}; -use heapless::{consts, LinearMap}; +use heapless::LinearMap; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -22,7 +22,7 @@ fn main() -> ! { radio.set_channel(Channel::_25); /* # Build a dictionary */ - let mut dict = LinearMap::::new(); + let mut dict = LinearMap::::new(); // the IEEE 802.15.4 packet that will carry our data let mut packet = Packet::new(); diff --git a/beginner/apps/src/bin/radio-puzzle-solution.rs b/beginner/apps/src/bin/radio-puzzle-solution.rs index 4988ddd..4a7e00f 100644 --- a/beginner/apps/src/bin/radio-puzzle-solution.rs +++ b/beginner/apps/src/bin/radio-puzzle-solution.rs @@ -6,7 +6,7 @@ use core::str; use cortex_m_rt::entry; use dk::ieee802154::{Channel, Packet}; -use heapless::{consts, LinearMap, Vec}; +use heapless::{LinearMap, Vec}; // this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior use apps as _; @@ -22,8 +22,8 @@ fn main() -> ! { radio.set_channel(Channel::_25); /* # Build a dictionary */ - let mut dict = LinearMap::::new(); - // ^^^^^^^^^^^^ NOTE larger capacity + let mut dict = LinearMap::::new(); + // ^^^ NOTE larger capacity // the IEEE 802.15.4 packet that will carry our data let mut packet = Packet::new(); @@ -66,7 +66,7 @@ fn main() -> ! { ); /* # Decrypt the string */ - let mut buffer = Vec::::new(); + let mut buffer = Vec::::new(); // iterate over the bytes for cipherletter in packet.iter() {