我不明白为什么这不起作用:
pub trait State {}
pub trait WithFinal: State {}
pub struct Machine<T: State> {
pub state: T,
error: Option<fn(&Event, &T)>,
transition: Option<fn(&T, &T, Event)>, // fn(¤t_state, &previous_state)
}
impl<T: WithFinal> Drop for Machine<T> {
fn drop(&mut self) {}
}
Compiling scdlang v0.1.0 (/home/wildan/Projects/OSS/scdlang)
error[E0367]: The requirement `T: statechart::WithFinal` is added only by the Drop impl.
--> src/main.rs:92:5
|
92 | / impl<T: WithFinal> Drop for Machine<T> {
93 | | fn drop(&mut self) {}
94 | | }
| |_____^
|
note: The same requirement must be part of the struct/enum definition
--> src/main.rs:74:5
|
74 | / pub struct Machine<T: State> {
75 | | pub state: T,
76 | | error: Option<fn(&Event, &T)>,
77 | | transition: Option<fn(&T, &T, Event)>, // fn(¤t_state, &previous_state)
78 | | }
| |_____^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0367`.
error: Could not compile `scdlang`.
To learn more, run the command again with --verbose.
我认为这应该是工作,因为WithFinal
延长特质State
然而,这两个2 impl
工作就好了:
trait DropLike {
fn drop(&mut self);
}
impl<T: WithFinal> DropLike for Machine<T> {
fn drop(&mut self) {}
}
impl<T: State> Drop for Machine<T> {
fn drop(&mut self) {}
}
简短的回答是,你不能落实Drop
于专门泛型类型。
你DropLike
特质是喜欢Drop
,但Drop
是一种语言的项目,并从编译器得到特殊待遇。这意味着该错误只适用于Drop
。
此代码是不合法的:这是不可能专门
Drop
到泛型类型实现的一个子集。为了使此代码工作,MyStruct
还必须要求T
工具Foo
。
(还与rustc --explain E0367
可见)
下面是似乎已经preciptated这种变化the issue。