Cloning DynamicStructs

55 views
Skip to first unread message

Vaci

unread,
Nov 18, 2021, 7:31:30 AM11/18/21
to Cap'n Proto
Hi,

Should it be possible to use capnp::clone() to copy a DynamicStruct? 

This code currently doesn't compile:

#include <capnp/dynamic.h>
#include <capnp/message.h>
#include "foo.capnp.h"
int main() {
  capnp::MallocMessageBuilder mb;
  auto root = mb.initRoot<Foo>();
  auto a = root.asReader();
  auto a2 = capnp::clone(a); // succeeds
  auto b = capnp::toDynamic(a);
  auto b2 = capnp::clone(b); // fails
}

...but I'm not clear what it is about DynamicStructs that make them less copyable?

Cheers,
Vaci



Kenton Varda

unread,
Nov 19, 2021, 2:39:51 PM11/19/21
to Vaci, Cap'n Proto
No, there's no fundamental limitation here. I think we just need an overload of `clone()` for this that accounts for the subtle API differences with `DynamicStruct`, e.g. passing the `Schema` in various places.

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/ce287ada-fee5-4bed-b353-10c438136d40n%40googlegroups.com.

Vaci

unread,
Jan 3, 2022, 8:15:02 AM1/3/22
to Cap'n Proto
I (eventually) figured out the very obvious solution -  just cast the DynamicStruct back to an AnyStruct, (which is clonable), and keep track of the schema elsewhere!
Reply all
Reply to author
Forward
0 new messages