Using generics in interfaces

24 views
Skip to first unread message

Karel Kubat

unread,
May 9, 2020, 5:15:09 PM5/9/20
to Cap'n Proto
I'm looking at a solution to 'embedding' python and javascript into a Rust application through RPC, my basic schema looks something like:

@0xc01ea2259855b1a4;

interface Executor {
ping @0 () -> (result :Bool);
getFunction @1 [I, O] (hashId :FunctionDescriptor) -> (fn :Function(I, O));
}

struct FunctionDescriptor {
id @0 :Text;
hash @1 :Data;
lang @2 :Language;
}

enum Language {
python @0;
javascript @1;
}

interface Function(I, O) {
call @0 (input :I) -> (output :O);
}

The compiler will happily generate rust code for me; although it seems to me I need to somehow constrain the generic I and O. In my model, a Rust client will call a Python Executor, attempt to acquire a concrete function, which might fail if the python server cannot provide that specific concrete implementation.

Should I use generics in this way; or use AnyPointers?

Karel Kubat

unread,
May 9, 2020, 5:24:40 PM5/9/20
to Cap'n Proto
As a side note; I'd prefer to fail when the getFunction is called with unsupported I O types; instead of failing when the pointer is obtained, but 'call' is called with invalid types.

Kenton Varda

unread,
May 11, 2020, 11:33:30 AM5/11/20
to Karel Kubat, Cap'n Proto
Hi Karel,

Cap'n Proto generics are erasure-based, meaning they are used only to guide the type checker, and are "erased" before runtime. In particular, the type information is not encoded on the wire. So, no error will be detected when you call getFunction() with the wrong types; the remote side doesn't know what types you used. In fact, even when you invoke the function, the remote side still won't know if you're using the wrong types. It's very likely that the callee will end up getting garbage input without noticing the type error.

To detect type mismatches, you'll need to explicitly transmit some description of the types and check them at the other end. For example, you could pass the type IDs of the input and output structs (if they are structs).

-Kenton

On Sat, May 9, 2020 at 4:24 PM Karel Kubat <k.l....@gmail.com> wrote:
As a side note; I'd prefer to fail when the getFunction is called with unsupported I O types; instead of failing when the pointer is obtained, but 'call' is called with invalid types.

--
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/38aecad0-6a47-40f1-bcb0-eb03167d2bc6%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages