Giter VIP home page Giter VIP logo

capnpc-rust's Introduction

THIS IS THE V2 DEVELOPMENT BRANCH

On this branch, we may make breaking changes to the API at any time. Do not use this branch if you want stability. If you want "1.0 plus bug fixes", use the master branch.

For more, see the 1.0 release announcement:

https://capnproto.org/news/2023-07-28-capnproto-1.0.html


Cap'n Proto is an insanely fast data interchange format and capability-based RPC system. Think JSON, except binary. Or think Protocol Buffers, except faster. In fact, in benchmarks, Cap'n Proto is INFINITY TIMES faster than Protocol Buffers.

Read more...

capnpc-rust's People

Contributors

aochagavia avatar bozaro avatar bspeice avatar danburkert avatar dfaust avatar dwrensha avatar ivan avatar kali avatar maurer avatar sprial404 avatar stebalien avatar tynril avatar vks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

capnpc-rust's Issues

shared memory implementation - need help

Hello,

I'm trying to use Capn Proto over a shared memory transport using a custom allocator.
I get errors when constructing a message from the data Capn Proto wrote to the allocator:

  • Error 1: Header claimed 4 words, but message has 3 words
  • Error 2: message contained out-of-bounds pointer

Using this code to read a message from the shared memory allocator:
(All I have is a pointer to the message, and how many bytes the message is)

            println!("shm start memory address: {:p}, message bytes: {}", start, bytes_length);
            let words: &[Word] = unsafe {from_raw_parts(start as *const Word, bytes_length / 8)};
            match serialize::read_message_from_words(words, ReaderOptions::new()) {
                Ok(slice_segments) => {
                    match slice_segments.get_root::<point::Reader>()
  • I get Error 1 if the allocator doesn't specify the word size for the message segment exactly.
    This doesn't smell right because the header should specify how many words are required..???

  • I get Error 2 if I hard code the allocator word size correctly and call get_root::point::Reader()

It smells like I need to call some function after I'm done creating my message that updates the segment header - and everything would magically work after that.

Any hints on how to get this working would be greatly appreciated.

Thanks!

How to get to Builder from Reader?

I receive messages from network, get a reader using read_message.
Now I'd like to inspect some fields and send it further without copying.
Is it possible?

List(Interface) is unsupported

When compiling this schema:

@0xffffffffffffffff;

interface Crop {
  harvest @0 () -> (food: Food);
}

struct Food {
  yumminess @0: UInt8;
}

struct Field {
  crops @0: List(Crop);
}

with the command capnp compile -orust schema.capnp, I get the output:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Failed, description: "List(Interface) is unsupported" }', /checkout/src/libcore/result.rs:916:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.
rust: plugin failed: exit code 101

I expect it to compile successfully. capnp compile -oc++ schema.capnp does compile successfully.

generics in superclass can cause compilation failure

The following schema produces Rust code that fails to typecheck:

@0xcb429469dd078275;
interface GenericBase(T) {}
interface GenericExtend extends(GenericBase(Data)) {}

The error given by rustc is:

 |   pub trait Server<> : ::test_capnp::generic_base::Server {
 |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 type argument, found 0

Importing schemas from other crates is awkward

If I write a sandstorm app using the low level cap'n proto API and I want to write some of my own schemas that refer to the sandstorm types, it currently does't work to just use https://crates.io/crates/sandstorm , because capnpc-rust has no awareness of cross-crate boundaries. Probably we need to use an annotation similar to https://github.com/sandstorm-io/capnproto/blob/2b8cde72a49492d26ac9809b490959b590a3cc7f/c%2B%2B/src/capnp/c%2B%2B.capnp#L25 .

The example just hangs

For some reason, running the example just hangs on p.wait(). Running the commands manually doesn't seem to cause a problem. I'm on rustc 1.0.0-nightly (2fc8b1e7c 2015-03-07) (built 2015-03-08).

When a schema is in a subdirectory generate fails to create output directory

When a capnp file is in a directory the generator does not correctly create the subdirectory in OUT.

In short this will fail:

extern crate capnpc;

fn main() {
    ::capnpc::CompilerCommand::new()
        .file("schema/process.capnp")
        .run()
        .expect("compiling");
}

The reason it fails is that the code in codegen.rs creates the directory $PWD/schema instead of the correct $OUT/schema.

Premature EOF Error when running the example.

Hi, thanks for the great work. I'm having this issue when running the example on windows, rust nightly 01/01/2016

Process didn't exit successfully: `D:\Projects\lobby-rs\target\debug\build\lobby-rs-c7d7991a1fa58573\build-script-build` (exit code: 101)
--- stderr
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Error { repr: Custom(Custom { kind: Other, error: StringError("Premature EOF") }) })', ../src/libcore\result.rs:688
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Error { repr: Custom(Custom { kind: Other, error: StringError("Premature EOF") }) })', ../src/libcore\result.rs:688

EDIT: this issue appears when you have capnp in the PATH, in my case it was due to using "cargo install capnpc"

Compiling the example fails with "Premature EOF"

After cloning this repository and cding into the examples/addressbook directory

cargo build --release                                                                                                                                                    
   Compiling byteorder v0.4.2
   Compiling capnp v0.6.2
   Compiling capnpc v0.6.0 (file:///PATH/capnpc-rust)
   Compiling addressbook v0.0.0 (file:///PATHt/capnpc-rust/example/addressbook)
       error failed to run custom build command for `addressbook v0.0.0 (file://PATH/capnpc-rust/example/addressbook)`
Process didn't exit successfully: `PATH/capnpc-rust/example/addressbook/target/release/build/addressbook-202ecf71d1cf3aa5/build-script-build` (exit code: 101)
--- stderr
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Failed, description: "Premature EOF" }', ../src/libcore/result.rs:746
note: Run with `RUST_BACKTRACE=1` for a backtrace.
thread '<main>' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Failed, description: "Premature EOF" }', ../src/libcore/result.rs:746
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Doing a manual

capnp-rust compile -orust:out --src-prefix=. addressbook.capnp

where capnp-rust is the bin compile from this crate, just never terminates. Am I doing something wrong?

Do I need the capnp binary from the website, to compule the scheme files?

Trouble accessing from non-top level?

Was playing with this and had the following file structure:

├── Cargo.lock
├── Cargo.toml
├── src
│   ├── main.rs
│   └── schema
│       ├── person.capnp
│       └── person_capnp.rs

