Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Yup, 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 ...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: