-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Future::boxed and Stream::box should prevent double boxing
Fixes #511
- Loading branch information
1 parent
9f03c50
commit 3d4a5d2
Showing
3 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
extern crate futures; | ||
|
||
use futures::Async; | ||
use futures::Poll; | ||
use futures::future::Future; | ||
use futures::stream::Stream; | ||
|
||
|
||
#[test] | ||
fn future_boxed_prevents_double_boxing() { | ||
struct MyFuture { | ||
r: &'static str, | ||
} | ||
|
||
impl Future for MyFuture { | ||
type Item = &'static str; | ||
type Error = (); | ||
|
||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
Ok(Async::Ready(self.r)) | ||
} | ||
} | ||
|
||
let f = MyFuture { r: "I'm ready" }; | ||
let f = f.boxed(); | ||
let ptr = f.as_ref() as *const Future<Item=_, Error=_>; | ||
let f = f.boxed(); | ||
let f = f.boxed(); | ||
let mut f = f.boxed(); | ||
assert_eq!(f.as_ref() as *const Future<Item=_, Error=_>, ptr); | ||
assert_eq!(Ok(Async::Ready("I'm ready")), f.poll()); | ||
} | ||
|
||
#[test] | ||
fn stream_boxed_prevents_double_boxing() { | ||
struct MyStream { | ||
i: u32, | ||
} | ||
|
||
impl Stream for MyStream { | ||
type Item = u32; | ||
type Error = (); | ||
|
||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { | ||
self.i += 1; | ||
Ok(Async::Ready(Some(self.i))) | ||
} | ||
} | ||
|
||
let s = MyStream { i: 0 }; | ||
let s = s.boxed(); | ||
let ptr = s.as_ref() as *const Stream<Item=_, Error=_>; | ||
let s = s.boxed(); | ||
let s = s.boxed(); | ||
let mut s = s.boxed(); | ||
assert_eq!(s.as_ref() as *const Stream<Item=_, Error=_>, ptr); | ||
assert_eq!(Ok(Async::Ready(Some(1))), s.poll()); | ||
assert_eq!(Ok(Async::Ready(Some(2))), s.poll()); | ||
assert_eq!(Ok(Async::Ready(Some(3))), s.poll()); | ||
} |