Skip to content

Commit

Permalink
Move unblocker to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 10, 2023
1 parent 102eb94 commit ec9f296
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 106 deletions.
70 changes: 0 additions & 70 deletions src/executor.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ compile_error!("You must enable at most one of the following features: defmt, lo

pub mod eth;
pub mod event_bus;
pub mod executor;
pub mod http;
pub mod io;
pub mod ipv4;
Expand Down
105 changes: 75 additions & 30 deletions src/utils/asyncify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::future::Future;

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pub mod event_bus;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
Expand All @@ -7,48 +9,91 @@ pub mod timer;
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
pub mod ws;

pub use async_wrapper::*;

#[cfg(feature = "alloc")]
pub use blocking_unblocker::*;

mod async_wrapper {
pub trait AsyncWrapper<S> {
fn new(sync: S) -> Self;
// Keep it GAT based for now so that it builds with stable Rust
// and therefore `crate::utils::asyncify` can also build with stable Rust
pub trait Unblocker {
type UnblockFuture<'a, F, T>: Future<Output = T> + Send
where
Self: 'a,
F: Send + 'a,
T: Send + 'a;

fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
where
F: FnOnce() -> T + Send + 'a,
T: Send + 'a;
}

impl<U> Unblocker for &U
where
U: Unblocker,
{
type UnblockFuture<'a, F, T>
= U::UnblockFuture<'a, F, T> where Self: 'a, F: Send + 'a, T: Send + 'a;

fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
where
F: FnOnce() -> T + Send + 'a,
T: Send + 'a,
{
(*self).unblock(f)
}
}

pub trait Asyncify {
type AsyncWrapper<S>: AsyncWrapper<S>;
impl<U> Unblocker for &mut U
where
U: Unblocker,
{
type UnblockFuture<'a, F, T>
= U::UnblockFuture<'a, F, T> where Self: 'a, F: Send + 'a, T: Send + 'a;

fn into_async(self) -> Self::AsyncWrapper<Self>
where
Self: Sized,
{
Self::AsyncWrapper::new(self)
}
fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
where
F: FnOnce() -> T + Send + 'a,
T: Send + 'a,
{
(**self).unblock(f)
}
}

fn as_async(&mut self) -> Self::AsyncWrapper<&mut Self> {
Self::AsyncWrapper::new(self)
}
pub trait AsyncWrapper<S> {
fn new(sync: S) -> Self;
}

pub trait Asyncify {
type AsyncWrapper<S>: AsyncWrapper<S>;

fn into_async(self) -> Self::AsyncWrapper<Self>
where
Self: Sized,
{
Self::AsyncWrapper::new(self)
}

pub trait UnblockingAsyncWrapper<U, S> {
fn new(unblocker: U, sync: S) -> Self;
fn as_async(&mut self) -> Self::AsyncWrapper<&mut Self> {
Self::AsyncWrapper::new(self)
}
}

pub trait UnblockingAsyncify {
type AsyncWrapper<U, S>: UnblockingAsyncWrapper<U, S>;
pub trait UnblockingAsyncWrapper<U, S> {
fn new(unblocker: U, sync: S) -> Self;
}

fn unblock_into_async<U>(self, unblocker: U) -> Self::AsyncWrapper<U, Self>
where
Self: Sized,
{
Self::AsyncWrapper::new(unblocker, self)
}
pub trait UnblockingAsyncify {
type AsyncWrapper<U, S>: UnblockingAsyncWrapper<U, S>;

fn unblock_as_async<U>(&mut self, unblocker: U) -> Self::AsyncWrapper<U, &mut Self> {
Self::AsyncWrapper::new(unblocker, self)
}
fn unblock_into_async<U>(self, unblocker: U) -> Self::AsyncWrapper<U, Self>
where
Self: Sized,
{
Self::AsyncWrapper::new(unblocker, self)
}

fn unblock_as_async<U>(&mut self, unblocker: U) -> Self::AsyncWrapper<U, &mut Self> {
Self::AsyncWrapper::new(unblocker, self)
}
}

Expand All @@ -75,7 +120,7 @@ mod blocking_unblocker {
}
}

impl crate::executor::asynch::Unblocker for BlockingUnblocker {
impl super::Unblocker for BlockingUnblocker {
type UnblockFuture<'a, F, T> = BlockingFuture<'a, T> where Self: 'a, F: Send + 'a, T: Send + 'a;

fn unblock<'a, F, T>(&'a self, f: F) -> Self::UnblockFuture<'a, F, T>
Expand Down
4 changes: 2 additions & 2 deletions src/utils/asyncify/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::time::Duration;
extern crate alloc;
use alloc::sync::Arc;

use crate::executor::asynch::Unblocker;
use crate::utils::asyncify::Unblocker;
use crate::utils::mutex::{Condvar, Mutex, RawCondvar};

#[cfg(feature = "nightly")]
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<CV, E> AsyncWrapper<E> for AsyncEventBus<(), CV, E> {
#[cfg(feature = "nightly")]
mod async_traits_impl {
use crate::event_bus::asynch::{ErrorType, EventBus, PostboxProvider, Receiver, Sender};
use crate::executor::asynch::Unblocker;
use crate::utils::asyncify::Unblocker;
use crate::utils::mutex::RawCondvar;

use super::{AsyncEventBus, AsyncPostbox, AsyncSubscription};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/asyncify/mqtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ pub mod client {
use alloc::sync::Arc;
use alloc::vec::Vec;

use crate::executor::asynch::Unblocker;
use crate::mqtt::client::asynch::{Client, Connection, MessageId, Publish, QoS};
use crate::mqtt::client::ErrorType;
use crate::utils::asyncify::mqtt::client::Event;
use crate::utils::asyncify::Unblocker;
use crate::utils::mutex::{Mutex, RawCondvar, RawMutex};

use super::{
Expand Down
4 changes: 2 additions & 2 deletions src/utils/asyncify/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub mod server {

impl<U, S> AsyncSender<U, S>
where
U: crate::executor::asynch::Unblocker,
U: crate::utils::asyncify::Unblocker,
S: Sender + SessionProvider + Send + Clone,
S::Error: Send + Sync,
{
Expand Down Expand Up @@ -439,7 +439,7 @@ pub mod server {
mod async_traits_impl {
use core::fmt::Debug;

use crate::executor::asynch::Unblocker;
use crate::utils::asyncify::Unblocker;
use crate::utils::mutex::RawCondvar;
use crate::ws::{callback_server::*, *};

Expand Down

0 comments on commit ec9f296

Please sign in to comment.