Why must I create a useless field of a generic type in a struct?

Viewed 146

I am trying to make an Endpoint struct that I can create passing in an async function to call later on.

use std::future::Future;

#[derive(Debug)]
struct Foo {
    a: u8,
}

#[derive(Debug)]
struct FooRes {
    a: u8,
}

async fn foo(f: Foo) -> FooRes {
    println!("in foo {:?}", f);
    return FooRes { a: 1 };
}

struct Endpoint<A, R, F, Fut>
where
    F: Fn(A) -> Fut,
    Fut: Future<Output = R>,
{
    // if i comment this out I get compiler errors
    // telling me to remove all the generic types except F even
    // though they are used in the constraint on F
    a: A,
    exe: F,
}

#[async_std::main]
async fn main() {
    let ep = Endpoint {
        a: Foo { a: 1 },
        exe: foo,
    };
    let r = (ep.exe)(Foo { a: 1 }).await;
    println!("in main {:?}", r);
}

This code compiles and runs as expected; however, it is bothering me that I need the a: A in the Endpoint struct, or the compiler errors because of the unused generic types in Endpoint. Can I define generics on F directly to avoid this, or is there some other strategy to get around needing a useless field on the struct?

0 Answers
Related