Split iced_futures into different backend implementations
This commit is contained in:
parent
5dab5a327e
commit
167be45a7d
19 changed files with 280 additions and 318 deletions
10
futures/src/backend.rs
Normal file
10
futures/src/backend.rs
Normal 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;
|
||||||
38
futures/src/backend/default.rs
Normal file
38
futures/src/backend/default.rs
Normal 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::*;
|
||||||
16
futures/src/backend/native.rs
Normal file
16
futures/src/backend/native.rs
Normal 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;
|
||||||
59
futures/src/backend/native/async_std.rs
Normal file
59
futures/src/backend/native/async_std.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
59
futures/src/backend/native/smol.rs
Normal file
59
futures/src/backend/native/smol.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
use crate::Executor;
|
//! A `ThreadPool` backend.
|
||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
/// A thread pool runtime for futures.
|
/// A thread pool executor for futures.
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "thread-pool")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "thread-pool")))]
|
||||||
pub type ThreadPool = futures::executor::ThreadPool;
|
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> {
|
fn new() -> Result<Self, futures::io::Error> {
|
||||||
futures::executor::ThreadPool::new()
|
futures::executor::ThreadPool::new()
|
||||||
}
|
}
|
||||||
72
futures/src/backend/native/tokio.rs
Normal file
72
futures/src/backend/native/tokio.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
use crate::Executor;
|
//! A backend that does nothing!
|
||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
/// An executor that drops all the futures, instead of spawning them.
|
/// An executor that drops all the futures, instead of spawning them.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Null;
|
pub struct Executor;
|
||||||
|
|
||||||
impl Executor for Null {
|
impl crate::Executor for Executor {
|
||||||
fn new() -> Result<Self, futures::io::Error> {
|
fn new() -> Result<Self, futures::io::Error> {
|
||||||
Ok(Self)
|
Ok(Self)
|
||||||
}
|
}
|
||||||
2
futures/src/backend/wasm.rs
Normal file
2
futures/src/backend/wasm.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
//! Backends that are only available on Wasm targets.
|
||||||
|
pub mod wasm_bindgen;
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::Executor;
|
//! A `wasm-bindgein-futures` backend.
|
||||||
|
|
||||||
/// A `wasm-bindgen-futures` runtime.
|
/// A `wasm-bindgen-futures` executor.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct WasmBindgen;
|
pub struct Executor;
|
||||||
|
|
||||||
impl Executor for WasmBindgen {
|
impl crate::Executor for Executor {
|
||||||
fn new() -> Result<Self, futures::io::Error> {
|
fn new() -> Result<Self, futures::io::Error> {
|
||||||
Ok(Self)
|
Ok(Self)
|
||||||
}
|
}
|
||||||
|
|
@ -1,38 +1,4 @@
|
||||||
//! Choose your preferred executor to power a runtime.
|
//! 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 crate::MaybeSend;
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -17,23 +17,10 @@ mod command;
|
||||||
mod maybe_send;
|
mod maybe_send;
|
||||||
mod runtime;
|
mod runtime;
|
||||||
|
|
||||||
|
pub mod backend;
|
||||||
pub mod executor;
|
pub mod executor;
|
||||||
pub mod subscription;
|
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 command::Command;
|
||||||
pub use executor::Executor;
|
pub use executor::Executor;
|
||||||
pub use maybe_send::MaybeSend;
|
pub use maybe_send::MaybeSend;
|
||||||
|
|
|
||||||
|
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,86 +1,14 @@
|
||||||
//! Choose your preferred executor to power your application.
|
//! Choose your preferred executor to power your application.
|
||||||
pub use crate::runtime::Executor;
|
pub use crate::runtime::Executor;
|
||||||
|
|
||||||
pub use platform::Default;
|
/// A default cross-platform executor.
|
||||||
|
///
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
/// - On native platforms, it will use:
|
||||||
mod platform {
|
/// - `iced_futures::backend::native::tokio` when the `tokio` feature is enabled.
|
||||||
use iced_futures::{executor, futures};
|
/// - `iced_futures::backend::native::async-std` when the `async-std` feature is
|
||||||
|
/// enabled.
|
||||||
#[cfg(feature = "tokio")]
|
/// - `iced_futures::backend::native::smol` when the `smol` feature is enabled.
|
||||||
type Executor = executor::Tokio;
|
/// - `iced_futures::backend::native::thread_pool` otherwise.
|
||||||
|
///
|
||||||
#[cfg(all(feature = "async-std", not(feature = "tokio"),))]
|
/// - On Wasm, it will use `iced_futures::backend::wasm::wasm_bindgen`.
|
||||||
type Executor = executor::AsyncStd;
|
pub type Default = iced_futures::backend::default::Executor;
|
||||||
|
|
||||||
#[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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ impl<T> Application for T
|
||||||
where
|
where
|
||||||
T: Sandbox,
|
T: Sandbox,
|
||||||
{
|
{
|
||||||
type Executor = crate::runtime::executor::Null;
|
type Executor = iced_futures::backend::null::Executor;
|
||||||
type Flags = ();
|
type Flags = ();
|
||||||
type Message = T::Message;
|
type Message = T::Message;
|
||||||
|
|
||||||
|
|
|
||||||
12
src/time.rs
12
src/time.rs
|
|
@ -1,12 +1,2 @@
|
||||||
//! Listen and react to time.
|
//! Listen and react to time.
|
||||||
pub use crate::runtime::time::{Duration, Instant};
|
pub use iced_futures::backend::default::time::*;
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue