Split iced_futures into different backend implementations

This commit is contained in:
Héctor Ramón Jiménez 2022-01-28 18:24:07 +07:00
parent 5dab5a327e
commit 167be45a7d
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
19 changed files with 280 additions and 318 deletions

10
futures/src/backend.rs Normal file
View file

@ -0,0 +1,10 @@
//! The underlying implementations of the `iced_futures` contract!
pub mod null;
#[cfg(not(target_arch = "wasm32"))]
pub mod native;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
pub mod default;

View file

@ -0,0 +1,38 @@
//! A default, cross-platform backend.
//!
//! - On native platforms, it will use:
//! - `backend::native::tokio` when the `tokio` feature is enabled.
//! - `backend::native::async-std` when the `async-std` feature is
//! enabled.
//! - `backend::native::smol` when the `smol` feature is enabled.
//! - `backend::native::thread_pool` otherwise.
//!
//! - On Wasm, it will use `backend::wasm::wasm_bindgen`.
#[cfg(not(target_arch = "wasm32"))]
mod platform {
#[cfg(feature = "tokio")]
pub use crate::backend::native::tokio::*;
#[cfg(all(feature = "async-std", not(feature = "tokio"),))]
pub use crate::backend::native::async_std::*;
#[cfg(all(
feature = "smol",
not(any(feature = "tokio", feature = "async-std")),
))]
pub use crate::backend::native::smol::*;
#[cfg(not(any(
feature = "tokio",
feature = "async-std",
feature = "smol",
)))]
pub use crate::backend::native::thread_pool::*;
}
#[cfg(target_arch = "wasm32")]
mod platform {
pub use crate::backend::wasm::wasm_bindgen::*;
}
pub use platform::*;

View file

@ -0,0 +1,16 @@
//! Backends that are only available in native platforms: Windows, macOS, or Linux.
#[cfg_attr(docsrs, doc(cfg(feature = "tokio",)))]
#[cfg(feature = "tokio")]
pub mod tokio;
#[cfg_attr(docsrs, doc(cfg(feature = "async-std",)))]
#[cfg(feature = "async-std")]
pub mod async_std;
#[cfg_attr(docsrs, doc(cfg(feature = "smol",)))]
#[cfg(feature = "smol")]
pub mod smol;
#[cfg_attr(docsrs, doc(cfg(feature = "thread-pool",)))]
#[cfg(feature = "thread-pool")]
pub mod thread_pool;

View file

@ -0,0 +1,59 @@
//! An `async-std` backend.
use futures::Future;
/// An `async-std` executor.
#[derive(Debug)]
pub struct Executor;
impl crate::Executor for Executor {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _ = async_std::task::spawn(future);
}
}
pub mod time {
//! Listen and react to time.
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
pub fn every<H: std::hash::Hasher, E>(
duration: std::time::Duration,
) -> Subscription<H, E, std::time::Instant> {
Subscription::from_recipe(Every(duration))
}
#[derive(Debug)]
struct Every(std::time::Duration);
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| std::time::Instant::now())
.boxed()
}
}
}

View file

@ -0,0 +1,59 @@
//! A `smol` backend.
use futures::Future;
/// A `smol` executor.
#[cfg_attr(docsrs, doc(cfg(feature = "smol")))]
#[derive(Debug)]
pub struct Executor;
impl crate::Executor for Executor {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
smol::spawn(future).detach();
}
}
pub mod time {
//! Listen and react to time.
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
pub fn every<H: std::hash::Hasher, E>(
duration: std::time::Duration,
) -> Subscription<H, E, std::time::Instant> {
Subscription::from_recipe(Every(duration))
}
#[derive(Debug)]
struct Every(std::time::Duration);
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
smol::Timer::interval(self.0).boxed()
}
}
}

View file

@ -1,12 +1,11 @@
use crate::Executor;
//! A `ThreadPool` backend.
use futures::Future;
/// A thread pool runtime for futures.
/// A thread pool executor for futures.
#[cfg_attr(docsrs, doc(cfg(feature = "thread-pool")))]
pub type ThreadPool = futures::executor::ThreadPool;
impl Executor for futures::executor::ThreadPool {
impl crate::Executor for futures::executor::ThreadPool {
fn new() -> Result<Self, futures::io::Error> {
futures::executor::ThreadPool::new()
}

View file

@ -0,0 +1,72 @@
//! A `tokio` backend.
use futures::Future;
/// A `tokio` executor.
pub type Executor = tokio::runtime::Runtime;
impl crate::Executor for Executor {
fn new() -> Result<Self, futures::io::Error> {
tokio::runtime::Runtime::new()
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _ = tokio::runtime::Runtime::spawn(self, future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
let _guard = tokio::runtime::Runtime::enter(self);
f()
}
}
pub mod time {
//! Listen and react to time.
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
pub fn every<H: std::hash::Hasher, E>(
duration: std::time::Duration,
) -> Subscription<H, E, std::time::Instant> {
Subscription::from_recipe(Every(duration))
}
#[derive(Debug)]
struct Every(std::time::Duration);
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
let start = tokio::time::Instant::now() + self.0;
let stream = {
futures::stream::unfold(
tokio::time::interval_at(start, self.0),
|mut interval| async move {
Some((interval.tick().await, interval))
},
)
};
stream.map(tokio::time::Instant::into_std).boxed()
}
}
}

View file

@ -1,12 +1,11 @@
use crate::Executor;
//! A backend that does nothing!
use futures::Future;
/// An executor that drops all the futures, instead of spawning them.
#[derive(Debug)]
pub struct Null;
pub struct Executor;
impl Executor for Null {
impl crate::Executor for Executor {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}

View file

@ -0,0 +1,2 @@
//! Backends that are only available on Wasm targets.
pub mod wasm_bindgen;

View file

@ -1,10 +1,10 @@
use crate::Executor;
//! A `wasm-bindgein-futures` backend.
/// A `wasm-bindgen-futures` runtime.
/// A `wasm-bindgen-futures` executor.
#[derive(Debug)]
pub struct WasmBindgen;
pub struct Executor;
impl Executor for WasmBindgen {
impl crate::Executor for Executor {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}

View file

@ -1,38 +1,4 @@
//! Choose your preferred executor to power a runtime.
mod null;
#[cfg(all(not(target_arch = "wasm32"), feature = "thread-pool"))]
mod thread_pool;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
mod tokio;
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
mod async_std;
#[cfg(all(not(target_arch = "wasm32"), feature = "smol"))]
mod smol;
#[cfg(target_arch = "wasm32")]
mod wasm_bindgen;
pub use null::Null;
#[cfg(all(not(target_arch = "wasm32"), feature = "thread-pool"))]
pub use thread_pool::ThreadPool;
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
pub use self::tokio::Tokio;
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
pub use self::async_std::AsyncStd;
#[cfg(all(not(target_arch = "wasm32"), feature = "smol"))]
pub use self::smol::Smol;
#[cfg(target_arch = "wasm32")]
pub use wasm_bindgen::WasmBindgen;
use crate::MaybeSend;
use futures::Future;

View file

@ -1,18 +0,0 @@
use crate::Executor;
use futures::Future;
/// An `async-std` runtime.
#[cfg_attr(docsrs, doc(cfg(feature = "async-std")))]
#[derive(Debug)]
pub struct AsyncStd;
impl Executor for AsyncStd {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _ = async_std::task::spawn(future);
}
}

View file

@ -1,18 +0,0 @@
use crate::Executor;
use futures::Future;
/// A `smol` runtime.
#[cfg_attr(docsrs, doc(cfg(feature = "smol")))]
#[derive(Debug)]
pub struct Smol;
impl Executor for Smol {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
smol::spawn(future).detach();
}
}

View file

@ -1,22 +0,0 @@
use crate::Executor;
use futures::Future;
/// A `tokio` runtime.
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub type Tokio = tokio::runtime::Runtime;
impl Executor for Tokio {
fn new() -> Result<Self, futures::io::Error> {
tokio::runtime::Runtime::new()
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _ = tokio::runtime::Runtime::spawn(self, future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
let _guard = tokio::runtime::Runtime::enter(self);
f()
}
}

View file

@ -17,23 +17,10 @@ mod command;
mod maybe_send;
mod runtime;
pub mod backend;
pub mod executor;
pub mod subscription;
#[cfg(all(
any(feature = "tokio", feature = "async-std", feature = "smol"),
not(target_arch = "wasm32")
))]
#[cfg_attr(
docsrs,
doc(cfg(any(
feature = "tokio",
feature = "async-std",
feature = "smol"
)))
)]
pub mod time;
pub use command::Command;
pub use executor::Executor;
pub use maybe_send::MaybeSend;

View file

