简体   繁体   English

如何动态确定枚举的变体

[英]How can I dynamically determine the variant of an enum

I want to implement a module that encapsulates different types of messages with different types and quantity of fields that enables to send and receive them using the same send and receive functions, and then determine what variant of the message is, with what fields;我想实现一个模块,用不同类型和数量的字段封装不同类型的消息,以便使用相同的sendreceive函数发送和接收它们,然后确定消息的变体是什么,具有哪些字段; using match .使用match

I have the following enum and functions (simplified):我有以下枚举和函数(简化):

pub enum Message {
  Start { field1 : type1 },
  End   { field2 : type2 },
}

impl Message {
  pub fn send( &self ) {
    send(self);
  }

  pub fn receive( &mut self ) {
    *self = receive();
  }
}

I want to use them as follows:我想按如下方式使用它们:

Send:发送:

let message = Message::Start;
message.send();

Receive收到

let message = Message;
message.receive();
match message {
  Start{field1} => { ... }
  End{field2} => { ... }
};

When calling the receive function, I get a compiler error that says " use of possibly-uninitialized message ".调用receive function 时,我收到一个编译器错误,显示“使用可能未初始化的message ”。 It makes sense because this variable has not been initialized.这是有道理的,因为这个变量还没有被初始化。 How can I achieve this behavior with no errors?我怎样才能在没有错误的情况下实现这种行为?

It sounds like you're looking for an associated function which returns a Message .听起来您正在寻找一个关联的 function ,它返回一个Message

impl Message {
  pub fn receive() -> Message {
    // Do whatever and return a Message::Start or Message::End as needed.
  }
}

// To call...
let my_message = Message::receive();

Associated functions are sort of like static functions in C++, and they use :: on the type name itself as the call syntax.关联函数有点像 C++ 中的 static 函数,它们在类型名称本身上使用::作为调用语法。 At runtime, it's just another ordinary function, but it's in the Message namespace so it's easier to find for programmers at write time.在运行时,它只是另一个普通的 function,但它位于Message命名空间中,因此程序员在编写时更容易找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当给定的枚举不是某种变体时,如何返回None? - How can I return None when a given enum is not a certain variant? 如何在将字段移动到新变体时更改枚举变体? - How can I change enum variant while moving the field to the new variant? 如何根据与变体中的类型匹配的泛型类型选择枚举变体? - How can I select an enum variant based on a generic type that matches a type inside the variant? 如何存储枚举,以便仅通过识别变体就可以检索它? - How can I store an enum so I can retrieve it by only identifying the variant? 如何对枚举进行变异,然后返回对枚举变体的引用? - How do I mutate an enum and then return a reference to an enum variant? 如何在不包含枚举变量名称的情况下序列化枚举? - How do I serialize an enum without including the name of the enum variant? 在将值移入元组类型枚举变体后,如何轻松获取对值的引用? - How can I easily get a reference to a value after it has been moved into a tuple-type enum variant? 是否有一个宏可以用来期待枚举的变体并提取其数据? - Is there a macro I can use to expect a variant of an enum and extract its data? 如何传递枚举变量以匹配作为函数参数? - How do I pass an enum variant to match on as a function parameter? 如何将类型声明为“枚举变体之一或无”? - How do I declare a type as "one of an enum variant or a None"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM