Skip to content

Commit

Permalink
feat: add basic wasm support
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 2, 2020
1 parent 2cd2ba3 commit e4df140
Show file tree
Hide file tree
Showing 37 changed files with 301 additions and 64 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ jobs:
with:
command: check
args: --features unstable --all --bins --examples --tests

- name: check wasm
uses: actions-rs/cargo@v1
with:
command: check
target: wasm32-unknown-unknown
override: true
args: --features unstable --all --bins --tests

- name: check bench
uses: actions-rs/cargo@v1
Expand Down
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,25 @@ once_cell = { version = "1.3.1", optional = true }
pin-project-lite = { version = "0.1.4", optional = true }
pin-utils = { version = "0.1.0-alpha.4", optional = true }
slab = { version = "0.4.2", optional = true }

[target.'cfg(not(target_os = "unknown"))'.dependencies]
smol = { path = "../smol", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-timer = "0.2.4"
wasm-bindgen-futures = "0.4.10"
futures-channel = "0.3.4"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.10"

[dev-dependencies]
femme = "1.3.0"
rand = "0.7.3"
surf = "1.0.3"
tempdir = "0.3.7"
futures = "0.3.4"
rand_xorshift = "0.2.0"

[[test]]
name = "stream"
Expand Down
2 changes: 1 addition & 1 deletion src/future/future/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::pin::Pin;
use std::time::Duration;

use pin_project_lite::pin_project;
use smol::Timer;

use crate::task::{Context, Poll};
use crate::utils::Timer;

pin_project! {
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion src/future/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::pin::Pin;
use std::time::Duration;

use pin_project_lite::pin_project;
use smol::Timer;

use crate::task::{Context, Poll};
use crate::utils::Timer;

/// Awaits a future or times out after a duration of time.
///
Expand Down
11 changes: 11 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,33 @@ cfg_std! {
cfg_default! {
// For use in the print macros.
#[doc(hidden)]
#[cfg(not(target_os = "unknown"))]
pub use stdio::{_eprint, _print};

#[cfg(not(target_os = "unknown"))]
pub use stderr::{stderr, Stderr};
#[cfg(not(target_os = "unknown"))]
pub use stdin::{stdin, Stdin};
#[cfg(not(target_os = "unknown"))]
pub use stdout::{stdout, Stdout};
pub use timeout::timeout;

mod timeout;
#[cfg(not(target_os = "unknown"))]
mod stderr;
#[cfg(not(target_os = "unknown"))]
mod stdin;
#[cfg(not(target_os = "unknown"))]
mod stdio;
#[cfg(not(target_os = "unknown"))]
mod stdout;
}

cfg_unstable_default! {
#[cfg(not(target_os = "unknown"))]
pub use stderr::StderrLock;
#[cfg(not(target_os = "unknown"))]
pub use stdin::StdinLock;
#[cfg(not(target_os = "unknown"))]
pub use stdout::StdoutLock;
}
11 changes: 5 additions & 6 deletions src/io/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::mem;

use crate::io::IoSliceMut;

pub use take::Take;
pub use bytes::Bytes;
pub use chain::Chain;
pub use take::Take;

extension_trait! {
use std::pin::Pin;
Expand Down Expand Up @@ -483,7 +483,7 @@ mod tests {
use crate::prelude::*;

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

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

// original file still usable, read the rest
assert_eq!(f.read_to_end(&mut other_buffer).await?, 4);
assert_eq!(f.read_to_end(&mut other_buffer).await.unwrap(), 4);
assert_eq!(&other_buffer, &[5, 6, 7, 8]);
Ok(())
})
});
}
}
2 changes: 1 addition & 1 deletion src/io/read/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<T: BufRead> BufRead for Take<T> {
}
}

