具有特征约束的通用闭包的类型签名
Type signature for generic closure with trait constraint
我有以下工作正常的结构:
pub struct Pattern {
pub regex: &'static str,
pub view: Fn (Request) -> Response,
}
但我想更改 view
以接受任何实现 Renderable
(特征约束)的类型。我期待以这种方式工作:
pub struct Pattern {
pub regex: &'static str,
pub view: Fn <T: Renderable> (Request) -> T,
}
但运气不好。有什么想法吗?
您想在结构(以及该结构的任何实现)上使用 where
子句:
trait A { fn moo(&self); }
struct S;
struct Pattern<T>
where T: A
{
view: Fn (T) -> S,
}
fn main() {}
我有以下工作正常的结构:
pub struct Pattern {
pub regex: &'static str,
pub view: Fn (Request) -> Response,
}
但我想更改 view
以接受任何实现 Renderable
(特征约束)的类型。我期待以这种方式工作:
pub struct Pattern {
pub regex: &'static str,
pub view: Fn <T: Renderable> (Request) -> T,
}
但运气不好。有什么想法吗?
您想在结构(以及该结构的任何实现)上使用 where
子句:
trait A { fn moo(&self); }
struct S;
struct Pattern<T>
where T: A
{
view: Fn (T) -> S,
}
fn main() {}