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

Commit 95414d6

Browse files
authored
add wasm32-unknown-unknown support to runtime-native (#22)
* update versions Signed-off-by: Yoshua Wuyts <[email protected]> * runtime 0.3.0-alpha.3 Signed-off-by: Yoshua Wuyts <[email protected]> * add wasm32-unknown-unknown support Signed-off-by: Yoshua Wuyts <[email protected]> * reorder cfgs Signed-off-by: Yoshua Wuyts <[email protected]> * Update runtime-native/src/wasm32.rs Co-Authored-By: yoshuawuyts <[email protected]> * wasm-bindgen feature flag Signed-off-by: Yoshua Wuyts <[email protected]> * remove unneeded combinators Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent 86aad62 commit 95414d6

File tree

6 files changed

+124
-64
lines changed

6 files changed

+124
-64
lines changed

runtime-native/Cargo.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ categories = ["asynchronous", "network-programming", "filesystem", "concurrency"
1313
edition = "2018"
1414

1515
[dependencies]
16+
futures-preview = { version = "0.3.0-alpha.15", features = ["compat"] }
17+
runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.2" }
18+
19+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
1620
async-datagram = "2.2.0"
17-
futures-preview = "0.3.0-alpha.15"
21+
juliex = "0.3.0-alpha.5"
1822
lazy_static = "1.3.0"
1923
romio = "0.3.0-alpha.6"
20-
runtime-raw = { path = "../runtime-raw", version = "0.3.0-alpha.2" }
21-
juliex = "0.3.0-alpha.5"
24+
25+
[target.'cfg(target_arch = "wasm32")'.dependencies]
26+
futures01 = { package = "futures", version = "0.1" }
27+
wasm-bindgen = "0.2.43"
28+
wasm-bindgen-futures = "0.3.20"

runtime-native/src/lib.rs

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,12 @@
1010
rust_2018_idioms
1111
)]
1212

13-
use futures::prelude::*;
14-
use futures::{future::BoxFuture, task::SpawnError};
15-
use lazy_static::lazy_static;
16-
17-
use std::io;
18-
use std::net::SocketAddr;
19-
use std::pin::Pin;
20-
21-
mod tcp;
22-
mod udp;
23-
24-
use tcp::{TcpListener, TcpStream};
25-
use udp::UdpSocket;
26-
27-
lazy_static! {
28-
static ref JULIEX_THREADPOOL: juliex::ThreadPool = {
29-
juliex::ThreadPool::with_setup(|| {
30-
runtime_raw::set_runtime(&Native);
31-
})
32-
};
33-
}
34-
35-
/// The Native runtime.
36-
#[derive(Debug)]
37-
pub struct Native;
38-
39-
impl runtime_raw::Runtime for Native {
40-
fn spawn_boxed(&self, fut: BoxFuture<'static, ()>) -> Result<(), SpawnError> {
41-
JULIEX_THREADPOOL.spawn_boxed(fut.into());
42-
Ok(())
43-
}
44-
45-
fn connect_tcp_stream(
46-
&self,
47-
addr: &SocketAddr,
48-
) -> BoxFuture<'static, io::Result<Pin<Box<dyn runtime_raw::TcpStream>>>> {
49-
let romio_connect = romio::TcpStream::connect(addr);
50-
let connect = romio_connect.map(|res| {
51-
res.map(|romio_stream| {
52-
Box::pin(TcpStream { romio_stream }) as Pin<Box<dyn runtime_raw::TcpStream>>
53-
})
54-
});
55-
connect.boxed()
56-
}
57-
58-
fn bind_tcp_listener(
59-
&self,
60-
addr: &SocketAddr,
61-
) -> io::Result<Pin<Box<dyn runtime_raw::TcpListener>>> {
62-
let romio_listener = romio::TcpListener::bind(&addr)?;
63-
Ok(Box::pin(TcpListener { romio_listener }))
64-
}
65-
66-
fn bind_udp_socket(
67-
&self,
68-
addr: &SocketAddr,
69-
) -> io::Result<Pin<Box<dyn runtime_raw::UdpSocket>>> {
70-
let romio_socket = romio::UdpSocket::bind(&addr)?;
71-
Ok(Box::pin(UdpSocket { romio_socket }))
72-
}
73-
}
13+
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
14+
mod wasm32;
15+
#[cfg(all(feature = "wasm-bindgen", target_arch = "wasm32"))]
16+
pub use wasm32::Native;
17+
18+
#[cfg(not(all(feature = "wasm-bindgen", target_arch = "wasm32")))]
19+
mod not_wasm32;
20+
#[cfg(not(all(feature = "wasm-bindgen", target_arch = "wasm32")))]
21+
pub use not_wasm32::Native;

