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.
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...string-interner/src/serde_impl.rs
Lines 91 to 96 in a8be633
...but then deserializes as
$ty...string-interner/src/serde_impl.rs
Lines 100 to 104 in a8be633
...which, for
SymbolU16andSymbolU32areu16andu32. 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 usingSymbolUsize, but of course a 64-bit integer is often going to be pretty overkill.I think the solution is to deserialize as
usizeand then trySymbol::try_from_usize(). Not sure though.