use futures::channel::oneshot; use futures::executor::{block_on, LocalPool}; use futures::future::{self, FutureExt, LocalFutureObj, TryFutureExt}; use futures::task::LocalSpawn; use std::cell::{Cell, RefCell}; use std::panic::AssertUnwindSafe; use std::rc::Rc; use std::task::Poll; use std::thread;
tx.send(42).unwrap(); // Should cause `f2` and then `rx3` to get resolved. let result = block_on(rx3).unwrap();
assert_eq!(result, 42);
t2.join().unwrap();
}
#[test] fn drop_in_poll() { let slot1 = Rc::new(RefCell::new(None)); let slot2 = slot1.clone();
let future1 = future::lazy(move |_| {
slot2.replace(None); // Drop future 1
})
.shared();
let future2 = LocalFutureObj::new(Box::new(future1.clone()));
slot1.replace(Some(future2));
let (tx0, rx0) = oneshot::channel::<i32>(); let f1 = rx0.shared(); let f2 = f1.clone();
// Repeated calls on the original or clone do not change the outcome. for _ in0..2 {
assert!(f1.peek().is_none());
assert!(f2.peek().is_none());
}
// Completing the underlying future has no effect, because the value has not been `poll`ed in.
tx0.send(42).unwrap(); for _ in0..2 {
assert!(f1.peek().is_none());
assert!(f2.peek().is_none());
}
// Once the Shared has been polled, the value is peekable on the clone.
spawn.spawn_local_obj(LocalFutureObj::new(Box::new(f1.map(|_| ())))).unwrap();
local_pool.run(); for _ in0..2 {
assert_eq!(*f2.peek().unwrap(), Ok(42));
}
}
#[test] fn downgrade() { let (tx, rx) = oneshot::channel::<i32>(); let shared = rx.shared(); // Since there are outstanding `Shared`s, we can get a `WeakShared`. let weak = shared.downgrade().unwrap(); // It should upgrade fine right now. letmut shared2 = weak.upgrade().unwrap();
// We should still be able to get a new `WeakShared` and upgrade it // because `shared2` is outstanding.
assert!(shared2.downgrade().is_some());
assert!(weak.upgrade().is_some());
assert_eq!(block_on(&mut shared2).unwrap(), 42); // Now that all `Shared`s have been exhausted, we should not be able // to get a new `WeakShared` or upgrade an existing one.
assert!(weak.upgrade().is_none());
assert!(shared2.downgrade().is_none());
}
#[test] fn ptr_eq() { use future::FusedFuture; use std::collections::hash_map::DefaultHasher; use std::hash::Hasher;
// Because these two futures share the same underlying future, // `ptr_eq` should return true.
assert!(shared.ptr_eq(&shared2)); // Equivalence relations are symmetric
assert!(shared2.ptr_eq(&shared));
// If `ptr_eq` returns true, they should hash to the same value.
shared.ptr_hash(&mut hasher);
shared2.ptr_hash(&mut hasher2);
assert_eq!(hasher.finish(), hasher2.finish());
// Now that `shared2` has completed, `ptr_eq` should return false.
assert!(shared2.is_terminated());
assert!(!shared.ptr_eq(&shared2));
// `ptr_eq` should continue to work for the other `Shared`. let shared3 = shared.clone(); letmut hasher3 = DefaultHasher::new();
assert!(shared.ptr_eq(&shared3));
#[test] fn shared_future_that_wakes_itself_until_pending_is_returned() { let proceed = Cell::new(false); let fut = futures::future::poll_fn(|cx| { if proceed.get() {
Poll::Ready(())
} else {
cx.waker().wake_by_ref();
Poll::Pending
}
})
.shared();
// The join future can only complete if the second future gets a chance to run after the first // has returned pending
assert_eq!(block_on(futures::future::join(fut, async { proceed.set(true) })), ((), ()));
}
#[test] #[should_panic(expected = "inner future panicked during poll")] fn panic_while_poll() { let fut = futures::future::poll_fn::<i8, _>(|_cx| panic!("test")).shared();
let fut_captured = fut.clone();
std::panic::catch_unwind(AssertUnwindSafe(|| {
block_on(fut_captured);
}))
.unwrap_err();
impl Drop for S { fn drop(&mutself) { let fut = futures::future::ready(1).shared();
assert_eq!(block_on(fut.clone()), 1);
assert_eq!(block_on(fut), 1);
}
}
let _s = S {};
panic!("test_marker");
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.