How to pass a 'None' value to async function in rust

Viewed 455

Consider following code

async fn main_call(&self) -> Result<()> 
{
   let _list = self.sub_function(/*what to pass here?*/).await;
   // do something with the result

   Ok(())
}

async fn sub_function<P, F>(&self, f : Option<F>) -> Result<Vec<P>> 
   where 
     F : FnOnce(P) -> bool, 
     P : Clone
{

    // do something, and return a value
    // the `Vec::new()` is just here for demonstration purposes
    Ok(Vec::new())
}

What shall I pass as value to sub_function, if I don't want to pass a closure? If I pass None, the compiler complains:

type inside `async fn` body must be known in this context E0698 cannot infer type for type parameter `T`
 declared on the enum `Option` Note: the type is part of the `async fn` body because of this `await`

The complaint is obvious, but I'm not sure what I shall pass. None::<FnOnce(12)->bool) does not work.

expected a `std::ops::FnOnce<(&_,)>` closure, found `dyn std::ops::FnOnce(u32) -> bool` 
E0277 expected an `FnOnce<(&_,)>` closure, found `dyn std::ops::FnOnce(u32) -> bool` 
Help: the trait `std::ops::FnOnce<(&_,)>` is not implemented for `dyn std::ops::FnOnce(u32) -> bool`

Again, the error message is obvious.

2 Answers

You can use turbofish notation to specify the real type of None. Since this is in fact Option::<T>::None, i.e. is is generic itself, this is viable:

async fn main_call(&self) -> Result<()> {
    // Any type can be used here, since it won't be used anyway.
    let _list = self.sub_function(None::<fn(()) -> bool>).await;
    // do something with the result

    Ok(())
}

Here's a playground with your (slightly fixed to work as MCVE) example with this addition.

The None type must be explicitly known at compile time. So calling sub_function would look like this

async fn main_call(&self) -> Result<()> 
{
   let _list = self.sub_function::<u32, Box<dyn FnOnce(u32) -> bool>>(None).await;
   // do something with the result

   Ok(())
}
Related