#[cfg(test)]
#[cfg(all(test, not(target_os = "unknown")))]
mod tests {
use crate::io;
use crate::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion src/io/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::task::{Context, Poll};
use std::time::Duration;

use pin_project_lite::pin_project;
use smol::Timer;

use crate::io;
use crate::utils::Timer;

/// Awaits an I/O future or times out after a duration of time.
///
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,17 @@ cfg_std! {
}

cfg_default! {
#[cfg(not(target_os = "unknown"))]
pub mod fs;
pub mod path;
pub mod net;
#[cfg(not(target_os = "unknown"))]
pub(crate) mod rt;
}

cfg_unstable! {
pub mod pin;
#[cfg(not(target_os = "unknown"))]
pub mod process;

mod unit;
Expand Down
6 changes: 6 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ pub use std::net::Shutdown;
pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};

#[cfg(not(target_os = "unknown"))]
pub use addr::ToSocketAddrs;
#[cfg(not(target_os = "unknown"))]
pub use tcp::{Incoming, TcpListener, TcpStream};
#[cfg(not(target_os = "unknown"))]
pub use udp::UdpSocket;

#[cfg(not(target_os = "unknown"))]
mod addr;
#[cfg(not(target_os = "unknown"))]
mod tcp;
#[cfg(not(target_os = "unknown"))]
mod udp;
12 changes: 10 additions & 2 deletions src/path/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::ffi::{OsStr, OsString};
use std::rc::Rc;
use std::sync::Arc;

use crate::fs;
use crate::io;
use crate::path::{Ancestors, Components, Display, Iter, PathBuf, StripPrefixError};
#[cfg(not(target_os = "unknown"))]
use crate::{fs, io};

/// A slice of a path.
///
Expand Down Expand Up @@ -584,6 +584,7 @@ impl Path {
/// #
/// # Ok(()) }) }
/// ```
#[cfg(not(target_os = "unknown"))]
pub async fn metadata(&self) -> io::Result<fs::Metadata> {
fs::metadata(self).await
}
Expand All @@ -607,6 +608,7 @@ impl Path {
/// #
/// # Ok(()) }) }
/// ```
#[cfg(not(target_os = "unknown"))]
pub async fn symlink_metadata(&self) -> io::Result<fs::Metadata> {
fs::symlink_metadata(self).await
}
Expand All @@ -632,6 +634,7 @@ impl Path {
/// #
/// # Ok(()) }) }
/// ```
#[cfg(not(target_os = "unknown"))]
pub async fn canonicalize(&self) -> io::Result<PathBuf> {
fs::canonicalize(self).await
}
Expand All @@ -654,6 +657,7 @@ impl Path {
/// #
/// # Ok(()) }) }
/// ```
#[cfg(not(target_os = "unknown"))]
pub async fn read_link(&self) -> io::Result<PathBuf> {
fs::read_link(self).await
}
Expand Down Expand Up @@ -688,6 +692,7 @@ impl Path {
/// #
/// # Ok(()) }) }
/// ```
#[cfg(not(target_os = "unknown"))]
pub async fn read_dir(&self) -> io::Result<fs::ReadDir> {
fs::read_dir(self).await
}
Expand Down Expand Up @@ -717,6 +722,7 @@ impl Path {
/// check errors, call [fs::metadata].
///
/// [fs::metadata]: ../fs/fn.metadata.html
#[cfg(not(target_os = "unknown"))]
pub async fn exists(&self) -> bool {
fs::metadata(self).await.is_ok()
}
Expand Down Expand Up @@ -749,6 +755,7 @@ impl Path {
///
/// [fs::metadata]: ../fs/fn.metadata.html
/// [fs::Metadata::is_file]: ../fs/struct.Metadata.html#method.is_file
#[cfg(not(target_os = "unknown"))]
pub async fn is_file(&self) -> bool {
fs::metadata(self)
.await
Expand Down Expand Up @@ -785,6 +792,7 @@ impl Path {
///
/// [fs::metadata]: ../fs/fn.metadata.html
/// [fs::Metadata::is_dir]: ../fs/struct.Metadata.html#method.is_dir
#[cfg(not(target_os = "unknown"))]
pub async fn is_dir(&self) -> bool {
fs::metadata(self)
.await
Expand Down
2 changes: 1 addition & 1 deletion src/stream/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::task::{Context, Poll};
use std::time::Duration;

use crate::stream::Stream;
use smol::Timer;
use crate::utils::Timer;

/// Creates a new stream that yields at a set interval.
///
Expand Down
2 changes: 1 addition & 1 deletion src/stream/stream/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use core::pin::Pin;
use core::time::Duration;

use pin_project_lite::pin_project;
use smol::Timer;

use crate::stream::Stream;
use crate::task::{Context, Poll};
use crate::utils::Timer;

pin_project! {
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion src/stream/stream/throttle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::pin::Pin;
use std::time::Duration;

use pin_project_lite::pin_project;
use smol::Timer;

use crate::stream::Stream;
use crate::task::{Context, Poll};
use crate::utils::Timer;

pin_project! {
/// A stream that only yields one element once every `duration`.
Expand Down
2 changes: 1 addition & 1 deletion src/stream/stream/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::pin::Pin;
use std::time::Duration;

use pin_project_lite::pin_project;
use smol::Timer;

use crate::stream::Stream;
use crate::task::{Context, Poll};
use crate::utils::Timer;

pin_project! {
/// A stream with timeout time set
Expand Down
2 changes: 1 addition & 1 deletion src/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl BarrierWaitResult {
}
}

#[cfg(test)]
#[cfg(all(test, not(target_os = "unknown")))]
mod test {
use futures::channel::mpsc::unbounded;
use futures::sink::SinkExt;
Expand Down
11 changes: 11 additions & 0 deletions src/task/block_on.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,20 @@ use crate::task::Builder;
/// })
/// }
/// ```
#[cfg(not(target_os = "unknown"))]
pub fn block_on<F, T>(future: F) -> T
where
F: Future<Output = T>,
{
Builder::new().blocking(future)
}

/// Spawns a task and waits for it to finish.
#[cfg(target_os = "unknown")]
pub fn block_on<F, T>(future: F)
where
F: Future<Output = T> + 'static,
T: 'static,
{
Builder::new().local(future).unwrap();
}
Loading

0 comments on commit e4df140

Please sign in to comment.