capnproto / capnpc-rust

Cap'n Proto code generation for Rust
75 stars 26 forks source link

How to build a list inside an union? #32

Closed novacrazy closed 7 years ago

novacrazy commented 7 years ago

I have in my capnp files an union that is akin to Rust's Option type:

struct Option(SomeType) {
    union {
        none @0: Void;
        some @1: SomeType;
    }
}

And I use it to store a List like:

struct Vertices {
    positions @0: List(Math.Point3);
    normals @1: Util.Option(List(Math.Vector3));
    uvs @2: Util.Option(List(TexCoord));
}

And the generated documentation looks like:

option doc

However, it seems that the initn_some method doesn't do anything, as when I try to read back in the data it shows up as None.

You can see Here how I am initializing the builder and attempting to copy the data into it.

And it executes just fine, and all data but the Option(List(_)) types appear to save perfectly.

So is it a bug that Lists inside union types cannot be properly initialized or am I doing it wrong?

Also, set_vector is defined in this file. It's nothing much.

An alternative could be a way to create a list builder without a parent somehow? Then I could just use set_some, but I don't see an obvious way of doing that.

novacrazy commented 7 years ago

I suppose I could have opened this in the runtime library repo, but since this more has to do with the generated Builder code, eh.

dwrensha commented 7 years ago

Thanks for the report! This should be fixed in https://github.com/dwrensha/capnpc-rust/commit/c248530330162d1876e38f99bb85c70b7a7015a8.

An alternative could be a way to create a list builder without a parent somehow? Then I could just use set_some, but I don't see an obvious way of doing that.

Yeah, this would require orphans: https://github.com/dwrensha/capnproto-rust/issues/51

novacrazy commented 7 years ago

Thanks for the quick fix! Everything seems to be working now.