How to compute the size of a returned Future at compile time, without constructing it?

I have a complex async function:

impl Session {
   async fn handle_connection(self) -> Result<Whatever> { ... }

This function is doing a lot of nested async calls and captures quite a lot of state.
I can invoke it and measure the size of the returned Future with std::mem::size_of_val.
However, for that I need to construct a Session value and the future.

Is there a way of computing the size of the returned Future without invoking the method?
I mean - sth like std::mem::size_of::<the return type of Session::handle_connection>() ?

I need to put the computed value into the Session field itself, so the session knows how much memory it uses. So I've got a chicken-and-egg problem here if the only way to compute the size is to create a Session first.

You can write a function that takes a closure to infer the return type of Session::handle_connection and then call std::mem::size_of on that type. Rust Playground


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.