Where person_capnp.rs is generated via capnp compile -o rust src/schema/*.capnp.

I was hoping to include a schema/mod.rs which re-exported the person in person_capnp.rs.

schema/mod.rs:

mod person_capnp {
    include!("./person_capnp.rs");
}

main.rs:

#![feature(path)]

extern crate capnp;
extern crate capnpc;

mod schema;

fn main() {

}

Results in this error:

capntest git:(master) ✗ cargo build
   Compiling capntest v0.0.1 (file:///Users/hoverbear/git/capntest)
src/schema/person_capnp.rs:60:55: 60:67 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:60     pub fn get_phones(self) -> struct_list::Reader<'a,::person_capnp::person::phone_number::Reader<'a>> {
                                                                                    ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:60:55: 60:103 error: use of undeclared type name `person_capnp::person::phone_number::Reader`
src/schema/person_capnp.rs:60     pub fn get_phones(self) -> struct_list::Reader<'a,::person_capnp::person::phone_number::Reader<'a>> {
                                                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:67:35: 67:47 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:67     pub fn get_birthdate(self) -> ::person_capnp::date::Reader<'a> {
                                                                ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:67:35: 67:67 error: use of undeclared type name `person_capnp::date::Reader`
src/schema/person_capnp.rs:67     pub fn get_birthdate(self) -> ::person_capnp::date::Reader<'a> {
                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:149:56: 149:68 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:149     pub fn get_phones(self) -> struct_list::Builder<'a,::person_capnp::person::phone_number::Builder<'a>> {
                                                                                      ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:149:56: 149:105 error: use of undeclared type name `person_capnp::person::phone_number::Builder`
src/schema/person_capnp.rs:149     pub fn get_phones(self) -> struct_list::Builder<'a,::person_capnp::person::phone_number::Builder<'a>> {
                                                                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:153:65: 153:77 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:153     pub fn set_phones(&mut self, value : struct_list::Reader<'a,::person_capnp::person::phone_number::Reader<'a>>) {
                                                                                               ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:153:65: 153:113 error: use of undeclared type name `person_capnp::person::phone_number::Reader`
src/schema/person_capnp.rs:153     pub fn set_phones(&mut self, value : struct_list::Reader<'a,::person_capnp::person::phone_number::Reader<'a>>) {
                                                                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:157:69: 157:81 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:157     pub fn init_phones(self, size : u32) -> struct_list::Builder<'a,::person_capnp::person::phone_number::Builder<'a>> {
                                                                                                   ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:157:69: 157:118 error: use of undeclared type name `person_capnp::person::phone_number::Builder`
src/schema/person_capnp.rs:157     pub fn init_phones(self, size : u32) -> struct_list::Builder<'a,::person_capnp::person::phone_number::Builder<'a>> {
                                                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:164:35: 164:47 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:164     pub fn get_birthdate(self) -> ::person_capnp::date::Builder<'a> {
                                                                 ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:164:35: 164:68 error: use of undeclared type name `person_capnp::date::Builder`
src/schema/person_capnp.rs:164     pub fn get_birthdate(self) -> ::person_capnp::date::Builder<'a> {
                                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:168:45: 168:57 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:168     pub fn set_birthdate(&mut self, value : ::person_capnp::date::Reader) {
                                                                           ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:168:45: 168:73 error: use of undeclared type name `person_capnp::date::Reader`
src/schema/person_capnp.rs:168     pub fn set_birthdate(&mut self, value : ::person_capnp::date::Reader) {
                                                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:172:38: 172:50 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:172     pub fn init_birthdate(self, ) -> ::person_capnp::date::Builder<'a> {
                                                                    ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:172:38: 172:71 error: use of undeclared type name `person_capnp::date::Builder`
src/schema/person_capnp.rs:172     pub fn init_birthdate(self, ) -> ::person_capnp::date::Builder<'a> {
                                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:187:36: 187:48 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:187     pub fn get_birthdate(&self) -> ::person_capnp::date::Pipeline {
                                                                  ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:187:36: 187:66 error: use of undeclared type name `person_capnp::date::Pipeline`
src/schema/person_capnp.rs:187     pub fn get_birthdate(&self) -> ::person_capnp::date::Pipeline {
                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:244:39: 244:51 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:244       pub fn get_type(self) -> Option<::person_capnp::person::phone_number::Type> {
                                                                     ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:244:39: 244:81 error: use of undeclared type name `person_capnp::person::phone_number::Type`
src/schema/person_capnp.rs:244       pub fn get_type(self) -> Option<::person_capnp::person::phone_number::Type> {
                                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:308:39: 308:51 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:308       pub fn get_type(self) -> Option<::person_capnp::person::phone_number::Type> {
                                                                     ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:308:39: 308:81 error: use of undeclared type name `person_capnp::person::phone_number::Type`
src/schema/person_capnp.rs:308       pub fn get_type(self) -> Option<::person_capnp::person::phone_number::Type> {
                                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:312:42: 312:54 error: failed to resolve. Maybe a missing `extern crate person_capnp`?
src/schema/person_capnp.rs:312       pub fn set_type(&mut self, value : ::person_capnp::person::phone_number::Type) {
                                                                        ^~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
src/schema/person_capnp.rs:312:42: 312:84 error: use of undeclared type name `person_capnp::person::phone_number::Type`
src/schema/person_capnp.rs:312       pub fn set_type(&mut self, value : ::person_capnp::person::phone_number::Type) {
                                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of include!
src/schema/mod.rs:2:5: 2:35 note: expansion site
error: aborting due to 24 previous errors
Could not compile `capntest`.

Fixing

Removing schema/mod.rs and making main.rs:

#![feature(path)]

extern crate capnp;
extern crate capnpc;

mod person_capnp {
    include!("./schema/person_capnp.rs");
}

use person_capnp::person;

fn main() {

}

Seems to resolve the issue.

This seems to be related to capnproto/capnproto-rust#16

Making type IDs accessible from Rust code

The current generated code doesn't seem to include the "Unique IDs" of the struct / enum / interface / annotation.
Could we add something like a const TYPE_ID: u64 = id in every relevant mod ?

Also, it would probably be difficult to keep an interface similar to the C++ one capnp::typeId<CapnprotoType>();: as a capnproto type is defined as a module, I don't think it is possible to take it as a parameter in a function or make it implement a HasTypeId trait (although it should be possible for a Reader or Builder)

Need more info in log

capnpc-rust was calling panic!(), but I couldn't see what file it was trying to open.
I think this patch will help others.

$ git diff
diff --git a/src/codegen.rs b/src/codegen.rs
index 16c9be7..93c8f11 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -1596,7 +1596,7 @@ pub fn main(mut inp : T) -> ::std::io::Result<()>
use std::io::Write;
try!(writer.write_all(text.as_bytes()));
}

  •        Err(e) => {panic!("could not open file for writing: {}", e)}
    
  •        Err(e) => {panic!("could not open file {:?} for writing: {}", filepath, e)}
     }
    
    }
    Ok(())

Serde support

C++ capnproto implementation supports json serialization. Would you be interested in adding support for serde? I think a Serialize impl could be generated by capnpc.

capnpc panics when function argument contains an underscore

Rust capnpc (ver 0.8.5) panics without a helpful message (in particular, no file name or line number) when a method parameter in an interface contains '_' (which is not allowed in capnp). Underscores in other names (methods, structs, ...) have a nice error message.

Minimal example:

@0xca18754725ac2b4c;
interface Foo {
    bar @0 (qu_uq :Int32) -> ();
}

Error mentions '_' but not the location (and a panic is not a nice result in any case):

thread 'main' panicked at 'not alphanumeric '_', i.e. 95', /home/gavento/.cargo/registry/src/github.com-1ecc6299db9ec823/capnpc-0.8.5/src/codegen.rs:115:8

Full trace: https://gist.github.com/gavento/9526b6b5971816c71041b2946e7a0cda

from FromPointerBuilder issue

Since moving over to v0.7.0 we've been getting this error.

The contract:

@0xbde554c96bf60f36;

struct MathsBoolean {
  boolean @0 :Bool;
}

Some code for a NAND logic gate:

  fn run(&mut self) -> Result<()> {
    let a = {
        let mut ip_a = try!(self.ports.recv("a"));
        let a_reader: maths_boolean::Reader = try!(ip_a.get_root());
        a_reader.get_boolean()
    };
    let b = {
        let mut ip_b = try!(self.ports.recv("b"));
        let b_reader: maths_boolean::Reader = try!(ip_b.get_root());
        b_reader.get_boolean()
    };

    let mut new_ip = IP::new();
    let mut boolean = new_ip.init_root::<maths_boolean::Builder>();
    boolean.set_boolean(if a == true && b == true {false} else {true});
    try!(self.ports.send("output", new_ip));
    Ok(())
  }

The error message:

   Compiling component v0.1.0 (file:///tmp/nix-build-maths_boolean_nand.drv-0/nand)
src/lib.rs:22:57: 22:65 error: the trait `capnp::traits::FromPointerReader<'_>` is not implemented for the type `contract_capnp::maths_boolean::Reader<'_>` [E0277]
src/lib.rs:22         let a_reader: maths_boolean::Reader = try!(ip_a.get_root());
                                                                      ^~~~~~~~
src/lib.rs:22:47: 22:68 note: in this expansion of try! (defined in <std macros>)
src/lib.rs:11:1: 37:2 note: in this expansion of component! (defined in <rustfbp macros>)
src/lib.rs:22:57: 22:65 help: run `rustc --explain E0277` to see a detailed explanation
src/lib.rs:27:57: 27:65 error: the trait `capnp::traits::FromPointerReader<'_>` is not implemented for the type `contract_capnp::maths_boolean::Reader<'_>` [E0277]
src/lib.rs:27         let b_reader: maths_boolean::Reader = try!(ip_b.get_root());
                                                                      ^~~~~~~~
src/lib.rs:27:47: 27:68 note: in this expansion of try! (defined in <std macros>)
src/lib.rs:11:1: 37:2 note: in this expansion of component! (defined in <rustfbp macros>)
src/lib.rs:27:57: 27:65 help: run `rustc --explain E0277` to see a detailed explanation
src/lib.rs:32:30: 32:39 error: the trait `capnp::traits::FromPointerBuilder<'_>` is not implemented for the type `contract_capnp::maths_boolean::Builder<'_>` [E0277]
src/lib.rs:32     let mut boolean = new_ip.init_root::<maths_boolean::Builder>();
                                           ^~~~~~~~~
src/lib.rs:11:1: 37:2 note: in this expansion of component! (defined in <rustfbp macros>)
src/lib.rs:32:30: 32:39 help: run `rustc --explain E0277` to see a detailed explanation
error: aborting due to 3 previous errors
Could not compile `component`.

Translate the comments into the Rust documentation?

I've just found that rustdoc can easily generate the docummentation for the code produced by the Cap'n Proto compiler. That's very useful, especially for a beginner like me who isn't familiar with the conventions used in the generated stubs.

One thing I'd like to see is the # comments documentation from the Cap'n Proto scheme files copied into rustdoc /// comments so that the generated rustdoc documentation contains the explanations from the schema.

support Cap'n Proto generics

What I'm referring to is the fact that Map(Key,Value) is translated to a pair of unparameterized rust type (one builder, one reader), using "any_pointer". It makes it very easy to generate invalid data. I think it would be much nicer to parameterize the Map Builder and Reader so that the right Builder and Reader. It would probably enhance the API safety, and client code readability.

I suspect this is by-design, but I find it very disturbing when working in rust with its very strict typesystem.

Trying to return a union - Help needed

Hi,

I have this schema:

struct Square {
  width @0 :Int64;
  height @1 :Text;
}

struct Circle {
  radius @0 :Int64;
}

struct Shape {
   union {
    square @0 :Square;
    circle @1 :Circle;
  }
}

I am trying to write a function that returns a mutable square or circle but don't want the caller to know about shape (not even sure it's possible in rust, because of ownership).

I tried:

pub fn get_square() -> ?? {
    let mut message = capnp::message::Builder::new_default();
    let mut message2 = capnp::message::Builder::new_default();
    let mut shape = message2.init_root::<Shape::Builder>();
    {
        let square = message.init_root::<Square::Builder>();
        shape.set_square(square.borrow_as_reader());
    }
   // How to return square ?? Is it possible ?
}

What I am trying to achieve is, having a module that expose several functions that let me create a square or circle, but without knowing about the internal parts of capnp.

How to build a list inside an union?

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.