#Borrowing issue with creating a custom serializer

3 messages · Page 1 of 1 (latest)

dire ocean
#

i'm writing a custom serializer with serde and i have an issue where the serializer is consumed early on. how can i fix the borrowing error here?

fn serialize_some<T>(self, value: &T) -> Result<Self::Ok>
    where
        T: ?Sized + Serialize {
    value.serialize(self)?;
    self.output.push_u1(1)?;
    Ok(())
}
``` ```rs
error[E0382]: borrow of moved value: `self`
  --> squash\src\lib.rs:90:9
   |
86 |     fn serialize_some<T>(self, value: &T) -> Result<Self::Ok>
   |                          ---- move occurs because `self` has type `&mut Serializer`, which does not implement the `Copy` trait
...
89 |         value.serialize(self)?;
   |                         ---- value moved here
90 |         self.output.push_u1(1)?;
   |         ^^^^^^^^^^^ value borrowed here after move

For more information about this error, try `rustc --explain E0382`.
error: could not compile `squash` (lib) due to 1 previous error
#

it's weird since the signature of serialize accepts &self, but self is consumed or something

final badge
#

Try calling it with &mut *self