I'm trying to implement Serialize
and Deserialize
for an external enum and I have no idea how. It has From<u64>
so all I want to do is just have that object serialize with that.
#[derive(Serialize, Deserialize)]
pub struct ImageBinds {
binds: HashMap<KeybdKey, Vec<u8>>, // KeybdKey doesn't implement serialize
}
// won't compile with this:
// only traits defined in the current crate can be implemented for arbitrary types
// impl doesn't use only types from inside the current crate
// I understand the orphan rule and all that but I have no idea how else I'd implement it
// I've looked at serde remote stuff and I cannot work out how to do it with an enum without
// literally just copying the whole crate into my scope which is stupid
impl Serialize for KeybdKey {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u64(u64::from(*self))
}
}
KeybdKey
is from the inputbot
crate.
HashMap
. You either need a custom deserialize for theHashMap
(i.e.,with
attribute), a wrapper type, or you try theserde_with
crate. The documentation only mentionsFrom<KeybdKey> for u64
but not aFrom<u64>
. So this will only allow you to write the serialization code, but not the deserialization. – Uta