Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom {de,}serialization #53

Open
phi-gamma opened this issue Mar 15, 2021 · 2 comments
Open

custom {de,}serialization #53

phi-gamma opened this issue Mar 15, 2021 · 2 comments

Comments

@phi-gamma
Copy link

phi-gamma commented Mar 15, 2021

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.

@haraldh
Copy link
Collaborator

haraldh commented Mar 15, 2021

interesting... have to think about it

@phi-gamma
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants