Skip to content
This repository has been 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
Show file tree
Hide file tree
Changes from 4 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"
Expand All @@ -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"
Expand All @@ -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]
Expand Down
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"
Expand All @@ -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"
Expand All @@ -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
Expand Up @@ -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(target_arch = "wasm32")]
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
mod wasm32;
#[cfg(target_arch = "wasm32")]
pub use wasm32::Native;

#[cfg(not(target_arch = "wasm32"))]
mod not_wasm32;
#[cfg(not(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 }))
}
}
48 changes: 48 additions & 0 deletions runtime-native/src/wasm32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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()
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
.compat()
.map(|_| JsValue::undefined())
.map_err(|_| JsValue::undefined());
future_to_promise(fut);
yoshuawuyts marked this conversation as resolved.
Show resolved Hide resolved
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"
Expand Down
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"
Expand All @@ -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"