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

Commit

Permalink
add wasm32-unknown-unknown support to runtime-native (#22)
Browse files Browse the repository at this point in the history
* update versions

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>

* runtime 0.3.0-alpha.3

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>

* add wasm32-unknown-unknown support

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>

* reorder cfgs

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>

* Update runtime-native/src/wasm32.rs

Co-Authored-By: yoshuawuyts <yoshuawuyts+github@gmail.com>

* wasm-bindgen feature flag

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>

* remove unneeded combinators

Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
  • Loading branch information
yoshuawuyts authored May 8, 2019
1 parent 86aad62 commit 95414d6
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 64 deletions.
13 changes: 10 additions & 3 deletions runtime-native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.2" }
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(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");
}
}

0 comments on commit 95414d6

Please sign in to comment.