runtime-native/src/not_wasm32.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use futures::prelude::*;
2+
use futures::{future::BoxFuture, task::SpawnError};
3+
use lazy_static::lazy_static;
4+
5+
use std::io;
6+
use std::net::SocketAddr;
7+
use std::pin::Pin;
8+
9+
mod tcp;
10+
mod udp;
11+
12+
use tcp::{TcpListener, TcpStream};
13+
use udp::UdpSocket;
14+
15+
lazy_static! {
16+
static ref JULIEX_THREADPOOL: juliex::ThreadPool = {
17+
juliex::ThreadPool::with_setup(|| {
18+
runtime_raw::set_runtime(&Native);
19+
})
20+
};
21+
}
22+
23+
/// The Native runtime.
24+
#[derive(Debug)]
25+
pub struct Native;
26+
27+
impl runtime_raw::Runtime for Native {
28+
fn spawn_boxed(&self, fut: BoxFuture<'static, ()>) -> Result<(), SpawnError> {
29+
JULIEX_THREADPOOL.spawn_boxed(fut.into());
30+
Ok(())
31+
}
32+
33+
fn connect_tcp_stream(
34+
&self,
35+
addr: &SocketAddr,
36+
) -> BoxFuture<'static, io::Result<Pin<Box<dyn runtime_raw::TcpStream>>>> {
37+
let romio_connect = romio::TcpStream::connect(addr);
38+
let connect = romio_connect.map(|res| {
39+
res.map(|romio_stream| {
40+
Box::pin(TcpStream { romio_stream }) as Pin<Box<dyn runtime_raw::TcpStream>>
41+
})
42+
});
43+
connect.boxed()
44+
}
45+
46+
fn bind_tcp_listener(
47+
&self,
48+
addr: &SocketAddr,
49+
) -> io::Result<Pin<Box<dyn runtime_raw::TcpListener>>> {
50+
let romio_listener = romio::TcpListener::bind(&addr)?;
51+
Ok(Box::pin(TcpListener { romio_listener }))
52+
}
53+
54+
fn bind_udp_socket(
55+
&self,
56+
addr: &SocketAddr,
57+
) -> io::Result<Pin<Box<dyn runtime_raw::UdpSocket>>> {
58+
let romio_socket = romio::UdpSocket::bind(&addr)?;
59+
Ok(Box::pin(UdpSocket { romio_socket }))
60+
}
61+
}
File renamed without changes.
File renamed without changes.

runtime-native/src/wasm32.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use futures::prelude::*;
2+
use futures::{future::BoxFuture, task::SpawnError};
3+
// use futures::compat::*;
4+
5+
use std::io;
6+
use std::net::SocketAddr;
7+
use std::pin::Pin;
8+
9+
use wasm_bindgen::prelude::*;
10+
use wasm_bindgen_futures::future_to_promise;
11+
12+
/// The Native runtime.
13+
#[derive(Debug)]
14+
pub struct Native;
15+
16+
impl runtime_raw::Runtime for Native {
17+
fn spawn_boxed(&self, fut: BoxFuture<'static, ()>) -> Result<(), SpawnError> {
18+
use futures01::future::Future;
19+
let fut = fut.unit_error().compat();
20+
wasm_bindgen_futures::spawn_local(fut);
21+
Ok(())
22+
}
23+
24+
fn connect_tcp_stream(
25+
&self,
26+
_addr: &SocketAddr,
27+
) -> BoxFuture<'static, io::Result<Pin<Box<dyn runtime_raw::TcpStream>>>> {
28+
panic!("Connecting TCP streams is currently not supported in wasm");
29+
}
30+
31+
fn bind_tcp_listener(
32+
&self,
33+
_addr: &SocketAddr,
34+
) -> io::Result<Pin<Box<dyn runtime_raw::TcpListener>>> {
35+
panic!("Binding TCP listeners is currently not supported in wasm");
36+
}
37+
38+
fn bind_udp_socket(
39+
&self,
40+
_addr: &SocketAddr,
41+
) -> io::Result<Pin<Box<dyn runtime_raw::UdpSocket>>> {
42+
panic!("Binding UDP sockets is currently not supported in wasm");
43+
}
44+
}

0 commit comments

Comments
 (0)