Skip to content
This repository was archived by the owner on Oct 30, 2019. It is now read-only.

add wasm32-unknown-unknown support to runtime-native #22

Merged
merged 7 commits into from
May 8, 2019
Merged
Changes from all 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
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 2 additions & 2 deletions runtime-attributes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
15 changes: 11 additions & 4 deletions runtime-native/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
70 changes: 9 additions & 61 deletions runtime-native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Pin<Box<dyn runtime_raw::TcpStream>>>> {
let romio_connect = romio::TcpStream::connect(addr);
let connect = romio_connect.map(|res| {
res.map(|romio_stream| {
Box::pin(TcpStream { romio_stream }) as Pin<Box<dyn runtime_raw::TcpStream>>
})
});
connect.boxed()
}

fn bind_tcp_listener(
&self,
addr: &SocketAddr,
) -> io::Result<Pin<Box<dyn runtime_raw::TcpListener>>> {
let romio_listener = romio::TcpListener::bind(&addr)?;
Ok(Box::pin(TcpListener { romio_listener }))
}

fn bind_udp_socket(
&self,
addr: &SocketAddr,
) -> io::Result<Pin<Box<dyn runtime_raw::UdpSocket>>> {
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;
61 changes: 61 additions & 0 deletions runtime-native/src/not_wasm32.rs
Original file line number Diff line number Diff line change
@@ -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<Pin<Box<dyn runtime_raw::TcpStream>>>> {
let romio_connect = romio::TcpStream::connect(addr);
let connect = romio_connect.map(|res| {
res.map(|romio_stream| {
Box::pin(TcpStream { romio_stream }) as Pin<Box<dyn runtime_raw::TcpStream>>
})
});
connect.boxed()
}

fn bind_tcp_listener(
&self,
addr: &SocketAddr,
) -> io::Result<Pin<Box<dyn runtime_raw::TcpListener>>> {
let romio_listener = romio::TcpListener::bind(&addr)?;
Ok(Box::pin(TcpListener { romio_listener }))
}

fn bind_udp_socket(
&self,
addr: &SocketAddr,
) -> io::Result<Pin<Box<dyn runtime_raw::UdpSocket>>> {
let romio_socket = romio::UdpSocket::bind(&addr)?;
Ok(Box::pin(UdpSocket { romio_socket }))
}
}
File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions runtime-native/src/wasm32.rs
Original file line number Diff line number Diff line change
@@ -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<Pin<Box<dyn runtime_raw::TcpStream>>>> {
panic!("Connecting TCP streams is currently not supported in wasm");
}

fn bind_tcp_listener(
&self,
_addr: &SocketAddr,
) -> io::Result<Pin<Box<dyn runtime_raw::TcpListener>>> {
panic!("Binding TCP listeners is currently not supported in wasm");
}

fn bind_udp_socket(
&self,
_addr: &SocketAddr,
) -> io::Result<Pin<Box<dyn runtime_raw::UdpSocket>>> {
panic!("Binding UDP sockets is currently not supported in wasm");
}
}
2 changes: 1 addition & 1 deletion runtime-raw/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions runtime-tokio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"