rust
rust copied to clipboard
custom {de,}serialization
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
.
interesting... have to think about it
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.