nm-rs 0.1.3

Rust bindings for the libnm library.
Documentation
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir
// from gtk-girs (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{ffi};
#[cfg(feature = "v1_42")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
use glib::{translate::*};

glib::wrapper! {
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct Range(Shared<ffi::NMRange>);

    match fn {
        ref => |ptr| ffi::nm_range_ref(ptr),
        unref => |ptr| ffi::nm_range_unref(ptr),
        type_ => || ffi::nm_range_get_type(),
    }
}

impl Range {
    /// Creates a new #NMRange object for the given range. Setting @end
    /// equal to @start creates a single-element range.
    /// ## `start`
    /// the first element of the range
    /// ## `end`
    /// the last element of the range, must be greater than or equal
    /// to @start.
    ///
    /// # Returns
    ///
    /// the new #NMRange object.
    #[cfg(feature = "v1_42")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
    #[doc(alias = "nm_range_new")]
    pub fn new(start: u64, end: u64) -> Range {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::nm_range_new(start, end))
        }
    }

    /// Compare two ranges.
    /// ## `b`
    /// another #NMRange
    ///
    /// # Returns
    ///
    /// zero if the two instances are equivalent or
    ///   a non-zero integer otherwise. This defines a total ordering
    ///   over the ranges.
    #[cfg(feature = "v1_42")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
    #[doc(alias = "nm_range_cmp")]
    pub fn cmp(&self, b: &Range) -> i32 {
        unsafe {
            ffi::nm_range_cmp(self.to_glib_none().0, b.to_glib_none().0)
        }
    }

    /// Gets the start and end values for the range.
    ///
    /// # Returns
    ///
    /// [`true`] if the range contains more than one
    /// element, [`false`] otherwise.
    ///
    /// ## `start`
    /// location to store the start value
    ///
    /// ## `end`
    /// location to store the end value
    #[cfg(feature = "v1_42")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
    #[doc(alias = "nm_range_get_range")]
    #[doc(alias = "get_range")]
    pub fn range(&self) -> Option<(u64, u64)> {
        unsafe {
            let mut start = std::mem::MaybeUninit::uninit();
            let mut end = std::mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::nm_range_get_range(self.to_glib_none().0, start.as_mut_ptr(), end.as_mut_ptr()));
            if ret { Some((start.assume_init(), end.assume_init())) } else { None }
        }
    }

    /// Convert a `NMRange` to a string.
    ///
    /// # Returns
    ///
    /// a string representing the range.
    #[cfg(feature = "v1_42")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
    #[doc(alias = "nm_range_to_str")]
    pub fn to_str(&self) -> glib::GString {
        unsafe {
            from_glib_full(ffi::nm_range_to_str(self.to_glib_none().0))
        }
    }

    /// Parses the string representation of the range to create a `NMRange`
    /// instance.
    /// ## `str`
    /// the string representation of a range
    ///
    /// # Returns
    ///
    /// the `NMRange` or [`None`]
    #[cfg(feature = "v1_42")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
    #[doc(alias = "nm_range_from_str")]
    pub fn from_str(str: &str) -> Result<Range, glib::Error> {
        assert_initialized_main_thread!();
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::nm_range_from_str(str.to_glib_none().0, &mut error);
            if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
        }
    }
}

#[cfg(feature = "v1_42")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_42")))]
impl std::fmt::Display for Range {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(&self.to_str())
    }
}