Skip to content

Commit c3450a9

Browse files
committed
feat: Make tokio an optional dependency
Closes #843
1 parent bd8ad34 commit c3450a9

File tree

6 files changed

+48
-35
lines changed

6 files changed

+48
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ quick-error = "1.0.0"
3939
collect-mac = "0.1.0"
4040
either = "1.0.0"
4141
itertools = "0.9"
42-
futures = { version = "0.3.1", features = ["thread-pool"] }
42+
futures = { version = "0.3.1", default-features = false }
4343
codespan = "0.9"
4444
codespan-reporting = "0.9"
45-
pin-project = "0.4"
45+
pin-project-lite = { version = "0.1", optional = true }
4646
salsa = { version = "0.14.0", package = "gluon-salsa" }
4747

4848
serde = { version = "1.0.0", optional = true }
4949
serde_state = { version = "0.4", optional = true }
5050
serde_derive_state = { version = "0.4.7", optional = true }
5151

52-
tokio = { version = "0.2", features = ["stream", "sync", "rt-core"] }
52+
tokio = { version = "0.2", features = ["stream", "sync", "rt-core"], optional = true }
5353

5454
# Binding crates
5555
regex = { version = "1", optional = true }
@@ -100,10 +100,11 @@ gluon_completion = { path = "completion", version = "0.15.1" } # GLUON
100100
gluon_codegen = { path = "codegen", version = "0.15.1" } # GLUON
101101

102102
[features]
103-
default = ["regex", "random"]
103+
default = ["async", "regex", "random"]
104+
async = ["tokio"]
104105
random = ["rand", "rand_xorshift"]
105106
serialization = ["serde", "serde_state", "serde_derive_state", "gluon_vm/serialization"]
106-
web = ["hyper", "http", "tower-service", "native-tls", "tokio/net", "tokio-tls"]
107+
web = ["async", "hyper", "http", "tower-service", "native-tls", "tokio/net", "tokio-tls", "pin-project-lite"]
107108

108109
docs_rs = ["serialization"]
109110

src/compiler_pipeline.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use std::{
1717
use either::Either;
1818
use salsa::ParallelDatabase;
1919

20+
#[cfg(feature = "serde")]
21+
use crate::ThreadExt;
22+
2023
use crate::{
2124
base::{
2225
ast::{self, OwnedExpr, RootExpr, SpannedExpr, Typed},
@@ -35,7 +38,7 @@ use crate::{
3538
macros::MacroExpander,
3639
thread::{RootedThread, RootedValue, Thread, ThreadInternal, VmRoot},
3740
},
38-
Error, ModuleCompiler, Result, ThreadExt,
41+
Error, ModuleCompiler, Result,
3942
};
4043

4144
pub type BoxFuture<'vm, T, E> =

src/import.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use {
1616
futures::{
1717
future::{self},
1818
prelude::*,
19-
task::SpawnExt,
2019
},
2120
itertools::Itertools,
2221
salsa::{debug::DebugQueryTable, Database},
@@ -573,7 +572,10 @@ where
573572

574573
let span = args[0].span;
575574

575+
#[cfg(feature = "tokio")]
576576
if let Some(spawn) = macros.spawn {
577+
use futures::task::SpawnExt;
578+
577579
let (tx, rx) = tokio::sync::oneshot::channel();
578580
spawn
579581
.spawn(Box::pin(async move {
@@ -585,28 +587,28 @@ where
585587
let _ = tx.send(result);
586588
}))
587589
.unwrap();
588-
Box::pin(async move {
590+
return Box::pin(async move {
589591
Ok(From::from(move || {
590592
rx.map(move |r| {
591593
r.unwrap_or_else(|err| Err(MacroError::new(Error::String(err.to_string()))))
592594
.map(move |id| pos::spanned(span, Expr::Ident(id)).into())
593595
})
594596
.boxed()
595597
}))
596-
})
597-
} else {
598-
Box::pin(async move {
599-
Ok(From::from(move || {
600-
async move {
601-
db.import(modulename)
602-
.await
603-
.map_err(|err| MacroError::message(err.to_string()))
604-
.map(move |id| pos::spanned(span, Expr::Ident(id)))
605-
}
606-
.boxed()
607-
}))
608-
})
598+
});
609599
}
600+
601+
Box::pin(async move {
602+
Ok(From::from(move || {
603+
async move {
604+
db.import(modulename)
605+
.await
606+
.map_err(|err| MacroError::message(err.to_string()))
607+
.map(move |id| pos::spanned(span, Expr::Ident(id)))
608+
}
609+
.boxed()
610+
}))
611+
})
610612
}
611613
}
612614

src/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -888,18 +888,25 @@ impl VmBuilder {
888888
}
889889

890890
pub async fn build_async(self) -> RootedThread {
891-
struct TokioSpawn;
892-
impl futures::task::Spawn for TokioSpawn {
893-
fn spawn_obj(
894-
&self,
895-
future: futures::task::FutureObj<'static, ()>,
896-
) -> StdResult<(), futures::task::SpawnError> {
897-
tokio::spawn(future);
898-
Ok(())
891+
#[allow(unused_mut, unused_assignments)]
892+
let mut spawner = None;
893+
894+
#[cfg(feature = "tokio")]
895+
{
896+
struct TokioSpawn;
897+
impl futures::task::Spawn for TokioSpawn {
898+
fn spawn_obj(
899+
&self,
900+
future: futures::task::FutureObj<'static, ()>,
901+
) -> StdResult<(), futures::task::SpawnError> {
902+
tokio::spawn(future);
903+
Ok(())
904+
}
899905
}
906+
spawner = Some(Box::new(TokioSpawn) as Box<dyn futures::task::Spawn + Send + Sync>);
900907
}
901908

902-
self.build_inner(Some(Box::new(TokioSpawn))).await
909+
self.build_inner(spawner).await
903910
}
904911

905912
async fn build_inner(

src/std_lib/http.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use {
1818
StatusCode,
1919
},
2020
hyper::{body::Bytes, Server},
21-
pin_project::pin_project,
21+
pin_project_lite::pin_project,
2222
};
2323

2424
use crate::base::types::{ArcType, Type};
@@ -360,8 +360,9 @@ async fn listen_(
360360
})
361361
});
362362

363-
#[pin_project]
364-
struct Acceptor<S>(#[pin] S);
363+
pin_project! {
364+
struct Acceptor<S>(#[pin] S);
365+
}
365366
impl<S, T, E> hyper::server::accept::Accept for Acceptor<S>
366367
where
367368
S: Stream<Item = Result<T, E>>,

0 commit comments

Comments
 (0)