Skip to content

Commit e4df140

Browse files
feat: add basic wasm support
1 parent 2cd2ba3 commit e4df140

37 files changed

+301
-64
lines changed

Diff for: .github/workflows/ci.yml

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ jobs:
5858
with:
5959
command: check
6060
args: --features unstable --all --bins --examples --tests
61+
62+
- name: check wasm
63+
uses: actions-rs/cargo@v1
64+
with:
65+
command: check
66+
target: wasm32-unknown-unknown
67+
override: true
68+
args: --features unstable --all --bins --tests
6169

6270
- name: check bench
6371
uses: actions-rs/cargo@v1

Diff for: Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,25 @@ once_cell = { version = "1.3.1", optional = true }
6363
pin-project-lite = { version = "0.1.4", optional = true }
6464
pin-utils = { version = "0.1.0-alpha.4", optional = true }
6565
slab = { version = "0.4.2", optional = true }
66+
67+
[target.'cfg(not(target_os = "unknown"))'.dependencies]
6668
smol = { path = "../smol", optional = true }
6769

70+
[target.'cfg(target_arch = "wasm32")'.dependencies]
71+
wasm-timer = "0.2.4"
72+
wasm-bindgen-futures = "0.4.10"
73+
futures-channel = "0.3.4"
74+
75+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
76+
wasm-bindgen-test = "0.3.10"
77+
6878
[dev-dependencies]
6979
femme = "1.3.0"
7080
rand = "0.7.3"
7181
surf = "1.0.3"
7282
tempdir = "0.3.7"
7383
futures = "0.3.4"
84+
rand_xorshift = "0.2.0"
7485

7586
[[test]]
7687
name = "stream"

Diff for: src/future/future/delay.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::pin::Pin;
33
use std::time::Duration;
44

55
use pin_project_lite::pin_project;
6-
use smol::Timer;
76

87
use crate::task::{Context, Poll};
8+
use crate::utils::Timer;
99

