Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only non-send futures #77

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion maplibre/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ readme = "../README.md"
web-webgl = ["wgpu/webgl"]
# Enable tracing using tracy on desktop/mobile and the chrome profiler on web
trace = ["tracing-subscriber", "tracing-tracy", "tracy-client"]
no-thread-safe-futures = []
embed-static-tiles = ["maplibre-build-tools/sqlite"]
headless = ["png"]


[target.'cfg(any(target_os = "macos", target_os = "ios", target_os = "linux", target_os = "android", target_os = "windows"))'.dependencies]
tokio = { version = "1.20.1", features = ["macros", "rt", "rt-multi-thread", "sync", "time"] }
tokio-util = { version = "0.7.1", features = ["rt"] }
env_logger = "0.9.0"
reqwest = { version = "0.11.11", default-features = false, features = ["rustls-tls", "gzip"] }
reqwest-middleware-cache = "0.1.1"
Expand Down
9 changes: 0 additions & 9 deletions maplibre/src/io/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ where

/// Can schedule a task from a future factory and a shared state.
pub trait ScheduleMethod: 'static {
#[cfg(not(feature = "no-thread-safe-futures"))]
fn schedule<T>(
&self,
future_factory: impl (FnOnce() -> T) + Send + 'static,
) -> Result<(), Error>
where
T: Future<Output = ()> + Send + 'static;

#[cfg(feature = "no-thread-safe-futures")]
fn schedule<T>(
&self,
future_factory: impl (FnOnce() -> T) + Send + 'static,
Expand Down
14 changes: 5 additions & 9 deletions maplibre/src/io/source_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ use async_trait::async_trait;
use crate::{coords::WorldTileCoords, error::Error, style::source::TileAddressingScheme};

/// A closure that returns a HTTP client.
pub type HTTPClientFactory<HC> = dyn Fn() -> HC;
pub type HttpClientFactory<HC> = dyn Fn() -> HC;

/// On the web platform futures are not thread-safe (i.e. not Send). This means we need to tell
/// async_trait that these bounds should not be placed on the async trait:
/// [https://github.com/dtolnay/async-trait/blob/b70720c4c1cc0d810b7446efda44f81310ee7bf2/README.md#non-threadsafe-futures](https://github.com/dtolnay/async-trait/blob/b70720c4c1cc0d810b7446efda44f81310ee7bf2/README.md#non-threadsafe-futures)
///
/// Users of this library can decide whether futures from the HTTPClient are thread-safe or not via
/// the future "no-thread-safe-futures". Tokio futures are thread-safe.
#[cfg_attr(feature = "no-thread-safe-futures", async_trait(?Send))]
#[cfg_attr(not(feature = "no-thread-safe-futures"), async_trait)]
// On the web platform futures are not thread-safe (i.e. not Send). This means we need to tell
// async_trait that these bounds should not be placed on the async trait:
// https://github.com/dtolnay/async-trait/blob/b70720c4c1cc0d810b7446efda44f81310ee7bf2/README.md#non-threadsafe-futures
#[async_trait(?Send)]
pub trait HttpClient: Clone + Sync + Send + 'static {
async fn fetch(&self, url: &str) -> Result<Vec<u8>, Error>;
}
Expand Down
2 changes: 1 addition & 1 deletion maplibre/src/platform/noweb/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl ReqwestHttpClient {
}
}

#[async_trait]
#[async_trait(?Send)]
impl HttpClient for ReqwestHttpClient {
async fn fetch(&self, url: &str) -> Result<Vec<u8>, Error> {
let response = self.client.get(url).send().await?;
Expand Down
20 changes: 16 additions & 4 deletions maplibre/src/platform/noweb/schedule_method.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
use std::future::Future;

use tokio::task;
use tokio_util::task::LocalPoolHandle;

use crate::{error::Error, ScheduleMethod};

/// Multi-threading with Tokio.
pub struct TokioScheduleMethod;
pub struct TokioScheduleMethod {
pool: LocalPoolHandle,
}

impl TokioScheduleMethod {
pub fn new() -> Self {
Self {}
Self {
pool: LocalPoolHandle::new(4),
}
}
}

impl ScheduleMethod for TokioScheduleMethod {
fn schedule<T>(&self, future_factory: impl FnOnce() -> T + Send + 'static) -> Result<(), Error>
where
T: Future<Output = ()> + Send + 'static,
T: Future<Output = ()> + 'static,
{
tokio::task::spawn((future_factory)());
self.pool.spawn_pinned(|| {
let unsend_data = (future_factory)();

async move { unsend_data.await }
});

Ok(())
}
}
2 changes: 1 addition & 1 deletion web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
async-trait = "0.1.56"
maplibre = { path = "../maplibre", features = ["no-thread-safe-futures"] }
maplibre = { path = "../maplibre", features = [] }
maplibre-winit = { path = "../maplibre-winit", version = "0.0.1" }
log = "0.4.17"

Expand Down