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

Write example for serializing external types using wrapper types #446

Open
ezkangaroo opened this issue Dec 11, 2023 · 1 comment
Open
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@ezkangaroo
Copy link

Hello, I'm trying to use rkvy in a struct which has custom type coming from external library, which provides custom serialize/deserialize, but not sure how to wire them. Here is example Code.

#[derive(Clone, Serialize, Deserialize, PartialEq, Archive)]
pub struct Store {
   pub id: i32,

   pub looker: SomeCustomLooker // coming from external library <- how to use custom functions here??
}

As it happens, external library is providing this functions:

serialize(val: SomeCustomLooker) -> Vec<u8>
deserialize(val: Vec<u8>) -> Result<SomeCustomLooker, ....>

In serde and bincode, I can do

#[derive(Clone, Serialize, Deserialize, PartialEq)]
pub struct Store {
   pub id: i32,

    #[serde(
        serialize_with = "serialize_looker",
        deserialize_with = "deserialize_looker"
    )]
   pub looker: SomeCustomLooker // coming from external library
}

but not sure, how to tackle this with rkvy.

@ezkangaroo ezkangaroo changed the title How to perform custom serialization with custom struct How to use custom serializer function for types coming from external library Dec 11, 2023
@djkoloski
Copy link
Collaborator

rkyv has a related but different system for serializing external types called wrapper types. The book documentation is somewhat sparse, so I encourage you to read the example for ArchiveWith. It's basically a way to let you manually write impls for external types as long as you add an attribute.

@djkoloski djkoloski added the documentation Improvements or additions to documentation label Jan 10, 2024
@djkoloski djkoloski changed the title How to use custom serializer function for types coming from external library Write example for serializing external types using wrapper types Mar 9, 2024
@djkoloski djkoloski added this to the v0.8 milestone Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants