[英]How can I store an enum so I can retrieve it by only identifying the variant?
I have an enum like: 我有一个像这样的枚举:
pub enum Component {
Position { vector: [f64; 2] },
RenderFn { render_fn: fn(Display, &mut Frame, Entity), },
}
I would like to store Component
s in a hashset/hashmap where they are identified only by their enum variant ( Position
or RenderFn
). 我想将Component
存储在哈希集/哈希图中,其中仅通过其枚举变量( Position
或RenderFn
)来标识它们。
There can be zero or one Position
and zero or one RenderFn
in the collection. 集合中可以有零个或一个Position
以及零个或一个RenderFn
。 I would like to be able to remove/retrieve it by passing an identifier/type ( Position
/ RenderFn
). 我希望能够通过传递一个标识符/类型( Position
/ RenderFn
)来删除/检索它。
Is there any way to do this without any ugly hacks? 有没有办法做到这一点而没有任何难看的骇客? Perhaps enums are not the way to go? 也许枚举不是要走的路?
It sounds like you want a structure, not a collection of enum variants. 听起来您需要结构,而不是枚举变量的集合。
struct Component {
position: Option<[f64; 2]>,
render_fn: Option<fn(Display, &mut Frame, Entity)>,
}
If this is likely to involve many kinds of components, and they mostly won't all be present, then maybe you want something like the typemap
crate. 如果这是可能的多种成分参与,他们大多不会悉数到场,那么也许你想要的东西,如typemap
箱子。
But to answer your question: no, a variant can't be separated from its associated values. 但是要回答您的问题:不,一个变体不能与其关联的值分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.