summaryrefslogtreecommitdiff
path: root/rust-bindings/src/sysroot_write_deployments_opts.rs
blob: 0c68e764aa2b1d2c44a609b7aec24f4428245797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use ffi::OstreeSysrootWriteDeploymentsOpts;
use glib::translate::*;

/// Options for writing a deployment.
#[derive(Default)]
pub struct SysrootWriteDeploymentsOpts {
    /// Perform cleanup after writing the deployment.
    pub do_postclean: bool,
}

impl<'a> ToGlibPtr<'a, *const OstreeSysrootWriteDeploymentsOpts> for SysrootWriteDeploymentsOpts {
    type Storage = Box<OstreeSysrootWriteDeploymentsOpts>;

    fn to_glib_none(&'a self) -> Stash<*const OstreeSysrootWriteDeploymentsOpts, Self> {
        // Creating this struct from zeroed memory is fine since it's `repr(C)` and only contains
        // primitive types.
        // The struct needs to be boxed so the pointer we return remains valid even as the Stash is
        // moved around.
        let mut options =
            Box::new(unsafe { std::mem::zeroed::<OstreeSysrootWriteDeploymentsOpts>() });
        options.do_postclean = self.do_postclean.into_glib();
        Stash(options.as_ref(), options)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use glib::ffi::{GFALSE, GTRUE};

    #[test]
    fn should_convert_default_options() {
        let options = SysrootWriteDeploymentsOpts::default();
        let stash = options.to_glib_none();
        let ptr = stash.0;
        unsafe {
            assert_eq!((*ptr).do_postclean, GFALSE);
        }
    }

    #[test]
    fn should_convert_non_default_options() {
        let options = SysrootWriteDeploymentsOpts { do_postclean: true };
        let stash = options.to_glib_none();
        let ptr = stash.0;
        unsafe {
            assert_eq!((*ptr).do_postclean, GTRUE);
        }
    }
}