]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/futures-api.rs
6e757fb4f9a42f16ce74b5ceab74d996429b008c
[rust.git] / src / test / run-pass / futures-api.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(arbitrary_self_types, futures_api, pin)]
12 #![allow(unused)]
13
14 use std::future::Future;
15 use std::pin::Pin;
16 use std::rc::Rc;
17 use std::sync::{
18     Arc,
19     atomic::{self, AtomicUsize},
20 };
21 use std::future::FutureObj;
22 use std::task::{
23     Context, Poll,
24     Wake, Waker, LocalWaker,
25     Spawn, SpawnObjError,
26     local_waker, local_waker_from_nonlocal,
27 };
28
29 struct Counter {
30     local_wakes: AtomicUsize,
31     nonlocal_wakes: AtomicUsize,
32 }
33
34 impl Wake for Counter {
35     fn wake(this: &Arc<Self>) {
36         this.nonlocal_wakes.fetch_add(1, atomic::Ordering::SeqCst);
37     }
38
39     unsafe fn wake_local(this: &Arc<Self>) {
40         this.local_wakes.fetch_add(1, atomic::Ordering::SeqCst);
41     }
42 }
43
44 struct NoopSpawner;
45
46 impl Spawn for NoopSpawner {
47     fn spawn_obj(&mut self, _: FutureObj<'static, ()>) -> Result<(), SpawnObjError> {
48         Ok(())
49     }
50 }
51
52 struct MyFuture;
53
54 impl Future for MyFuture {
55     type Output = ();
56     fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
57         // Ensure all the methods work appropriately
58         cx.waker().wake();
59         cx.waker().wake();
60         cx.local_waker().wake();
61         cx.spawner().spawn_obj(Box::pinned(MyFuture).into()).unwrap();
62         Poll::Ready(())
63     }
64 }
65
66 fn test_local_waker() {
67     let counter = Arc::new(Counter {
68         local_wakes: AtomicUsize::new(0),
69         nonlocal_wakes: AtomicUsize::new(0),
70     });
71     let waker = unsafe { local_waker(counter.clone()) };
72     let spawner = &mut NoopSpawner;
73     let cx = &mut Context::new(&waker, spawner);
74     assert_eq!(Poll::Ready(()), Pin::new(&mut MyFuture).poll(cx));
75     assert_eq!(1, counter.local_wakes.load(atomic::Ordering::SeqCst));
76     assert_eq!(2, counter.nonlocal_wakes.load(atomic::Ordering::SeqCst));
77 }
78
79 fn test_local_as_nonlocal_waker() {
80     let counter = Arc::new(Counter {
81         local_wakes: AtomicUsize::new(0),
82         nonlocal_wakes: AtomicUsize::new(0),
83     });
84     let waker: LocalWaker = local_waker_from_nonlocal(counter.clone());
85     let spawner = &mut NoopSpawner;
86     let cx = &mut Context::new(&waker, spawner);
87     assert_eq!(Poll::Ready(()), Pin::new(&mut MyFuture).poll(cx));
88     assert_eq!(0, counter.local_wakes.load(atomic::Ordering::SeqCst));
89     assert_eq!(3, counter.nonlocal_wakes.load(atomic::Ordering::SeqCst));
90 }
91
92 fn main() {
93     test_local_waker();
94     test_local_as_nonlocal_waker();
95 }