Is there a way to add extension functions to Future?

use std::future::Future;

trait FutureFunctions {
    type Output;
    fn race() {

impl<F> FutureFunctions for dyn Future<Output = F> {
    type Output = F;
    fn race() {

fn main() {
    <dyn Future::<Output = ()>>::race();

    // doesn't compile:
    // Future::<Output = ()>::race();

I just want to remove <dyn ...>. Then I'll be able to do this with my framework:

use rialight::prelude::*;

let _ = Future::race(iterable).await;

It'll use the futures crate behind

impl for T: Future?

Nope. I tried a non-dyn bound instead too, similiar to that:

impl<T, Item> FutureFunctions for T
    where T: Future<Output = Item>
    type Output = Item;

I don't think you can make this syntax work. If you want to call the function like this, why not make it a freestanding function?


1 Like

Future is not a type (since edition 2021), so it can't be used where the type is expected, including the method call syntax.


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.