1010
pin_project! {
1111
#[doc(hidden)]

Diff for: src/future/timeout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use std::pin::Pin;
55
use std::time::Duration;
66

77
use pin_project_lite::pin_project;
8-
use smol::Timer;
98

109
use crate::task::{Context, Poll};
10+
use crate::utils::Timer;
1111

1212
/// Awaits a future or times out after a duration of time.
1313
///

Diff for: src/io/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,33 @@ cfg_std! {
307307
cfg_default! {
308308
// For use in the print macros.
309309
#[doc(hidden)]
310+
#[cfg(not(target_os = "unknown"))]
310311
pub use stdio::{_eprint, _print};
311312

313+
#[cfg(not(target_os = "unknown"))]
312314
pub use stderr::{stderr, Stderr};
315+
#[cfg(not(target_os = "unknown"))]
313316
pub use stdin::{stdin, Stdin};
317+
#[cfg(not(target_os = "unknown"))]
314318
pub use stdout::{stdout, Stdout};
315319
pub use timeout::timeout;
316320

317321
mod timeout;
322+
#[cfg(not(target_os = "unknown"))]
318323
mod stderr;
324+
#[cfg(not(target_os = "unknown"))]
319325
mod stdin;
326+
#[cfg(not(target_os = "unknown"))]
320327
mod stdio;
328+
#[cfg(not(target_os = "unknown"))]
321329
mod stdout;
322330
}
323331

324332
cfg_unstable_default! {
333+
#[cfg(not(target_os = "unknown"))]
325334
pub use stderr::StderrLock;
335+
#[cfg(not(target_os = "unknown"))]
326336
pub use stdin::StdinLock;
337+
#[cfg(not(target_os = "unknown"))]
327338
pub use stdout::StdoutLock;
328339
}

Diff for: src/io/read/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use std::mem;
1717

1818
use crate::io::IoSliceMut;
1919

20-
pub use take::Take;
2120
pub use bytes::Bytes;
2221
pub use chain::Chain;
22+
pub use take::Take;
2323

2424
extension_trait! {
2525
use std::pin::Pin;
@@ -483,7 +483,7 @@ mod tests {
483483
use crate::prelude::*;
484484

485485
#[test]
486-
fn test_read_by_ref() -> io::Result<()> {
486+
fn test_read_by_ref() {
487487
crate::task::block_on(async {
488488
let mut f = io::Cursor::new(vec![0u8, 1, 2, 3, 4, 5, 6, 7, 8]);
489489
let mut buffer = Vec::new();
@@ -493,14 +493,13 @@ mod tests {
493493
let reference = f.by_ref();
494494

495495
// read at most 5 bytes
496-
assert_eq!(reference.take(5).read_to_end(&mut buffer).await?, 5);
496+
assert_eq!(reference.take(5).read_to_end(&mut buffer).await.unwrap(), 5);
497497
assert_eq!(&buffer, &[0, 1, 2, 3, 4])
498498
} // drop our &mut reference so we can use f again
499499

500500
// original file still usable, read the rest
501-
assert_eq!(f.read_to_end(&mut other_buffer).await?, 4);
501+
assert_eq!(f.read_to_end(&mut other_buffer).await.unwrap(), 4);
502502
assert_eq!(&other_buffer, &[5, 6, 7, 8]);
503-
Ok(())
504-
})
503+
});
505504
}
506505
}

Diff for: src/io/read/take.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl<T: BufRead> BufRead for Take<T> {
218218
}
219219
}
220220

221-
#[cfg(test)]
221+
#[cfg(all(test, not(target_os = "unknown")))]
222222
mod tests {
223223
use crate::io;
224224
use crate::prelude::*;

Diff for: src/io/timeout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::task::{Context, Poll};
44
use std::time::Duration;
55

66
use pin_project_lite::pin_project;
7-
use smol::Timer;
87

98
use crate::io;
9+
use crate::utils::Timer;
1010

1111
/// Awaits an I/O future or times out after a duration of time.
1212
///

Diff for: src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,17 @@ cfg_std! {
267267
}
268268

269269
cfg_default! {
270+
#[cfg(not(target_os = "unknown"))]
270271
pub mod fs;
271272
pub mod path;
272273
pub mod net;
274+
#[cfg(not(target_os = "unknown"))]
273275
pub(crate) mod rt;
274276
}
275277

276278
cfg_unstable! {
277279
pub mod pin;
280+
#[cfg(not(target_os = "unknown"))]
278281
pub mod process;
279282

280283
mod unit;

Diff for: src/net/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ pub use std::net::Shutdown;
6161
pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
6262
pub use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
6363

64+
#[cfg(not(target_os = "unknown"))]
6465
pub use addr::ToSocketAddrs;
66+
#[cfg(not(target_os = "unknown"))]
6567
pub use tcp::{Incoming, TcpListener, TcpStream};
68+
#[cfg(not(target_os = "unknown"))]
6669
pub use udp::UdpSocket;
6770

71+
#[cfg(not(target_os = "unknown"))]
6872
mod addr;
73+
#[cfg(not(target_os = "unknown"))]
6974
mod tcp;
75+
#[cfg(not(target_os = "unknown"))]
7076
mod udp;

Diff for: src/path/path.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::ffi::{OsStr, OsString};
44
use std::rc::Rc;
55
use std::sync::Arc;
66

7-
use crate::fs;
8-
use crate::io;
97
use crate::path::{Ancestors, Components, Display, Iter, PathBuf, StripPrefixError};
8+
#[cfg(not(target_os = "unknown"))]
9+
use crate::{fs, io};
1010

1111
/// A slice of a path.
1212
///
@@ -584,6 +584,7 @@ impl Path {
584584
/// #
585585
/// # Ok(()) }) }
586586
/// ```
587+
#[cfg(not(target_os = "unknown"))]
587588
pub async fn metadata(&self) -> io::Result<fs::Metadata> {
588589
fs::metadata(self).await
589590
}
@@ -607,6 +608,7 @@ impl Path {
607608
/// #
608609
/// # Ok(()) }) }
609610
/// ```
611+
#[cfg(not(target_os = "unknown"))]
610612
pub async fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
611613
fs::symlink_metadata(self).await
612614
}
@@ -632,6 +634,7 @@ impl Path {
632634
/// #
633635
/// # Ok(()) }) }
634636
/// ```
637+
#[cfg(not(target_os = "unknown"))]
635638
pub async fn canonicalize(&self) -> io::Result<PathBuf> {
636639
fs::canonicalize(self).await
637640
}
@@ -654,6 +657,7 @@ impl Path {
654657
/// #
655658
/// # Ok(()) }) }
656659
/// ```
660+
#[cfg(not(target_os = "unknown"))]
657661
pub async fn read_link(&self) -> io::Result<PathBuf> {
658662
fs::read_link(self).await
659663
}
@@ -688,6 +692,7 @@ impl Path {
688692
/// #
689693
/// # Ok(()) }) }
690694
/// ```
695+
#[cfg(not(target_os = "unknown"))]
691696
pub async fn read_dir(&self) -> io::Result<fs::ReadDir> {
692697
fs::read_dir(self).await
693698
}
@@ -717,6 +722,7 @@ impl Path {
717722
/// check errors, call [fs::metadata].
718723
///
719724
/// [fs::metadata]: ../fs/fn.metadata.html
725+
#[cfg(not(target_os = "unknown"))]
720726
pub async fn exists(&self) -> bool {
721727
fs::metadata(self).await.is_ok()
722728
}
@@ -749,6 +755,7 @@ impl Path {
749755
///
750756
/// [fs::metadata]: ../fs/fn.metadata.html
751757
/// [fs::Metadata::is_file]: ../fs/struct.Metadata.html#method.is_file
758+
#[cfg(not(target_os = "unknown"))]
752759
pub async fn is_file(&self) -> bool {
753760
fs::metadata(self)
754761
.await
@@ -785,6 +792,7 @@ impl Path {
785792
///
786793
/// [fs::metadata]: ../fs/fn.metadata.html
787794
/// [fs::Metadata::is_dir]: ../fs/struct.Metadata.html#method.is_dir
795+
#[cfg(not(target_os = "unknown"))]
788796
pub async fn is_dir(&self) -> bool {
789797
fs::metadata(self)
790798
.await

Diff for: src/stream/interval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::task::{Context, Poll};
44
use std::time::Duration;
55

66
use crate::stream::Stream;
7-
use smol::Timer;
7+
use crate::utils::Timer;
88

99
/// Creates a new stream that yields at a set interval.
1010
///

Diff for: src/stream/stream/delay.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use core::pin::Pin;
33
use core::time::Duration;
44

55
use pin_project_lite::pin_project;
6-
use smol::Timer;
76

87
use crate::stream::Stream;
98
use crate::task::{Context, Poll};
9+
use crate::utils::Timer;
1010

1111
pin_project! {
1212
#[doc(hidden)]

Diff for: src/stream/stream/throttle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use std::pin::Pin;
33
use std::time::Duration;
44

55
use pin_project_lite::pin_project;
6-
use smol::Timer;
76

87
use crate::stream::Stream;
98
use crate::task::{Context, Poll};
9+
use crate::utils::Timer;
1010

1111
pin_project! {
1212
/// A stream that only yields one element once every `duration`.

Diff for: src/stream/stream/timeout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use std::pin::Pin;
55
use std::time::Duration;
66

77
use pin_project_lite::pin_project;
8-
use smol::Timer;
98

109
use crate::stream::Stream;
1110
use crate::task::{Context, Poll};
11+
use crate::utils::Timer;
1212

1313
pin_project! {
1414
/// A stream with timeout time set

Diff for: src/sync/barrier.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl BarrierWaitResult {
202202
}
203203
}
204204

205-
#[cfg(test)]
205+
#[cfg(all(test, not(target_os = "unknown")))]
206206
mod test {
207207
use futures::channel::mpsc::unbounded;
208208
use futures::sink::SinkExt;

Diff for: src/task/block_on.rs

+11
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,20 @@ use crate::task::Builder;
2525
/// })
2626
/// }
2727
/// ```
28+
#[cfg(not(target_os = "unknown"))]
2829
pub fn block_on<F, T>(future: F) -> T
2930
where
3031
F: Future<Output = T>,
3132
{
3233
Builder::new().blocking(future)
3334
}
35+
36+
/// Spawns a task and waits for it to finish.
37+
#[cfg(target_os = "unknown")]
38+
pub fn block_on<F, T>(future: F)
39+
where
40+
F: Future<Output = T> + 'static,
41+
T: 'static,
42+
{
43+
Builder::new().local(future).unwrap();
44+
}

0 commit comments

Comments
 (0)