test: Add Future and Stream impl for Spawn. (#2412)

This commit is contained in:
Lucio Franco
2020-04-17 15:37:59 -04:00
committed by GitHub
parent 6f00d7158b
commit 19a87e090e
4 changed files with 27 additions and 3 deletions
+4
View File
@@ -1,3 +1,7 @@
# 0.2.1 (April 17, 2020)
- Add `Future` and `Stream` implementations for `task::Spawn<T>`.
# 0.2.0 (November 25, 2019)
- Initial release
+2 -2
View File
@@ -7,13 +7,13 @@ name = "tokio-test"
# - Cargo.toml
# - Update CHANGELOG.md.
# - Create "v0.2.x" git tag.
version = "0.2.0"
version = "0.2.1"
edition = "2018"
authors = ["Tokio Contributors <[email protected]>"]
license = "MIT"
repository = "https://github.com/tokio-rs/tokio"
homepage = "https://tokio.rs"
documentation = "https://docs.rs/tokio-test/0.2.0/tokio_test"
documentation = "https://docs.rs/tokio-test/0.2.1/tokio_test"
description = """
Testing utilities for Tokio- and futures-based code
"""
+1 -1
View File
@@ -1,4 +1,4 @@
#![doc(html_root_url = "https://docs.rs/tokio-test/0.2.0")]
#![doc(html_root_url = "https://docs.rs/tokio-test/0.2.1")]
#![warn(
missing_debug_implementations,
missing_docs,
+20
View File
@@ -116,6 +116,26 @@ impl<T: Stream> Spawn<T> {
}
}
impl<T: Future> Future for Spawn<T> {
type Output = T::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// Safety: we only expose &mut T if T: Unpin therefore this is safe.
let future = unsafe { self.map_unchecked_mut(|s| &mut s.future) };
future.poll(cx)
}
}
impl<T: Stream> Stream for Spawn<T> {
type Item = T::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
// Safety: we only expose &mut T if T: Unpin therefore this is safe.
let stream = unsafe { self.map_unchecked_mut(|s| &mut s.future) };
stream.poll_next(cx)
}
}
impl MockTask {
/// Creates new mock task
fn new() -> Self {