rust icon indicating copy to clipboard operation
rust copied to clipboard

custom {de,}serialization

Open phi-gamma opened this issue 3 years ago • 2 comments

AFAICS the generator will always define a native Rust struct and offload the derivation of Deserialize and Serialize to Serde. Often it would be convenient if custom serialization could be made to work for Varlink types. The motivation is mainly to prevent extra copies / conversions when working with FFI.

IOW currently the path between JSON and opaque FFI type is JSON ←→ Rust struct ←→ FFI struct; ideally it would be just JSON ←→ FFI struct.

Say I have an interface

interface bar.foo
type Foo (
    one: string,
    two: int
)
method GetFoo() -> (foo: Foo)

Varlink will define a struct Foo containing those fields and a trait Call_GetFoo that handles such a struct. Now I’m actually converting the struct directly into some opaque FFI type:

#[repr(C)] pub struct FFI_FOO { _private: [u8; 0], }

discarding the Rust struct immediately after.

I’m looking for a way of hooking into the varlink generator so it defines an alternative to Call_GetFoo that instead uses the newtype FFI_FOO_PTR:

struct FFI_FOO_PTR (*mut FFI_FOO);

which has implementations for Deserialize and Serialize.

phi-gamma avatar Mar 15 '21 15:03 phi-gamma

interesting... have to think about it

haraldh avatar Mar 15 '21 15:03 haraldh

Cool. Another situation where assigning custom types would be beneficial is when you have a &str or Cow<'a, str> and want to serialize those into a varlink string without creating an owned String. That probably only works in one direction though.

phi-gamma avatar Mar 15 '21 15:03 phi-gamma