Skip to content

impl_serde_for_symbol! serializes as usize but deserializes as underlying type #111

Description

@lemoncove

I don't have a totally complete understanding of how all this works, so take all this with a grain of salt.

Here, impl_serde_for_symbol! serializes as usize...

impl ::serde::Serialize for $crate::symbol::$name {
fn serialize<T: ::serde::Serializer>(
&self,
serializer: T,
) -> ::core::result::Result<T::Ok, T::Error> {
self.to_usize().serialize(serializer)

...but then deserializes as $ty...

impl<'de> ::serde::Deserialize<'de> for $crate::symbol::$name {
fn deserialize<D: ::serde::Deserializer<'de>>(
deserializer: D,
) -> ::core::result::Result<Self, D::Error> {
let index = <$ty as ::serde::Deserialize<'de>>::deserialize(deserializer)?;

...which, for SymbolU16 and SymbolU32 are u16 and u32. This is normally not a problem because in formats like JSON, a number is just a number. But when using binary-aware formats like bitcode, this results in a failure to deserialize unless using SymbolUsize, but of course a 64-bit integer is often going to be pretty overkill.

I think the solution is to deserialize as usize and then try Symbol::try_from_usize(). Not sure though.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions