#optional #proc-macro #serde

macro empty_type_derive

Converting between types and their optional counterparts

5 releases

0.2.3 Jul 7, 2022
0.2.2 Jun 22, 2022
0.2.1 Jun 22, 2022
0.2.0 Jun 22, 2022
0.1.0 Jun 21, 2022

#24 in #procmacro


Used in 4 crates (via empty_type)

Apache-2.0

39KB
852 lines

EmptyType Derive Macro

Used to derive a corresponding Container for an EmptyType implementation. Will roughly produce equivalent code to:

use empty_type::{EmptyType, Container};

struct Data {
    key: String
}

#[derive(Default)]
struct OptionalData {
   key: Option<String>
}

impl EmptyType for Data {
    type Container = OptionalData;
    
    fn new_container() -> Self::Container {
        OptionalData {
            key: None
        }       
    }
}

impl Container for OptionalData {
    type Value = Data;
    
    fn try_open(&mut self) -> Result<Self::Value, Box<dyn std::error::Error>> {
        Ok(Data {
            key: self.key.open()
        })
    }
}

Dependencies

~1.5MB
~38K SLoC