#How to serialize built-in struct that with Serde crate?

9 messages · Page 1 of 1 (latest)

summer plover
#

I have a struct that contains a few members, one of which is a File struct, and when I use #[derive(Serialize, Deserialize)] the compiler tells me that File does not implement ```#[derive(Serialize, Deserialize)]

violet cosmos
#

No, you cannot make File serializable, due to Rust's orphan rule. Roughly, you can only implement a trait for a type, if your crate either defines the trait or the type. serde has the option to do a remote derive https://serde.rs/remote-derive.html which uses the with attribute to hook up custom serialization code https://serde.rs/field-attrs.html#with What do you expect a serialized File to look like? You can also skip the field with the skip attribute.

summer plover
#

I am building my own version of Git in Rust, and the idea is that I need to serialize the struct above to store it in a file, and deserialize it back whenever I need it to get the original struct. I have no idea how to idea how to do it as easily as in Java

hardy dust
#

Are you trying to store the bytes of a file inside another file?

summer plover
#

Yeah, the git system will serialize the files changed in each commit, and store the serialized bytes in another file

hardy dust
#

I'm with jonas wondering what you believe a serialized File type, or serialized file at all truly, would look like

summer plover
#

Well... This project is inspired by a course project that I found online, and they did it in Java but I want to do it in Rust. Perhaps I should spend more time reading the doc for the project