From e4319de3d526285381f5cc53e14f9a17d123a81a Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 3 Apr 2021 20:30:34 +0300 Subject: [PATCH] const generics --- Cargo.toml | 3 +- book/en/src/internals/tasks.md | 8 ++-- examples/shared.rs | 12 ++---- examples/static.rs | 12 ++---- macros/src/codegen/dispatchers.rs | 6 +-- macros/src/codegen/software_tasks.rs | 10 ++--- macros/src/codegen/timer_queue.rs | 4 +- macros/src/codegen/util.rs | 15 +------- src/export.rs | 9 ++--- src/linked_list.rs | 57 ++++++++++------------------ src/tq.rs | 8 ++-- 11 files changed, 50 insertions(+), 94 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44f5786c91..e3cb010b5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,9 +44,8 @@ cortex-m = "0.7.0" cortex-m-rtic-macros = { path = "macros", version = "0.6.0-alpha.5" } rtic-monotonic = "0.1.0-alpha.2" rtic-core = "0.3.1" -heapless = "0.6.1" +heapless = "0.7.1" bare-metal = "1.0.0" -generic-array = "0.14" [dependencies.dwt-systick-monotonic] version = "0.1.0-alpha.3" diff --git a/book/en/src/internals/tasks.md b/book/en/src/internals/tasks.md index a533dc0c26..0407176d60 100644 --- a/book/en/src/internals/tasks.md +++ b/book/en/src/internals/tasks.md @@ -78,8 +78,8 @@ mod app { } // ready queue of the task dispatcher - // `U4` is a type-level integer that represents the capacity of this queue - static mut RQ1: Queue, U4> = Queue::new(); + // `5-1=4` represents the capacity of this queue + static mut RQ1: Queue, 5> = Queue::new(); // interrupt handler chosen to dispatch tasks at priority `1` #[no_mangle] @@ -153,7 +153,7 @@ mod app { // used to track how many more `bar` messages can be enqueued // `U2` is the capacity of the `bar` task; a max of two instances can be queued // this queue is filled by the framework before `init` runs - static mut bar_FQ: Queue<(), U2> = Queue::new(); + static mut bar_FQ: Queue<(), 3> = Queue::new(); // Priority ceiling for the consumer endpoint of `bar_FQ` const bar_FQ_CEILING: u8 = 2; @@ -227,7 +227,7 @@ mod app { // the free queue: used to track free slots in the `baz_INPUTS` array // this queue is initialized with values `0` and `1` before `init` is executed - static mut baz_FQ: Queue = Queue::new(); + static mut baz_FQ: Queue = Queue::new(); // Priority ceiling for the consumer endpoint of `baz_FQ` const baz_FQ_CEILING: u8 = 2; diff --git a/examples/shared.rs b/examples/shared.rs index c3fa07b9c0..9585c3885e 100644 --- a/examples/shared.rs +++ b/examples/shared.rs @@ -10,23 +10,19 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] mod app { use cortex_m_semihosting::{debug, hprintln}; - use heapless::{ - consts::*, - i, - spsc::{Consumer, Producer, Queue}, - }; + use heapless::spsc::{Consumer, Producer, Queue}; use lm3s6965::Interrupt; #[shared] struct Shared { - p: Producer<'static, u32, U4>, - c: Consumer<'static, u32, U4>, + p: Producer<'static, u32, 5>, + c: Consumer<'static, u32, 5>, } #[local] struct Local {} - #[init(local = [q: Queue = Queue(i::Queue::new())])] + #[init(local = [q: Queue = Queue::new()])] fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { let (p, c) = cx.local.q.split(); diff --git a/examples/static.rs b/examples/static.rs index f51c5f2d10..0ea5d2df80 100644 --- a/examples/static.rs +++ b/examples/static.rs @@ -11,23 +11,19 @@ use panic_semihosting as _; mod app { use cortex_m_semihosting::{debug, hprintln}; - use heapless::{ - consts::*, - i, - spsc::{Consumer, Producer, Queue}, - }; + use heapless::spsc::{Consumer, Producer, Queue}; use lm3s6965::Interrupt; #[shared] struct Shared { - p: Producer<'static, u32, U4>, - c: Consumer<'static, u32, U4>, + p: Producer<'static, u32, 5>, + c: Consumer<'static, u32, 5>, } #[local] struct Local {} - #[init(local = [q: Queue = Queue(i::Queue::new())])] + #[init(local = [q: Queue = Queue::new()])] fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { let (p, c) = cx.local.q.split(); diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs index ac55003658..c239b0f828 100644 --- a/macros/src/codegen/dispatchers.rs +++ b/macros/src/codegen/dispatchers.rs @@ -42,15 +42,13 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec), - quote!(rtic::export::Queue(unsafe { - rtic::export::iQueue::u8_sc() - })), + quote!(rtic::export::Queue::new()), ) }; diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index cfd21e40d1..0b07335946 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -32,8 +32,8 @@ pub fn codegen( let (_, _, _, input_ty) = util::regroup_inputs(inputs); let cap = task.args.capacity; - let cap_lit = util::capacity_literal(cap); - let cap_ty = util::capacity_typenum(cap, true); + let cap_lit = util::capacity_literal(cap as usize); + let cap_lit_p1 = util::capacity_literal(cap as usize + 1); // Create free queues and inputs / instants buffers let fq = util::fq_ident(name); @@ -41,10 +41,8 @@ pub fn codegen( let (fq_ty, fq_expr, mk_uninit): (_, _, Box Option<_>>) = { ( - quote!(rtic::export::SCFQ<#cap_ty>), - quote!(rtic::export::Queue(unsafe { - rtic::export::iQueue::u8_sc() - })), + quote!(rtic::export::SCFQ<#cap_lit_p1>), + quote!(rtic::export::Queue::new()), Box::new(|| util::link_section_uninit()), ) }; diff --git a/macros/src/codegen/timer_queue.rs b/macros/src/codegen/timer_queue.rs index 9e30d1001d..abddbadcc0 100644 --- a/macros/src/codegen/timer_queue.rs +++ b/macros/src/codegen/timer_queue.rs @@ -62,12 +62,12 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec>); diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index 86bd69551d..c2330d46f8 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -8,23 +8,10 @@ use syn::{Attribute, Ident, LitInt, PatType}; use crate::check::Extra; /// Turns `capacity` into an unsuffixed integer literal -pub fn capacity_literal(capacity: u8) -> LitInt { +pub fn capacity_literal(capacity: usize) -> LitInt { LitInt::new(&capacity.to_string(), Span::call_site()) } -/// Turns `capacity` into a type-level (`typenum`) integer -pub fn capacity_typenum(capacity: u8, round_up_to_power_of_two: bool) -> TokenStream2 { - let capacity = if round_up_to_power_of_two { - capacity.checked_next_power_of_two().expect("UNREACHABLE") - } else { - capacity - }; - - let ident = Ident::new(&format!("U{}", capacity), Span::call_site()); - - quote!(rtic::export::consts::#ident) -} - /// Identifier for the free queue pub fn fq_ident(task: &Ident) -> Ident { Ident::new(&format!("{}_FQ", task.to_string()), Span::call_site()) diff --git a/src/export.rs b/src/export.rs index 91a4a5efba..e449ef41d5 100644 --- a/src/export.rs +++ b/src/export.rs @@ -13,13 +13,12 @@ pub use cortex_m::{ peripheral::{scb::SystemHandler, syst::SystClkSource, DWT, NVIC}, Peripherals, }; -use heapless::spsc::SingleCore; -pub use heapless::{consts, i::Queue as iQueue, spsc::Queue}; -pub use heapless::{i::BinaryHeap as iBinaryHeap, BinaryHeap}; +pub use heapless::spsc::Queue; +pub use heapless::BinaryHeap; pub use rtic_monotonic as monotonic; -pub type SCFQ = Queue; -pub type SCRQ = Queue<(T, u8), N, u8, SingleCore>; +pub type SCFQ = Queue; +pub type SCRQ = Queue<(T, u8), N>; #[cfg(armv7m)] #[inline(always)] diff --git a/src/linked_list.rs b/src/linked_list.rs index 9ea4d19f5c..bbb935f82b 100644 --- a/src/linked_list.rs +++ b/src/linked_list.rs @@ -3,8 +3,6 @@ use core::marker::PhantomData; use core::mem::MaybeUninit; use core::ops::{Deref, DerefMut}; use core::ptr; -pub use generic_array::ArrayLength; -use generic_array::GenericArray; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] struct LinkedIndex(u16); @@ -37,21 +35,19 @@ pub struct Node { } /// Iterator for the linked list. -pub struct Iter<'a, T, Kind, N> +pub struct Iter<'a, T, Kind, const N: usize> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { list: &'a LinkedList, index: LinkedIndex, } -impl<'a, T, Kind, N> Iterator for Iter<'a, T, Kind, N> +impl<'a, T, Kind, const N: usize> Iterator for Iter<'a, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { type Item = &'a T; @@ -66,11 +62,10 @@ where } /// Comes from [`LinkedList::find_mut`]. -pub struct FindMut<'a, T, Kind, N> +pub struct FindMut<'a, T, Kind, const N: usize> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { list: &'a mut LinkedList, is_head: bool, @@ -79,11 +74,10 @@ where maybe_changed: bool, } -impl<'a, T, Kind, N> FindMut<'a, T, Kind, N> +impl<'a, T, Kind, const N: usize> FindMut<'a, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn pop_internal(&mut self) -> T { if self.is_head { @@ -122,11 +116,10 @@ where } } -impl Drop for FindMut<'_, T, Kind, N> +impl Drop for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn drop(&mut self) { // Only resort the list if the element has changed @@ -137,11 +130,10 @@ where } } -impl Deref for FindMut<'_, T, Kind, N> +impl Deref for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { type Target = T; @@ -150,11 +142,10 @@ where } } -impl DerefMut for FindMut<'_, T, Kind, N> +impl DerefMut for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn deref_mut(&mut self) -> &mut Self::Target { self.maybe_changed = true; @@ -162,11 +153,10 @@ where } } -impl fmt::Debug for FindMut<'_, T, Kind, N> +impl fmt::Debug for FindMut<'_, T, Kind, N> where T: PartialEq + PartialOrd + core::fmt::Debug, Kind: kind::Kind, - N: ArrayLength>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("FindMut") @@ -189,23 +179,21 @@ where } /// The linked list. -pub struct LinkedList +pub struct LinkedList where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { - list: MaybeUninit, N>>, + list: MaybeUninit<[Node; N]>, head: LinkedIndex, free: LinkedIndex, _kind: PhantomData, } -impl LinkedList +impl LinkedList where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { /// Internal helper to not do pointer arithmetic all over the place. #[inline] @@ -266,7 +254,7 @@ where _kind: PhantomData, }; - let len = N::U16; + let len = N as u16; let mut free = 0; if len == 0 { @@ -451,11 +439,10 @@ where } } -impl Drop for LinkedList +impl Drop for LinkedList where T: PartialEq + PartialOrd, Kind: kind::Kind, - N: ArrayLength>, { fn drop(&mut self) { let mut index = self.head; @@ -471,11 +458,10 @@ where } } -impl fmt::Debug for LinkedList +impl fmt::Debug for LinkedList where T: PartialEq + PartialOrd + core::fmt::Debug, Kind: kind::Kind, - N: ArrayLength>, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_list().entries(self.iter()).finish() @@ -518,11 +504,10 @@ pub mod kind { mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; - use generic_array::typenum::consts::*; #[test] fn test_peek() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); assert_eq!(ll.peek().unwrap(), &1); @@ -533,7 +518,7 @@ mod tests { ll.push(3).unwrap(); assert_eq!(ll.peek().unwrap(), &3); - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(2).unwrap(); assert_eq!(ll.peek().unwrap(), &2); @@ -547,7 +532,7 @@ mod tests { #[test] fn test_full() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); @@ -557,14 +542,14 @@ mod tests { #[test] fn test_empty() { - let ll: LinkedList = LinkedList::new(); + let ll: LinkedList = LinkedList::new(); assert!(ll.is_empty()) } #[test] fn test_zero_size() { - let ll: LinkedList = LinkedList::new(); + let ll: LinkedList = LinkedList::new(); assert!(ll.is_empty()); assert!(ll.is_full()); @@ -572,7 +557,7 @@ mod tests { #[test] fn test_rejected_push() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); @@ -585,7 +570,7 @@ mod tests { #[test] fn test_updating() { - let mut ll: LinkedList = LinkedList::new(); + let mut ll: LinkedList = LinkedList::new(); ll.push(1).unwrap(); ll.push(2).unwrap(); ll.push(3).unwrap(); diff --git a/src/tq.rs b/src/tq.rs index 985e0f8817..cd44abe2bc 100644 --- a/src/tq.rs +++ b/src/tq.rs @@ -1,5 +1,5 @@ use crate::{ - linked_list::{ArrayLength, LinkedList, Min, Node}, + linked_list::{LinkedList, Min}, time::{Clock, Instant}, Monotonic, }; @@ -14,16 +14,14 @@ fn unwrapper(val: Result) -> T { } } -pub struct TimerQueue(pub LinkedList, Min, N>) +pub struct TimerQueue(pub LinkedList, Min, N>) where Mono: Monotonic, - N: ArrayLength>>, Task: Copy; -impl TimerQueue +impl TimerQueue where Mono: Monotonic, - N: ArrayLength>>, Task: Copy, { pub fn new() -> Self {