Get rid of some warnings

This commit is contained in:
Thales Fragoso
2021-02-13 21:41:36 -03:00
parent a7797a918d
commit b69f72e055
40 changed files with 194 additions and 228 deletions

View File

@ -16,7 +16,7 @@ mod util;
mod waker;
use self::util::UninitCell;
use crate::fmt::{panic, *};
use crate::fmt::panic;
use crate::interrupt::OwnedInterrupt;
use crate::time::Alarm;
@ -56,10 +56,10 @@ impl<F: Future + 'static> Task<F> {
}
}
return SpawnToken {
SpawnToken {
raw_task: None,
phantom: PhantomData,
};
}
}
unsafe fn poll(p: NonNull<raw::Task>) {

View File

@ -2,7 +2,7 @@ use core::cell::Cell;
use core::cmp::min;
use core::ptr;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicPtr, Ordering};
use core::sync::atomic::Ordering;
use super::raw::{Task, STATE_TIMER_QUEUED};
use crate::time::Instant;

View File

@ -1,4 +1,5 @@
#![macro_use]
#![allow(clippy::module_inception)]
#[cfg(all(feature = "defmt", feature = "log"))]
compile_error!("You may not enable both `defmt` and `log` features.");

View File

@ -1,6 +1,5 @@
use core::mem;
use core::ptr;
use core::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use core::sync::atomic::{AtomicPtr, Ordering};
use cortex_m::peripheral::NVIC;
pub use embassy_macros::interrupt_declare as declare;

View File

@ -71,7 +71,7 @@ where
let i = ready!(Pin::new(&mut this.writer).poll_write(cx, buffer))?;
if i == 0 {
return Poll::Ready(Err(Error::WriteZero.into()));
return Poll::Ready(Err(Error::WriteZero));
}
*this.amt += i;
this.reader.as_mut().consume(i);

View File

@ -1,10 +1,8 @@
use core::iter::Iterator;
use core::pin::Pin;
use futures::future::Future;
use futures::ready;
use futures::task::{Context, Poll};
use super::super::error::{Error, Result};
use super::super::error::Result;
use super::super::traits::AsyncBufRead;
pub struct Drain<'a, R: ?Sized> {

View File

@ -75,14 +75,14 @@ pub trait AsyncBufReadExt: AsyncBufRead {
ReadWhile::new(self, f, buf)
}
fn skip_while<'a, F: Fn(u8) -> bool>(&'a mut self, f: F) -> SkipWhile<'a, Self, F>
fn skip_while<F: Fn(u8) -> bool>(&mut self, f: F) -> SkipWhile<Self, F>
where
Self: Unpin,
{
SkipWhile::new(self, f)
}
fn drain<'a>(&'a mut self) -> Drain<'a, Self>
fn drain(&mut self) -> Drain<Self>
where
Self: Unpin,
{
@ -96,14 +96,14 @@ pub trait AsyncBufReadExt: AsyncBufRead {
Read::new(self, buf)
}
fn read_buf<'a>(&'a mut self) -> ReadBuf<'a, Self>
fn read_buf(&mut self) -> ReadBuf<Self>
where
Self: Unpin,
{
ReadBuf::new(self)
}
fn read_byte<'a>(&'a mut self) -> ReadByte<'a, Self>
fn read_byte(&mut self) -> ReadByte<Self>
where
Self: Unpin,
{
@ -147,12 +147,19 @@ pub trait AsyncWriteExt: AsyncWrite {
WriteAll::new(self, buf)
}
fn write_byte<'a>(&'a mut self, byte: u8) -> WriteByte<'a, Self>
fn write_byte(&mut self, byte: u8) -> WriteByte<Self>
where
Self: Unpin,
{
WriteByte::new(self, byte)
}
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>
where
Self: Unpin,
{
Write::new(self, buf)
}
}
impl<R: AsyncWrite + ?Sized> AsyncWriteExt for R {}

View File

