Make defmt optional with new anyfmt crate

This commit is contained in:
Dario Nieuwenhuis
2020-11-01 17:17:24 +01:00
parent 2c13e25184
commit 5e8608c7a5
29 changed files with 267 additions and 233 deletions

View File

@ -6,7 +6,6 @@ edition = "2018"
[features]
std = ["futures/std"]
defmt-default = []
defmt-trace = []
defmt-debug = []
defmt-info = []
@ -14,9 +13,12 @@ defmt-warn = []
defmt-error = []
[dependencies]
defmt = "0.1.0"
anyfmt = { version = "0.1.0", path = "../anyfmt" }
defmt = { version = "0.1.0", optional = true }
cortex-m = "0.6.3"
futures = { version = "0.3.5", default-features = false }
pin-project = { version = "0.4.23", default-features = false }
futures-intrusive = { version = "0.3.1", default-features = false }
embassy-macros = { version = "0.1.0", path = "../embassy-macros"}

View File

@ -63,7 +63,8 @@ pub struct Task<F: Future + 'static> {
future: UninitCell<F>, // Valid if STATE_RUNNING
}
#[derive(Copy, Clone, Debug, defmt::Format)]
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SpawnError {
Busy,
}

View File

@ -1,7 +1,7 @@
use core::future::Future;
#[derive(defmt::Format, Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
Failed,
AddressMisaligned,
@ -48,4 +48,3 @@ pub trait Flash {
/// This is guaranteed to be a power of 2.
fn erase_size(&self) -> usize;
}

View File

@ -2,7 +2,8 @@
///
/// This list is intended to grow over time and it is not recommended to
/// exhaustively match against it.
#[derive(defmt::Format, Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
/// An entity was not found, often a file.
NotFound,

View File

@ -1,4 +1,5 @@
use crate::util::Dewrap;
use anyfmt::*;
pub trait Rand {
fn rand(&self, buf: &mut [u8]);
}
@ -10,5 +11,5 @@ pub unsafe fn set_rand(rand: &'static dyn Rand) {
}
pub fn rand(buf: &mut [u8]) {
unsafe { RAND.dexpect(defmt::intern!("No rand set")).rand(buf) }
unsafe { expect!(RAND, "No rand set").rand(buf) }
}

View File

@ -3,7 +3,8 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use super::TICKS_PER_SECOND;
#[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Duration {
pub(crate) ticks: u64,
}

View File

@ -5,7 +5,8 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
use super::TICKS_PER_SECOND;
use super::{now, Duration};
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Instant {
ticks: u64,
}

View File

@ -8,7 +8,7 @@ pub use instant::Instant;
pub use timer::Timer;
pub use traits::*;
use crate::util::Dewrap;
use anyfmt::*;
// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something.
pub const TICKS_PER_SECOND: u64 = 32768;
@ -20,5 +20,5 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) {
}
pub(crate) fn now() -> u64 {
unsafe { CLOCK.dexpect(defmt::intern!("No clock set")).now() }
unsafe { expect!(CLOCK, "No clock set").now() }
}

View File

@ -1,3 +1,4 @@
use anyfmt::panic;
use core::mem;
pub struct DropBomb {
@ -16,6 +17,6 @@ impl DropBomb {
impl Drop for DropBomb {
fn drop(&mut self) {
depanic!("boom")
panic!("boom")
}
}

View File

@ -1,34 +0,0 @@
#![macro_use]
#[macro_export]
macro_rules! depanic {
($( $i:expr ),*) => {
{
defmt::error!($( $i ),*);
panic!();
}
}
}
#[macro_export]
macro_rules! deassert {
($cond:expr) => {
deassert!($cond, "assertion failed");
};
($cond:expr, $msg:literal) => {
{
if !$cond {
defmt::error!($msg);
panic!();
}
}
};
($cond:expr, $msg:literal, $( $i:expr ),*) => {
{
if !$cond {
defmt::error!($msg, $( $i ),*);
panic!();
}
}
};
}

View File

@ -1,74 +1,11 @@
#![macro_use]
mod macros;
mod signal;
pub use signal::*;
mod portal;
pub use portal::*;
mod waker_store;
pub use waker_store::*;
mod drop_bomb;
pub use drop_bomb::*;
mod forever;
mod portal;
mod signal;
mod waker_store;
pub use drop_bomb::*;
pub use forever::*;
use defmt::{debug, error, info, intern, trace, warn};
pub use macros::*;
pub trait Dewrap<T> {
/// dewrap = defmt unwrap
fn dewrap(self) -> T;
/// dexpect = defmt expect
fn dexpect<M: defmt::Format>(self, msg: M) -> T;
fn dewarn<M: defmt::Format>(self, msg: M) -> Self;
}
impl<T> Dewrap<T> for Option<T> {
fn dewrap(self) -> T {
match self {
Some(t) => t,
None => depanic!("unwrap failed: enum is none"),
}
}
fn dexpect<M: defmt::Format>(self, msg: M) -> T {
match self {
Some(t) => t,
None => depanic!("unexpected None: {:?}", msg),
}
}
fn dewarn<M: defmt::Format>(self, msg: M) -> Self {
if self.is_none() {
warn!("{:?} is none", msg);
}
self
}
}
impl<T, E: defmt::Format> Dewrap<T> for Result<T, E> {
fn dewrap(self) -> T {
match self {
Ok(t) => t,
Err(e) => depanic!("unwrap failed: {:?}", e),
}
}
fn dexpect<M: defmt::Format>(self, msg: M) -> T {
match self {
Ok(t) => t,
Err(e) => depanic!("unexpected error: {:?}: {:?}", msg, e),
}
}
fn dewarn<M: defmt::Format>(self, msg: M) -> Self {
if let Err(e) = &self {
warn!("{:?} err: {:?}", msg, e);
}
self
}
}
pub use portal::*;
pub use signal::*;
pub use waker_store::*;

View File

@ -1,3 +1,4 @@
use anyfmt::panic;
use core::cell::UnsafeCell;
use core::future::Future;
use core::mem;
@ -27,7 +28,7 @@ impl<T> Portal<T> {
unsafe {
match *self.state.get() {
State::None => {}
State::Running => depanic!("Portall::call() called reentrantly"),
State::Running => panic!("Portall::call() called reentrantly"),
State::Waiting(func) => (*func)(val),
}
}
@ -58,7 +59,7 @@ impl<T> Portal<T> {
let state = &mut *self.state.get();
match state {
State::None => {}
_ => depanic!("Multiple tasks waiting on same portal"),
_ => panic!("Multiple tasks waiting on same portal"),
}
*state = State::Waiting(func_ptr);
}
@ -110,7 +111,7 @@ impl<T> Portal<T> {
let state = &mut *self.state.get();
match *state {
State::None => {}
_ => depanic!("Multiple tasks waiting on same portal"),
_ => panic!("Multiple tasks waiting on same portal"),
}
*state = State::Waiting(func_ptr);
}

View File

@ -1,3 +1,4 @@
use anyfmt::panic;
use core::cell::UnsafeCell;
use core::future::Future;
use core::mem;
@ -58,7 +59,7 @@ impl<'a, T: Send> Future for WaitFuture<'a, T> {
Poll::Pending
}
State::Waiting(w) if w.will_wake(cx.waker()) => Poll::Pending,
State::Waiting(_) => depanic!("waker overflow"),
State::Waiting(_) => panic!("waker overflow"),
State::Signaled(_) => match mem::replace(state, State::None) {
State::Signaled(res) => Poll::Ready(res),
_ => unreachable!(),