@ -1,105 +0,0 @@
//! Listen and react to time.
use crate::subscription::{self, Subscription};
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
pub fn every<H: std::hash::Hasher, E>(
duration: std::time::Duration,
) -> Subscription<H, E, std::time::Instant> {
Subscription::from_recipe(Every(duration))
}
struct Every(std::time::Duration);
#[cfg(all(
not(any(feature = "tokio", feature = "async-std")),
feature = "smol"
))]
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
smol::Timer::interval(self.0).boxed()
}
}
#[cfg(feature = "async-std")]
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
async_std::stream::interval(self.0)
.map(|_| std::time::Instant::now())
.boxed()
}
}
#[cfg(all(
feature = "tokio",
not(any(feature = "async-std", feature = "smol"))
))]
impl<H, E> subscription::Recipe<H, E> for Every
where
H: std::hash::Hasher,
{
type Output = std::time::Instant;
fn hash(&self, state: &mut H) {
use std::hash::Hash;
std::any::TypeId::of::<Self>().hash(state);
self.0.hash(state);
}
fn stream(
self: Box<Self>,
_input: futures::stream::BoxStream<'static, E>,
) -> futures::stream::BoxStream<'static, Self::Output> {
use futures::stream::StreamExt;
let start = tokio::time::Instant::now() + self.0;
let stream = {
futures::stream::unfold(
tokio::time::interval_at(start, self.0),
|mut interval| async move {
Some((interval.tick().await, interval))
},
)
};
stream.map(tokio::time::Instant::into_std).boxed()
}
}

View file

@ -1,86 +1,14 @@
//! Choose your preferred executor to power your application.
pub use crate::runtime::Executor;
pub use platform::Default;
#[cfg(not(target_arch = "wasm32"))]
mod platform {
use iced_futures::{executor, futures};
#[cfg(feature = "tokio")]
type Executor = executor::Tokio;
#[cfg(all(feature = "async-std", not(feature = "tokio"),))]
type Executor = executor::AsyncStd;
#[cfg(all(
feature = "smol",
not(any(feature = "tokio", feature = "async-std")),
))]
type Executor = executor::Smol;
#[cfg(not(any(
feature = "tokio",
feature = "async-std",
feature = "smol",
)))]
type Executor = executor::ThreadPool;
/// A default cross-platform executor.
///
/// - On native platforms, it will use:
/// - `iced_futures::executor::Tokio` when the `tokio` feature is enabled.
/// - `iced_futures::executor::AsyncStd` when the `async-std` feature is
/// enabled.
/// - `iced_futures::executor::ThreadPool` otherwise.
/// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
#[derive(Debug)]
pub struct Default(Executor);
impl super::Executor for Default {
fn new() -> Result<Self, futures::io::Error> {
Ok(Default(Executor::new()?))
}
fn spawn(
&self,
future: impl futures::Future<Output = ()> + Send + 'static,
) {
let _ = self.0.spawn(future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
super::Executor::enter(&self.0, f)
}
}
}
#[cfg(target_arch = "wasm32")]
mod platform {
use iced_futures::{executor::WasmBindgen, futures, Executor};
/// A default cross-platform executor.
///
/// - On native platforms, it will use:
/// - `iced_futures::executor::Tokio` when the `tokio` feature is enabled.
/// - `iced_futures::executor::AsyncStd` when the `async-std` feature is
/// enabled.
/// - `iced_futures::executor::ThreadPool` otherwise.
/// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
#[derive(Debug)]
pub struct Default(WasmBindgen);
impl Executor for Default {
fn new() -> Result<Self, futures::io::Error> {
Ok(Default(WasmBindgen::new()?))
}
fn spawn(&self, future: impl futures::Future<Output = ()> + 'static) {
self.0.spawn(future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
self.0.enter(f)
}
}
}
/// A default cross-platform executor.
///
/// - On native platforms, it will use:
/// - `iced_futures::backend::native::tokio` when the `tokio` feature is enabled.
/// - `iced_futures::backend::native::async-std` when the `async-std` feature is
/// enabled.
/// - `iced_futures::backend::native::smol` when the `smol` feature is enabled.
/// - `iced_futures::backend::native::thread_pool` otherwise.
///
/// - On Wasm, it will use `iced_futures::backend::wasm::wasm_bindgen`.
pub type Default = iced_futures::backend::default::Executor;

View file

@ -156,7 +156,7 @@ impl<T> Application for T
where
T: Sandbox,
{
type Executor = crate::runtime::executor::Null;
type Executor = iced_futures::backend::null::Executor;
type Flags = ();
type Message = T::Message;

View file

@ -1,12 +1,2 @@
//! Listen and react to time.
pub use crate::runtime::time::{Duration, Instant};
use crate::Subscription;
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
pub fn every(duration: Duration) -> Subscription<Instant> {
iced_futures::time::every(duration)
}
pub use iced_futures::backend::default::time::*;