@ -25,7 +25,7 @@ impl<'a, R: AsyncBufRead + ?Sized + Unpin> Future for ReadByte<'a, R> {
let Self { reader } = &mut *self;
let mut reader = Pin::new(reader);
let rbuf = ready!(reader.as_mut().poll_fill_buf(cx))?;
if rbuf.len() == 0 {
if rbuf.is_empty() {
return Poll::Ready(Err(Error::UnexpectedEof));
}

View File

@ -31,7 +31,7 @@ impl<R: AsyncBufRead + ?Sized + Unpin> Future for ReadExact<'_, R> {
let this = &mut *self;
while !this.buf.is_empty() {
let buf = ready!(Pin::new(&mut this.reader).poll_fill_buf(cx))?;
if buf.len() == 0 {
if buf.is_empty() {
return Poll::Ready(Err(Error::UnexpectedEof));
}

View File

@ -29,7 +29,7 @@ impl<'a, R: AsyncBufRead + ?Sized + Unpin> Future for ReadToEnd<'a, R> {
let mut reader = Pin::new(reader);
loop {
let rbuf = ready!(reader.as_mut().poll_fill_buf(cx))?;
if rbuf.len() == 0 {
if rbuf.is_empty() {
return Poll::Ready(Ok(*n));
}

View File

@ -35,7 +35,7 @@ impl<'a, R: AsyncBufRead + ?Sized + Unpin, F: Fn(u8) -> bool> Future for ReadWhi
let mut reader = Pin::new(reader);
loop {
let rbuf = ready!(reader.as_mut().poll_fill_buf(cx))?;
if rbuf.len() == 0 {
if rbuf.is_empty() {
return Poll::Ready(Err(Error::UnexpectedEof));
}

View File

@ -28,7 +28,7 @@ impl<'a, R: AsyncBufRead + ?Sized + Unpin, F: Fn(u8) -> bool> Future for SkipWhi
let mut reader = Pin::new(reader);
loop {
let buf = ready!(reader.as_mut().poll_fill_buf(cx))?;
if buf.len() == 0 {
if buf.is_empty() {
return Poll::Ready(Err(Error::UnexpectedEof));
}

View File

@ -1,4 +1,3 @@
use core::convert::TryInto;
use core::fmt;
use core::ops::{Add, AddAssign, Sub, SubAssign};
@ -49,7 +48,7 @@ impl Instant {
pub fn duration_since(&self, earlier: Instant) -> Duration {
Duration {
ticks: (self.ticks - earlier.ticks).try_into().unwrap(),
ticks: self.ticks.checked_sub(earlier.ticks).unwrap(),
}
}
@ -58,7 +57,7 @@ impl Instant {
None
} else {
Some(Duration {
ticks: (self.ticks - earlier.ticks).try_into().unwrap(),
ticks: self.ticks - earlier.ticks,
})
}
}
@ -68,7 +67,7 @@ impl Instant {
ticks: if self.ticks < earlier.ticks {
0
} else {
(self.ticks - earlier.ticks).try_into().unwrap()
self.ticks - earlier.ticks
},
}
}
@ -79,12 +78,12 @@ impl Instant {
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
self.ticks
.checked_add(duration.ticks.into())
.checked_add(duration.ticks)
.map(|ticks| Instant { ticks })
}
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
self.ticks
.checked_sub(duration.ticks.into())
.checked_sub(duration.ticks)
.map(|ticks| Instant { ticks })
}
}

View File

@ -1,7 +1,7 @@
use core::cell::UnsafeCell;
use cortex_m::interrupt::CriticalSection;
use crate::fmt::{assert, panic, *};
use crate::fmt::assert;
/// A "mutex" based on critical sections
///

View File

@ -1,6 +1,5 @@
use crate::fmt::panic;
use core::cell::UnsafeCell;
use core::future::Future;
use core::mem;
use core::mem::MaybeUninit;
@ -34,93 +33,89 @@ impl<T> Portal<T> {
}
}
pub fn wait_once<'a, R, F>(&'a self, mut func: F) -> impl Future<Output = R> + 'a
pub async fn wait_once<'a, R, F>(&'a self, mut func: F) -> R
where
F: FnMut(T) -> R + 'a,
{
async move {
let bomb = DropBomb::new();
let signal = Signal::new();
let mut result: MaybeUninit<R> = MaybeUninit::uninit();
let mut call_func = |val: T| {
unsafe {
let state = &mut *self.state.get();
*state = State::None;
result.as_mut_ptr().write(func(val))
};
signal.signal(());
};
let func_ptr: *mut dyn FnMut(T) = &mut call_func as _;
let func_ptr: *mut dyn FnMut(T) = unsafe { mem::transmute(func_ptr) };
let bomb = DropBomb::new();
let signal = Signal::new();
let mut result: MaybeUninit<R> = MaybeUninit::uninit();
let mut call_func = |val: T| {
unsafe {
let state = &mut *self.state.get();
match state {
State::None => {}
_ => panic!("Multiple tasks waiting on same portal"),
}
*state = State::Waiting(func_ptr);
*state = State::None;
result.as_mut_ptr().write(func(val))
};
signal.signal(());
};
let func_ptr: *mut dyn FnMut(T) = &mut call_func as _;
let func_ptr: *mut dyn FnMut(T) = unsafe { mem::transmute(func_ptr) };
unsafe {
let state = &mut *self.state.get();
match state {
State::None => {}
_ => panic!("Multiple tasks waiting on same portal"),
}
signal.wait().await;
bomb.defuse();
unsafe { result.assume_init() }
*state = State::Waiting(func_ptr);
}
signal.wait().await;
bomb.defuse();
unsafe { result.assume_init() }
}
pub fn wait_many<'a, R, F>(&'a self, mut func: F) -> impl Future<Output = R> + 'a
pub async fn wait_many<'a, R, F>(&'a self, mut func: F) -> R
where
F: FnMut(T) -> Option<R> + 'a,
{
async move {
let bomb = DropBomb::new();
let signal = Signal::new();
let mut result: MaybeUninit<R> = MaybeUninit::uninit();
let mut call_func = |val: T| {
unsafe {
let state = &mut *self.state.get();
let func_ptr = match *state {
State::Waiting(p) => p,
_ => unreachable!(),
};
// Set state to Running while running the function to avoid reentrancy.
*state = State::Running;
*state = match func(val) {
None => State::Waiting(func_ptr),
Some(res) => {
result.as_mut_ptr().write(res);
signal.signal(());
State::None
}
};
};
};
let func_ptr: *mut dyn FnMut(T) = &mut call_func as _;
let func_ptr: *mut dyn FnMut(T) = unsafe { mem::transmute(func_ptr) };
let bomb = DropBomb::new();
let signal = Signal::new();
let mut result: MaybeUninit<R> = MaybeUninit::uninit();
let mut call_func = |val: T| {
unsafe {
let state = &mut *self.state.get();
match *state {
State::None => {}
_ => panic!("Multiple tasks waiting on same portal"),
}
*state = State::Waiting(func_ptr);
let func_ptr = match *state {
State::Waiting(p) => p,
_ => unreachable!(),
};
// Set state to Running while running the function to avoid reentrancy.
*state = State::Running;
*state = match func(val) {
None => State::Waiting(func_ptr),
Some(res) => {
result.as_mut_ptr().write(res);
signal.signal(());
State::None
}
};
};
};
let func_ptr: *mut dyn FnMut(T) = &mut call_func as _;
let func_ptr: *mut dyn FnMut(T) = unsafe { mem::transmute(func_ptr) };
unsafe {
let state = &mut *self.state.get();
match *state {
State::None => {}
_ => panic!("Multiple tasks waiting on same portal"),
}
signal.wait().await;
bomb.defuse();
unsafe { result.assume_init() }
*state = State::Waiting(func_ptr);
}
signal.wait().await;
bomb.defuse();
unsafe { result.assume_init() }
}
}

View File

@ -32,9 +32,8 @@ impl<T: Send> Signal<T> {
pub fn signal(&self, val: T) {
cortex_m::interrupt::free(|_| unsafe {
let state = &mut *self.state.get();
match mem::replace(state, State::Signaled(val)) {
State::Waiting(waker) => waker.wake(),
_ => {}
if let State::Waiting(waker) = mem::replace(state, State::Signaled(val)) {
waker.wake();
}
})
}
@ -98,9 +97,7 @@ impl<'a, I: OwnedInterrupt> InterruptFuture<'a, I> {
interrupt.unpend();
interrupt.enable();
Self {
interrupt: interrupt,
}
Self { interrupt }
}
unsafe fn interrupt_handler(ctx: *mut ()) {
@ -109,7 +106,7 @@ impl<'a, I: OwnedInterrupt> InterruptFuture<'a, I> {
_ => unreachable!(),
};
if ctx as *const _ != ptr::null() {
if !ctx.is_null() {
executor::raw::wake_task(ptr::NonNull::new_unchecked(ctx as _));
}

View File

@ -40,7 +40,9 @@ impl WakerRegistration {
/// Wake the registered waker, if any.
pub fn wake(&mut self) {
self.waker.take().map(|w| w.wake());
if let Some(w) = self.waker.take() {
w.wake()
}
}
pub fn context(&self) -> Option<Context<'_>> {