Rust 抱怨泛型参数不满足 trait bound,即使它不是必需的
Rust complains about generic argument not satisfying trait bound even though it is not required
我正在尝试编译以下 rust 代码:
#[derive(Clone)]
struct Foo<Data> {
f: fn(&Data),
}
trait Trait : Clone {
type DataType;
}
// throws:
// error[E0277]: the trait bound `Data: std::clone::Clone` is not satisfied
impl<Data> Trait for Foo<Data> {
type DataType = Data;
}
它抱怨 Data
通用参数不满足 Clone
约束,即使它不应该满足。
根据我的理解 Foo<Data>
应该支持 Clone
而不需要 Data
来支持它。
我做错了什么?
Clone
的 derive
有时会添加限制性的 where
约束。截至 2020 年 5 月,它仍然是一个未解决的错误。参见:#[derive] sometimes uses incorrect bounds · Issue #26925 · rust-lang/rust.
解决方法是为此结构手动实施 Clone
。例如:
impl<Data> Clone for Foo<Data> {
fn clone(&self) -> Self {
Self { f: self.f }
}
}
我正在尝试编译以下 rust 代码:
#[derive(Clone)]
struct Foo<Data> {
f: fn(&Data),
}
trait Trait : Clone {
type DataType;
}
// throws:
// error[E0277]: the trait bound `Data: std::clone::Clone` is not satisfied
impl<Data> Trait for Foo<Data> {
type DataType = Data;
}
它抱怨 Data
通用参数不满足 Clone
约束,即使它不应该满足。
根据我的理解 Foo<Data>
应该支持 Clone
而不需要 Data
来支持它。
我做错了什么?
Clone
的 derive
有时会添加限制性的 where
约束。截至 2020 年 5 月,它仍然是一个未解决的错误。参见:#[derive] sometimes uses incorrect bounds · Issue #26925 · rust-lang/rust.
解决方法是为此结构手动实施 Clone
。例如:
impl<Data> Clone for Foo<Data> {
fn clone(&self) -> Self {
Self { f: self.f }
}
}