future is actually a statemachine, and all of the value that kept across the wait point are saved in the statemachine
for example
async fn foo() -> u32 {
let mut b = [0;1024]
tokio::fs::read_into_string("file", &b).await;
println!("len is {}", b.len());
}
we can think of a future as mutliple chunk of code
enum Statemachine {
Chunk1 {
x:[u8; 1024],
fut: tokio::fs::ReadIntoFuture<'x>,
},
...yield
Chunk2 {
}
}
when we reture the future to the caller, we actually return the statemachine to caller, so this 1024 length of bytes are returned to the caller too. if your async function call another async function, you don’nt know how large data the future will container, that will cause lots of memcpy. One way to solve is, if the variable you pass to future is large, you can box it, so memcpy become cheap.