serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
8.84k stars 748 forks source link

impl Serialize and Deserialize for ManuallyDrop #1507

Open DrSensor opened 5 years ago

DrSensor commented 5 years ago

Hi, any plan to implement Serialize and Deserialize for ManuallyDrop? I happen to bump into a case when I need the compiler to not automatically drop on a certain function.

#[derive(Default, Serialize, Deserialize)]
pub struct Machine<'a> {
    #[serde(flatten)]
    schema: StateChart, // it should be 👇
    // schema: mem::ManuallyDrop<StateChart>,

    #[serde(skip)]
    builder: Scdlang<'a>,
}

impl<'a> Parser<'a> for Machine<'a> {
    fn parse(&mut self, source: &str) -> Result<(), DynError> {
        let ast = Self::try_parse(source, self.builder.to_owned())?;
        self.schema.states = ast.schema.states.to_owned(); //âš  expensive clone
        // self.schema.states = ast.schema.states; //👈 with ManuallyDrop
        Ok(()
    }
}

impl Drop for Machine<'_> {
    fn drop(&mut self) {
        self.clear_cache().expect("Deadlock");
    }
}
dtolnay commented 5 years ago

I would accept a PR to add the impls.

DrSensor commented 5 years ago

@dtolnay, could you give a guide where it should be implemented?