2 ms·
I haven't actually gotten a chance to use Rust yet, but would something like this work? #[derive(RustcDecodable, RustcEncodable)] enum Msg { Ba
by wtetzner 11y ago
I haven't actually gotten a chance to use Rust yet, but would something like this work?
#[derive(RustcDecodable, RustcEncodable)]
enum Msg {
Basic { cmd: String, id: i32 },
Positioned { cmd: String, id: i32, x: i32, y: i32 },
Speed { cmd: String, id: i32, speed: i32 },
Full { cmd: String, id: i32, x: i32, y: i32, speed: i32 }
}
That would allow you to prevent weird cases like x being provided but y not being provided.
- drbawb 11y agoYup, you can encode enums just fine. ADTs get encoded with an explicit `variant` tag at the front, and then an array of field values. For e.g: { "variant": "Basic", fields: ["cmd-value", 42] } { "variant": "Positioned", fields: ["cmd-value", 42, 0, 0] } //etc ...