File tree Expand file tree Collapse file tree 5 files changed +79
-21
lines changed Expand file tree Collapse file tree 5 files changed +79
-21
lines changed Original file line number Diff line number Diff line change 21
21
- name : cargo doc
22
22
run : cargo doc --no-deps
23
23
24
+ test_wasm :
25
+ name : Test (wasm)
26
+ runs-on : ubuntu-latest
27
+ strategy :
28
+ matrix :
29
+ rust :
30
+ - stable
31
+ steps :
32
+ - uses : actions/checkout@master
33
+ - name : Install Rust and add wasm target
34
+ run : rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }} && rustup target add wasm32-unknown-unknown
35
+ - name : Install wasm-pack
36
+ uses : taiki-e/cache-cargo-install-action@v1
37
+ with :
38
+ tool : wasm-pack@0.12.1
39
+ - name : cargo test
40
+ run : wasm-pack test --firefox --headless -- --features=wasm-bindgen
41
+ - name : cargo doc
42
+ run : cargo doc --no-deps --target=wasm32-unknown-unknown --features=wasm-bindgen
43
+
24
44
style :
25
45
name : Style
26
46
runs-on : ubuntu-latest
59
79
git -c user.name='ci' -c user.email='ci' commit -m 'Deploy futures-timer API documentation'
60
80
git push -f -q https://git:${{ secrets.github_token }}@github.com/${{ github.repository }} HEAD:gh-pages
61
81
if : github.event_name == 'push' && github.event.ref == 'refs/heads/master' && github.repository == 'async-rs/futures-timer'
62
-
63
- check_wasm :
64
- name : Check Wasm
65
- needs : [test]
66
- runs-on : ubuntu-latest
67
- steps :
68
- - uses : actions/checkout@master
69
- - name : Install Rust and add wasm target
70
- run : rustup update stable && rustup target add wasm32-unknown-unknown
71
- - name : cargo check
72
- run : cargo check --target wasm32-unknown-unknown --features wasm-bindgen
Original file line number Diff line number Diff line change @@ -18,7 +18,10 @@ send_wrapper = { version = "0.4.0", optional = true }
18
18
19
19
[dev-dependencies ]
20
20
async-std = { version = " 1.0.1" , features = [" attributes" ] }
21
+ cfg-if = " 1.0.0"
21
22
futures = " 0.3.1"
23
+ wasm-bindgen-test = " 0.3.42"
24
+ web-time = " 1.1.0"
22
25
23
26
[features ]
24
27
wasm-bindgen = [
Original file line number Diff line number Diff line change @@ -11,13 +11,15 @@ use std::{
11
11
12
12
/// A version of `Delay` that works on wasm.
13
13
#[ derive( Debug ) ]
14
- pub struct Delay ( SendWrapper < TimeoutFuture > ) ;
14
+ pub struct Delay ( Option < SendWrapper < TimeoutFuture > > ) ;
15
15
16
16
impl Delay {
17
17
/// Creates a new future which will fire at `dur` time into the future.
18
18
#[ inline]
19
19
pub fn new ( dur : Duration ) -> Delay {
20
- Self ( SendWrapper :: new ( TimeoutFuture :: new ( dur. as_millis ( ) as u32 ) ) )
20
+ Self ( Some ( SendWrapper :: new ( TimeoutFuture :: new (
21
+ dur. as_millis ( ) as u32
22
+ ) ) ) )
21
23
}
22
24
23
25
/// Resets the timeout.
@@ -30,7 +32,16 @@ impl Delay {
30
32
impl Future for Delay {
31
33
type Output = ( ) ;
32
34
33
- fn poll ( self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Self :: Output > {
34
- Pin :: new ( & mut * Pin :: into_inner ( self ) . 0 ) . poll ( cx)
35
+ fn poll ( mut self : Pin < & mut Self > , cx : & mut Context ) -> Poll < Self :: Output > {
36
+ match self . 0 . as_mut ( ) {
37
+ Some ( delay) => match Pin :: new ( & mut * * delay) . poll ( cx) {
38
+ Poll :: Pending => Poll :: Pending ,
39
+ Poll :: Ready ( ( ) ) => {
40
+ self . 0 . take ( ) ;
41
+ Poll :: Ready ( ( ) )
42
+ }
43
+ } ,
44
+ None => Poll :: Ready ( ( ) ) ,
45
+ }
35
46
}
36
47
}
Original file line number Diff line number Diff line change 1
1
use std:: error:: Error ;
2
2
use std:: pin:: Pin ;
3
- use std:: time:: { Duration , Instant } ;
3
+ use std:: time:: Duration ;
4
4
5
+ use futures:: FutureExt ;
5
6
use futures_timer:: Delay ;
6
7
7
- #[ async_std:: test]
8
+ cfg_if:: cfg_if! {
9
+ if #[ cfg( all( target_arch = "wasm32" , feature = "wasm-bindgen" ) ) ] {
10
+ use wasm_bindgen_test:: wasm_bindgen_test as async_test;
11
+ use web_time:: Instant ;
12
+ wasm_bindgen_test:: wasm_bindgen_test_configure!( run_in_browser) ;
13
+ } else {
14
+ use std:: time:: Instant ;
15
+ use async_std:: test as async_test;
16
+ }
17
+ }
18
+
19
+ #[ async_test]
8
20
async fn works ( ) {
9
21
let i = Instant :: now ( ) ;
10
22
let dur = Duration :: from_millis ( 100 ) ;
11
23
let _d = Delay :: new ( dur) . await ;
12
24
assert ! ( i. elapsed( ) > dur) ;
13
25
}
14
26
15
- #[ async_std :: test ]
27
+ #[ async_test ]
16
28
async fn reset ( ) -> Result < ( ) , Box < dyn Error + Send + Sync + ' static > > {
17
29
let i = Instant :: now ( ) ;
18
30
let dur = Duration :: from_millis ( 100 ) ;
@@ -29,3 +41,15 @@ async fn reset() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
29
41
assert ! ( i. elapsed( ) > dur) ;
30
42
Ok ( ( ) )
31
43
}
44
+
45
+ #[ async_test]
46
+ async fn use_after_ready ( ) {
47
+ let dur = Duration :: from_millis ( 100 ) ;
48
+ let mut d = Delay :: new ( dur) ;
49
+
50
+ Pin :: new ( & mut d) . await ;
51
+
52
+ // Use after ready should return immediately if `Delay::reset`
53
+ // was not called.
54
+ Pin :: new ( & mut d) . now_or_never ( ) . unwrap ( ) ;
55
+ }
Original file line number Diff line number Diff line change 1
1
use std:: error:: Error ;
2
- use std:: time:: { Duration , Instant } ;
2
+ use std:: time:: Duration ;
3
3
4
4
use futures_timer:: Delay ;
5
5
6
- #[ async_std:: test]
6
+ cfg_if:: cfg_if! {
7
+ if #[ cfg( all( target_arch = "wasm32" , feature = "wasm-bindgen" ) ) ] {
8
+ use wasm_bindgen_test:: wasm_bindgen_test as async_test;
9
+ use web_time:: Instant ;
10
+ wasm_bindgen_test:: wasm_bindgen_test_configure!( run_in_browser) ;
11
+ } else {
12
+ use std:: time:: Instant ;
13
+ use async_std:: test as async_test;
14
+ }
15
+ }
16
+
17
+ #[ async_test]
7
18
async fn smoke ( ) -> Result < ( ) , Box < dyn Error + Send + Sync + ' static > > {
8
19
let dur = Duration :: from_millis ( 10 ) ;
9
20
let start = Instant :: now ( ) ;
@@ -12,7 +23,7 @@ async fn smoke() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
12
23
Ok ( ( ) )
13
24
}
14
25
15
- #[ async_std :: test ]
26
+ #[ async_test ]
16
27
async fn two ( ) -> Result < ( ) , Box < dyn Error + Send + Sync + ' static > > {
17
28
let dur = Duration :: from_millis ( 10 ) ;
18
29
Delay :: new ( dur) . await ;
You can’t perform that action at this time.
0 commit comments