cairo/
utils.rs

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{ffi::CStr, fmt};

use crate::{ffi, Error};

// rustdoc-stripper-ignore-next
/// Resets all static data within cairo to its original state (i.e. identical to the state at program
/// invocation). For example, all caches within cairo will be flushed empty.
///
/// # Safety
/// It is only safe to call this function when there are no active cairo objects remaining (all
/// cairo objects have been dropped).
///
/// This function is thread safe.
#[doc(alias = "cairo_debug_reset_static_data")]
pub unsafe fn debug_reset_static_data() {
    ffi::cairo_debug_reset_static_data()
}

pub fn status_to_result(status: ffi::cairo_status_t) -> Result<(), Error> {
    match status {
        ffi::STATUS_SUCCESS => Ok(()),
        err => Err(err.into()),
    }
}

#[doc(alias = "cairo_version_string")]
#[doc(alias = "get_version_string")]
pub fn version_string() -> &'static str {
    unsafe {
        let ptr = ffi::cairo_version_string();
        CStr::from_ptr(ptr)
            .to_str()
            .expect("invalid version string")
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct Version {
    major: u8,
    minor: u8,
    micro: u8,
}

impl Version {
    #[doc(alias = "cairo_version")]
    #[doc(alias = "get_version")]
    pub fn new() -> Version {
        let version = unsafe { ffi::cairo_version() };
        Version {
            major: (version / 10_000 % 100) as _,
            minor: (version / 100 % 100) as _,
            micro: (version % 100) as _,
        }
    }

    pub fn major(self) -> u8 {
        self.major
    }
    pub fn minor(self) -> u8 {
        self.minor
    }
    pub fn micro(self) -> u8 {
        self.micro
    }
}

impl Default for Version {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.micro)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn check_versions() {
        assert_eq!(version_string(), Version::new().to_string());
    }
}