cairo/
utils.rs

1// Copyright 2013-2018, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use ffi;
6use std::ffi::CStr;
7use std::fmt;
8
9pub unsafe fn debug_reset_static_data() {
10    ffi::cairo_debug_reset_static_data()
11}
12
13pub fn get_version_string() -> &'static str {
14    unsafe {
15        let ptr = ffi::cairo_version_string();
16        CStr::from_ptr(ptr)
17            .to_str()
18            .expect("invalid version string")
19    }
20}
21
22#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
23pub struct Version {
24    pub major: u8,
25    pub minor: u8,
26    pub micro: u8,
27}
28
29impl Version {
30    pub fn get_version() -> Version {
31        let version = unsafe { ffi::cairo_version() };
32        Version {
33            major: (version / 10_000 % 100) as _,
34            minor: (version / 100 % 100) as _,
35            micro: (version % 100) as _,
36        }
37    }
38}
39
40impl fmt::Display for Version {
41    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
42        write!(f, "{}.{}.{}", self.major, self.minor, self.micro)
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn check_versions() {
52        assert_eq!(get_version_string(), Version::get_version().to_string());
53    }
54}