#Translation of semantic ints

8 messages · Page 1 of 1 (latest)

severe temple
#

In C#, Enums are just semantic ints (or other integer-like types). In Rust however, they're (afaict) discriminated unions.

I'm building a set of microservices that require communication over websockets. I'm curious what's the best way to represent the semantic value of the numeric value (e.g. the C# enum) when it comes to deserializing the message in Rust?

#

For context:

public enum DispatchAction 
{
  Hello, // 0
  Identify, // 1
  Ready, // 2
  Disconnecting // 3
}
main cobalt
severe temple
#

serde seems to be bog standard, and some other libs I'm using use it so I'll definitely be doing that, thanks!

#

Oh huh I didn't even know you could do this with enum variants

main cobalt
#

Formally, you're setting the value of the discriminant, and IIRC you can do that for variants that aren't dataless too

#

(though this crate won't work with them anyway)

severe temple