nm_rs/auto/
range.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir
3// from gtk-girs (https://github.com/gtk-rs/gir-files)
4// DO NOT EDIT
5
6use crate::{ffi};
7#[cfg(feature = "v1_42")]
8#[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
9use glib::{translate::*};
10
11glib::wrapper! {
12    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
13    pub struct Range(Shared<ffi::NMRange>);
14
15    match fn {
16        ref => |ptr| ffi::nm_range_ref(ptr),
17        unref => |ptr| ffi::nm_range_unref(ptr),
18        type_ => || ffi::nm_range_get_type(),
19    }
20}
21
22impl Range {
23    /// Creates a new #NMRange object for the given range. Setting @end
24    /// equal to @start creates a single-element range.
25    /// ## `start`
26    /// the first element of the range
27    /// ## `end`
28    /// the last element of the range, must be greater than or equal
29    /// to @start.
30    ///
31    /// # Returns
32    ///
33    /// the new #NMRange object.
34    #[cfg(feature = "v1_42")]
35    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
36    #[doc(alias = "nm_range_new")]
37    pub fn new(start: u64, end: u64) -> Range {
38        assert_initialized_main_thread!();
39        unsafe {
40            from_glib_full(ffi::nm_range_new(start, end))
41        }
42    }
43
44    /// Compare two ranges.
45    /// ## `b`
46    /// another #NMRange
47    ///
48    /// # Returns
49    ///
50    /// zero if the two instances are equivalent or
51    ///   a non-zero integer otherwise. This defines a total ordering
52    ///   over the ranges.
53    #[cfg(feature = "v1_42")]
54    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
55    #[doc(alias = "nm_range_cmp")]
56    pub fn cmp(&self, b: &Range) -> i32 {
57        unsafe {
58            ffi::nm_range_cmp(self.to_glib_none().0, b.to_glib_none().0)
59        }
60    }
61
62    /// Gets the start and end values for the range.
63    ///
64    /// # Returns
65    ///
66    /// [`true`] if the range contains more than one
67    /// element, [`false`] otherwise.
68    ///
69    /// ## `start`
70    /// location to store the start value
71    ///
72    /// ## `end`
73    /// location to store the end value
74    #[cfg(feature = "v1_42")]
75    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
76    #[doc(alias = "nm_range_get_range")]
77    #[doc(alias = "get_range")]
78    pub fn range(&self) -> Option<(u64, u64)> {
79        unsafe {
80            let mut start = std::mem::MaybeUninit::uninit();
81            let mut end = std::mem::MaybeUninit::uninit();
82            let ret = from_glib(ffi::nm_range_get_range(self.to_glib_none().0, start.as_mut_ptr(), end.as_mut_ptr()));
83            if ret { Some((start.assume_init(), end.assume_init())) } else { None }
84        }
85    }
86
87    /// Convert a `NMRange` to a string.
88    ///
89    /// # Returns
90    ///
91    /// a string representing the range.
92    #[cfg(feature = "v1_42")]
93    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
94    #[doc(alias = "nm_range_to_str")]
95    pub fn to_str(&self) -> glib::GString {
96        unsafe {
97            from_glib_full(ffi::nm_range_to_str(self.to_glib_none().0))
98        }
99    }
100
101    /// Parses the string representation of the range to create a `NMRange`
102    /// instance.
103    /// ## `str`
104    /// the string representation of a range
105    ///
106    /// # Returns
107    ///
108    /// the `NMRange` or [`None`]
109    #[cfg(feature = "v1_42")]
110    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
111    #[doc(alias = "nm_range_from_str")]
112    pub fn from_str(str: &str) -> Result<Range, glib::Error> {
113        assert_initialized_main_thread!();
114        unsafe {
115            let mut error = std::ptr::null_mut();
116            let ret = ffi::nm_range_from_str(str.to_glib_none().0, &mut error);
117            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
118        }
119    }
120}
121
122#[cfg(feature = "v1_42")]
123#[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
124impl std::fmt::Display for Range {
125    #[inline]
126    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
127        f.write_str(&self.to_str())
128    }
129}