caio
caio

Reputation: 1989

Type signature for generic closure with trait constraint

I have the following struct that works fine:

pub struct Pattern {
    pub regex: &'static str,
    pub view: Fn (Request) -> Response,
}

But I'd like to change view to accept any type that implements Renderable (trait constraint). I was expecting to make it work this way:

pub struct Pattern {
    pub regex: &'static str,
    pub view: Fn <T: Renderable> (Request) -> T,
}

But no luck. Any ideas?

Upvotes: 0

Views: 445

Answers (1)

Shepmaster
Shepmaster

Reputation: 431779

You want to use a where clause on the struct (and any implementations for that struct):

trait A { fn moo(&self); }
struct S;

struct Pattern<T>
    where T: A
{
    view: Fn (T) -> S,
}

fn main() {}

Upvotes: 1

Related Questions