|
| 1 | +// Test that we don't duplicate storage for futures moved around in .await, and |
| 2 | +// for futures moved into other futures. |
| 3 | +// |
| 4 | +// The exact sizes can change by a few bytes (we'd like to know when they do). |
| 5 | +// What we don't want to see is the wrong multiple of 1024 (the size of BigFut) |
| 6 | +// being reflected in the size. |
| 7 | +// |
| 8 | +// See issue #59123 for a full explanation. |
| 9 | + |
| 10 | +// edition:2018 |
| 11 | + |
| 12 | +#![feature(async_await)] |
| 13 | + |
| 14 | +use std::future::Future; |
| 15 | +use std::pin::Pin; |
| 16 | +use std::task::{Context, Poll}; |
| 17 | + |
| 18 | +const BIG_FUT_SIZE: usize = 1024; |
| 19 | +struct BigFut([u8; BIG_FUT_SIZE]); |
| 20 | + |
| 21 | +impl BigFut { |
| 22 | + fn new() -> Self { |
| 23 | + BigFut([0; BIG_FUT_SIZE]) |
| 24 | + } } |
| 25 | + |
| 26 | +impl Drop for BigFut { |
| 27 | + fn drop(&mut self) {} |
| 28 | +} |
| 29 | + |
| 30 | +impl Future for BigFut { |
| 31 | + type Output = (); |
| 32 | + |
| 33 | + fn poll(self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Self::Output> { |
| 34 | + Poll::Ready(()) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[allow(dead_code)] |
| 39 | +struct Joiner { |
| 40 | + a: Option<BigFut>, |
| 41 | + b: Option<BigFut>, |
| 42 | + c: Option<BigFut>, |
| 43 | +} |
| 44 | + |
| 45 | +impl Future for Joiner { |
| 46 | + type Output = (); |
| 47 | + |
| 48 | + fn poll(self: Pin<&mut Self>, _ctx: &mut Context<'_>) -> Poll<Self::Output> { |
| 49 | + Poll::Ready(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn noop() {} |
| 54 | + |
| 55 | +async fn single() { |
| 56 | + let x = BigFut::new(); |
| 57 | + x.await; |
| 58 | +} |
| 59 | + |
| 60 | +async fn single_with_noop() { |
| 61 | + let x = BigFut::new(); |
| 62 | + noop(); |
| 63 | + x.await; |
| 64 | +} |
| 65 | + |
| 66 | +async fn joined() { |
| 67 | + let a = BigFut::new(); |
| 68 | + let b = BigFut::new(); |
| 69 | + let c = BigFut::new(); |
| 70 | + |
| 71 | + let joiner = Joiner { |
| 72 | + a: Some(a), |
| 73 | + b: Some(b), |
| 74 | + c: Some(c), |
| 75 | + }; |
| 76 | + joiner.await |
| 77 | +} |
| 78 | + |
| 79 | +async fn joined_with_noop() { |
| 80 | + let a = BigFut::new(); |
| 81 | + let b = BigFut::new(); |
| 82 | + let c = BigFut::new(); |
| 83 | + |
| 84 | + let joiner = Joiner { |
| 85 | + a: Some(a), |
| 86 | + b: Some(b), |
| 87 | + c: Some(c), |
| 88 | + }; |
| 89 | + noop(); |
| 90 | + joiner.await |
| 91 | +} |
| 92 | + |
| 93 | +fn main() { |
| 94 | + assert_eq!(1028, std::mem::size_of_val(&single())); |
| 95 | + assert_eq!(1032, std::mem::size_of_val(&single_with_noop())); |
| 96 | + assert_eq!(3084, std::mem::size_of_val(&joined())); |
| 97 | + assert_eq!(3084, std::mem::size_of_val(&joined_with_noop())); |
| 98 | +} |
0 commit comments