|  | 
|  | 1 | +#![cfg(rust_at_least_1_18)] | 
|  | 2 | + | 
|  | 3 | +extern crate futures; | 
|  | 4 | + | 
|  | 5 | +use futures::Async; | 
|  | 6 | +use futures::Poll; | 
|  | 7 | +use futures::future::Future; | 
|  | 8 | +use futures::stream::Stream; | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +#[test] | 
|  | 12 | +fn future_boxed_prevents_double_boxing() { | 
|  | 13 | +    struct MyFuture { | 
|  | 14 | +        r: &'static str, | 
|  | 15 | +    } | 
|  | 16 | + | 
|  | 17 | +    impl Future for MyFuture { | 
|  | 18 | +        type Item = &'static str; | 
|  | 19 | +        type Error = (); | 
|  | 20 | + | 
|  | 21 | +        fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | 
|  | 22 | +            Ok(Async::Ready(self.r)) | 
|  | 23 | +        } | 
|  | 24 | +    } | 
|  | 25 | + | 
|  | 26 | +    let f = MyFuture { r: "I'm ready" }; | 
|  | 27 | +    let f = f.boxed(); | 
|  | 28 | +    let ptr = f.as_ref() as *const Future<Item=_, Error=_>; | 
|  | 29 | +    let f = f.boxed(); | 
|  | 30 | +    let f = f.boxed(); | 
|  | 31 | +    let mut f = f.boxed(); | 
|  | 32 | +    assert_eq!(f.as_ref() as *const Future<Item=_, Error=_>, ptr); | 
|  | 33 | +    assert_eq!(Ok(Async::Ready("I'm ready")), f.poll()); | 
|  | 34 | +} | 
|  | 35 | + | 
|  | 36 | +#[test] | 
|  | 37 | +fn stream_boxed_prevents_double_boxing() { | 
|  | 38 | +    struct MyStream { | 
|  | 39 | +        i: u32, | 
|  | 40 | +    } | 
|  | 41 | + | 
|  | 42 | +    impl Stream for MyStream { | 
|  | 43 | +        type Item = u32; | 
|  | 44 | +        type Error = (); | 
|  | 45 | + | 
|  | 46 | +        fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> { | 
|  | 47 | +            self.i += 1; | 
|  | 48 | +            Ok(Async::Ready(Some(self.i))) | 
|  | 49 | +        } | 
|  | 50 | +    } | 
|  | 51 | + | 
|  | 52 | +    let s = MyStream { i: 0 }; | 
|  | 53 | +    let s = s.boxed(); | 
|  | 54 | +    let ptr = s.as_ref() as *const Stream<Item=_, Error=_>; | 
|  | 55 | +    let s = s.boxed(); | 
|  | 56 | +    let s = s.boxed(); | 
|  | 57 | +    let mut s = s.boxed(); | 
|  | 58 | +    assert_eq!(s.as_ref() as *const Stream<Item=_, Error=_>, ptr); | 
|  | 59 | +    assert_eq!(Ok(Async::Ready(Some(1))), s.poll()); | 
|  | 60 | +    assert_eq!(Ok(Async::Ready(Some(2))), s.poll()); | 
|  | 61 | +    assert_eq!(Ok(Async::Ready(Some(3))), s.poll()); | 
|  | 62 | +} | 
0 commit comments