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

Seamless serialization of existing Rust structs #182

Open
jmillan opened this issue Apr 20, 2023 · 6 comments
Open

Seamless serialization of existing Rust structs #182

jmillan opened this issue Apr 20, 2023 · 6 comments

Comments

@jmillan
Copy link

jmillan commented Apr 20, 2023

Having an existing Rust struct which is also represented in the flatbuffers schema and thus in the autogenerated code.., is there any derive or traits we can implement in our struct so it can seamlessly serialize it into the builder, rather than having to manually convert from our struct to the one created by the autogenerated code?

Imagine a simple struct like:

struct DominantSpeakerNotification {
    producer_id: ProducerId,
}

Which has an analogous flatbuffers table which leads to the autogenerated code:

pub struct DominantSpeakerNotification {
    pub producer_id: ::planus::alloc::string::String,
}

Is there any derive or trait we could implement to serialize our DominantSpeakerNotification into the builder?

@TethysSvensson
Copy link
Collaborator

We could probably implement this, but I am very hesitant to implement custom attributes if we can avoid it.

Depending on your performance and organizational requirements, some of these work-arounds might suit your particular needs:

  • Use the type generated by planus through-out your code and get rid of your own type
  • Implement From and Into between the types
  • Ignore the generated DominantSpeakerNotification and exclusively use the DominantSpeakerNotificationRef and DominantSpeakerNotificationBuilder types.

At my work we use a combination of these approaches, though we try to use the first one as much as possible.

If these work-arounds does not work for your use-case, could you expand a bit more upon what you need and why?

I'd love to support as many use-cases as possible, but I don't think I will add features without understanding the situations where those features are needed.

@nazar-pc
Copy link

nazar-pc commented Apr 20, 2023

Big challenge with Planus (and Flatbuffers in general) is that generated types do not contain documentation and do not allow us to have more fine-grained type. For instance, above ProducerId is actually struct ProducerId(::uuid::Uuid) with serde::{Seriaize,Deserialize} and some other stuff derived.

On Flatbuffers level right now it is just String, which is both heap-allocated and does not have any semantic meaning to it.

With Planus generated data structures end up being something like Box<HashMap<String, Box<HashSet<String>>>, which is A LOT of unnecessary allocations.

@TethysSvensson
Copy link
Collaborator

@nazar-pc We support docstrings in planus, which mitigates the issue you mentioned a bit. However you are right that we do not allow the level of documentation you would get otherwise.

@TethysSvensson
Copy link
Collaborator

Regarding the heap allocations, I would suggest that you use a [byte: 16] field, however planus does not currently support arrays. We would definitely be open to changing that though.

@jmillan
Copy link
Author

jmillan commented Apr 20, 2023

exclusively use the DominantSpeakerNotificationRef and DominantSpeakerNotificationBuilder types

There is no DominantSpeakerNotificationBuilder type in the created body. Do you mean using DominiantSpeakerNotification::create() method?

@TethysSvensson
Copy link
Collaborator

We should really get around to making a new release...

On the current main branch there is a builder 😊

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

3 participants