diff --git a/Cargo.toml b/Cargo.toml index 44f36e11..d3008b43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "runtime" description = "Empowering everyone to build asynchronous software." -version = "0.3.0-alpha.2" +version = "0.3.0-alpha.3" license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/rustasync/runtime" @@ -14,9 +14,9 @@ edition = "2018" [dependencies] futures-preview = "0.3.0-alpha.15" -runtime-attributes = { path = "runtime-attributes", version = "0.3.0-alpha.2" } -runtime-raw = { path = "runtime-raw", version = "0.3.0-alpha.1" } -runtime-native = { path = "runtime-native", version = "0.3.0-alpha.1" } +runtime-attributes = { path = "runtime-attributes", version = "0.3.0-alpha.3" } +runtime-raw = { path = "runtime-raw", version = "0.3.0-alpha.2" } +runtime-native = { path = "runtime-native", version = "0.3.0-alpha.2" } [dev-dependencies] failure = "0.1.5" @@ -25,7 +25,7 @@ futures-preview = { version = "0.3.0-alpha.15", features = ["nightly", "async-aw juliex = "0.3.0-alpha.5" mio = "0.6.16" rand = "0.6.5" -runtime-tokio = { path = "runtime-tokio", version = "0.3.0-alpha.1" } +runtime-tokio = { path = "runtime-tokio", version = "0.3.0-alpha.3" } tokio = "0.1.19" [profile.bench] diff --git a/runtime-attributes/Cargo.toml b/runtime-attributes/Cargo.toml index 4f74bd03..4e8228fd 100644 --- a/runtime-attributes/Cargo.toml +++ b/runtime-attributes/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "runtime-attributes" description = "Proc Macro attributes for the Runtime crate." -version = "0.3.0-alpha.2" +version = "0.3.0-alpha.3" license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/rustasync/runtime" @@ -21,4 +21,4 @@ proc-macro2 = { version = "0.4.29", features = ["nightly"] } quote = "0.6.12" [dev-dependencies] -runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.1" } +runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.2" } diff --git a/runtime-native/Cargo.toml b/runtime-native/Cargo.toml index a57e25dc..36532c30 100644 --- a/runtime-native/Cargo.toml +++ b/runtime-native/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "runtime-native" description = "A cross-platform asynchronous runtime" -version = "0.3.0-alpha.1" +version = "0.3.0-alpha.2" license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/rustasync/runtime" @@ -13,9 +13,16 @@ categories = ["asynchronous", "network-programming", "filesystem", "concurrency" edition = "2018" [dependencies] +futures-preview = { version = "0.3.0-alpha.15", features = ["compat"] } +runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.2" } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] async-datagram = "2.2.0" -futures-preview = "0.3.0-alpha.15" +juliex = "0.3.0-alpha.5" lazy_static = "1.3.0" romio = "0.3.0-alpha.6" -runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.1" } -juliex = "0.3.0-alpha.5" + +[target.'cfg(target_arch = "wasm32")'.dependencies] +futures01 = { package = "futures", version = "0.1" } +wasm-bindgen = "0.2.43" +wasm-bindgen-futures = "0.3.20" diff --git a/runtime-native/src/lib.rs b/runtime-native/src/lib.rs index a589676b..f4250298 100644 --- a/runtime-native/src/lib.rs +++ b/runtime-native/src/lib.rs @@ -10,64 +10,12 @@ rust_2018_idioms )] -use futures::prelude::*; -use futures::{future::BoxFuture, task::SpawnError}; -use lazy_static::lazy_static; - -use std::io; -use std::net::SocketAddr; -use std::pin::Pin; - -mod tcp; -mod udp; - -use tcp::{TcpListener, TcpStream}; -use udp::UdpSocket; - -lazy_static! { - static ref JULIEX_THREADPOOL: juliex::ThreadPool = { - juliex::ThreadPool::with_setup(|| { - runtime_raw::set_runtime(&Native); - }) - }; -} - -/// The Native runtime. -#[derive(Debug)] -pub struct Native; - -impl runtime_raw::Runtime for Native { - fn spawn_boxed(&self, fut: BoxFuture<'static, ()>) -> Result<(), SpawnError> { - JULIEX_THREADPOOL.spawn_boxed(fut.into()); - Ok(()) - } - - fn connect_tcp_stream( - &self, - addr: &SocketAddr, - ) -> BoxFuture<'static, io::Result>>> { - let romio_connect = romio::TcpStream::connect(addr); - let connect = romio_connect.map(|res| { - res.map(|romio_stream| { - Box::pin(TcpStream { romio_stream }) as Pin> - }) - }); - connect.boxed() - } - - fn bind_tcp_listener( - &self, - addr: &SocketAddr, - ) -> io::Result>> { - let romio_listener = romio::TcpListener::bind(&addr)?; - Ok(Box::pin(TcpListener { romio_listener })) - } - - fn bind_udp_socket( - &self, - addr: &SocketAddr, - ) -> io::Result>> { - let romio_socket = romio::UdpSocket::bind(&addr)?; - Ok(Box::pin(UdpSocket { romio_socket })) - } -} +#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))] +mod wasm32; +#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))] +pub use wasm32::Native; + +#[cfg(not(all(feature = "wasm-bindgen", target_arch = "wasm32")))] +mod not_wasm32; +#[cfg(not(all(feature = "wasm-bindgen", target_arch = "wasm32")))] +pub use not_wasm32::Native; diff --git a/runtime-native/src/not_wasm32.rs b/runtime-native/src/not_wasm32.rs new file mode 100644 index 00000000..482f4219 --- /dev/null +++ b/runtime-native/src/not_wasm32.rs @@ -0,0 +1,61 @@ +use futures::prelude::*; +use futures::{future::BoxFuture, task::SpawnError}; +use lazy_static::lazy_static; + +use std::io; +use std::net::SocketAddr; +use std::pin::Pin; + +mod tcp; +mod udp; + +use tcp::{TcpListener, TcpStream}; +use udp::UdpSocket; + +lazy_static! { + static ref JULIEX_THREADPOOL: juliex::ThreadPool = { + juliex::ThreadPool::with_setup(|| { + runtime_raw::set_runtime(&Native); + }) + }; +} + +/// The Native runtime. +#[derive(Debug)] +pub struct Native; + +impl runtime_raw::Runtime for Native { + fn spawn_boxed(&self, fut: BoxFuture<'static, ()>) -> Result<(), SpawnError> { + JULIEX_THREADPOOL.spawn_boxed(fut.into()); + Ok(()) + } + + fn connect_tcp_stream( + &self, + addr: &SocketAddr, + ) -> BoxFuture<'static, io::Result>>> { + let romio_connect = romio::TcpStream::connect(addr); + let connect = romio_connect.map(|res| { + res.map(|romio_stream| { + Box::pin(TcpStream { romio_stream }) as Pin> + }) + }); + connect.boxed() + } + + fn bind_tcp_listener( + &self, + addr: &SocketAddr, + ) -> io::Result>> { + let romio_listener = romio::TcpListener::bind(&addr)?; + Ok(Box::pin(TcpListener { romio_listener })) + } + + fn bind_udp_socket( + &self, + addr: &SocketAddr, + ) -> io::Result>> { + let romio_socket = romio::UdpSocket::bind(&addr)?; + Ok(Box::pin(UdpSocket { romio_socket })) + } +} diff --git a/runtime-native/src/tcp.rs b/runtime-native/src/not_wasm32/tcp.rs similarity index 100% rename from runtime-native/src/tcp.rs rename to runtime-native/src/not_wasm32/tcp.rs diff --git a/runtime-native/src/udp.rs b/runtime-native/src/not_wasm32/udp.rs similarity index 100% rename from runtime-native/src/udp.rs rename to runtime-native/src/not_wasm32/udp.rs diff --git a/runtime-native/src/wasm32.rs b/runtime-native/src/wasm32.rs new file mode 100644 index 00000000..907dba95 --- /dev/null +++ b/runtime-native/src/wasm32.rs @@ -0,0 +1,44 @@ +use futures::prelude::*; +use futures::{future::BoxFuture, task::SpawnError}; +// use futures::compat::*; + +use std::io; +use std::net::SocketAddr; +use std::pin::Pin; + +use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::future_to_promise; + +/// The Native runtime. +#[derive(Debug)] +pub struct Native; + +impl runtime_raw::Runtime for Native { + fn spawn_boxed(&self, fut: BoxFuture<'static, ()>) -> Result<(), SpawnError> { + use futures01::future::Future; + let fut = fut.unit_error().compat(); + wasm_bindgen_futures::spawn_local(fut); + Ok(()) + } + + fn connect_tcp_stream( + &self, + _addr: &SocketAddr, + ) -> BoxFuture<'static, io::Result>>> { + panic!("Connecting TCP streams is currently not supported in wasm"); + } + + fn bind_tcp_listener( + &self, + _addr: &SocketAddr, + ) -> io::Result>> { + panic!("Binding TCP listeners is currently not supported in wasm"); + } + + fn bind_udp_socket( + &self, + _addr: &SocketAddr, + ) -> io::Result>> { + panic!("Binding UDP sockets is currently not supported in wasm"); + } +} diff --git a/runtime-raw/Cargo.toml b/runtime-raw/Cargo.toml index cbe437bb..bb918e8d 100644 --- a/runtime-raw/Cargo.toml +++ b/runtime-raw/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "runtime-raw" description = "Traits to implement custom Runtimes." -version = "0.3.0-alpha.1" +version = "0.3.0-alpha.2" license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/rustasync/runtime" diff --git a/runtime-tokio/Cargo.toml b/runtime-tokio/Cargo.toml index bbf813bd..52662145 100644 --- a/runtime-tokio/Cargo.toml +++ b/runtime-tokio/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "runtime-tokio" description = "A Tokio-based asynchronous runtime" -version = "0.3.0-alpha.2" +version = "0.3.0-alpha.3" license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/rustasync/runtime" @@ -17,5 +17,5 @@ futures-preview = { version = "0.3.0-alpha.15", features = ["compat", "io-compat futures01 = { package = "futures", version = "0.1" } lazy_static = "1.3.0" mio = "0.6.16" -runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.1" } +runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.2" } tokio = "0.1.19"