gio/auto/
inet_address_mask.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, InetAddress, Initable, SocketFamily};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    /// `GInetAddressMask` represents a range of IPv4 or IPv6 addresses
15    /// described by a base address and a length indicating how many bits
16    /// of the base address are relevant for matching purposes. These are
17    /// often given in string form. For example, `10.0.0.0/8`, or `fe80::/10`.
18    ///
19    /// ## Properties
20    ///
21    ///
22    /// #### `address`
23    ///  The base address.
24    ///
25    /// Readable | Writeable
26    ///
27    ///
28    /// #### `family`
29    ///  The address family (IPv4 or IPv6).
30    ///
31    /// Readable
32    ///
33    ///
34    /// #### `length`
35    ///  The prefix length, in bytes.
36    ///
37    /// Readable | Writeable
38    ///
39    /// # Implements
40    ///
41    /// [`InetAddressMaskExt`][trait@crate::prelude::InetAddressMaskExt], [`trait@glib::ObjectExt`], [`InitableExt`][trait@crate::prelude::InitableExt]
42    #[doc(alias = "GInetAddressMask")]
43    pub struct InetAddressMask(Object<ffi::GInetAddressMask, ffi::GInetAddressMaskClass>) @implements Initable;
44
45    match fn {
46        type_ => || ffi::g_inet_address_mask_get_type(),
47    }
48}
49
50impl InetAddressMask {
51    pub const NONE: Option<&'static InetAddressMask> = None;
52
53    /// Creates a new #GInetAddressMask representing all addresses whose
54    /// first @length bits match @addr.
55    /// ## `addr`
56    /// a #GInetAddress
57    /// ## `length`
58    /// number of bits of @addr to use
59    ///
60    /// # Returns
61    ///
62    /// a new #GInetAddressMask, or [`None`] on error
63    #[doc(alias = "g_inet_address_mask_new")]
64    pub fn new(addr: &impl IsA<InetAddress>, length: u32) -> Result<InetAddressMask, glib::Error> {
65        unsafe {
66            let mut error = std::ptr::null_mut();
67            let ret =
68                ffi::g_inet_address_mask_new(addr.as_ref().to_glib_none().0, length, &mut error);
69            if error.is_null() {
70                Ok(from_glib_full(ret))
71            } else {
72                Err(from_glib_full(error))
73            }
74        }
75    }
76
77    /// Parses @mask_string as an IP address and (optional) length, and
78    /// creates a new #GInetAddressMask. The length, if present, is
79    /// delimited by a "/". If it is not present, then the length is
80    /// assumed to be the full length of the address.
81    /// ## `mask_string`
82    /// an IP address or address/length string
83    ///
84    /// # Returns
85    ///
86    /// a new #GInetAddressMask corresponding to @string, or [`None`]
87    /// on error.
88    #[doc(alias = "g_inet_address_mask_new_from_string")]
89    #[doc(alias = "new_from_string")]
90    pub fn from_string(mask_string: &str) -> Result<InetAddressMask, glib::Error> {
91        unsafe {
92            let mut error = std::ptr::null_mut();
93            let ret =
94                ffi::g_inet_address_mask_new_from_string(mask_string.to_glib_none().0, &mut error);
95            if error.is_null() {
96                Ok(from_glib_full(ret))
97            } else {
98                Err(from_glib_full(error))
99            }
100        }
101    }
102}
103
104impl std::fmt::Display for InetAddressMask {
105    #[inline]
106    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
107        f.write_str(&InetAddressMaskExt::to_str(self))
108    }
109}
110
111unsafe impl Send for InetAddressMask {}
112unsafe impl Sync for InetAddressMask {}
113
114mod sealed {
115    pub trait Sealed {}
116    impl<T: super::IsA<super::InetAddressMask>> Sealed for T {}
117}
118
119/// Trait containing all [`struct@InetAddressMask`] methods.
120///
121/// # Implementors
122///
123/// [`InetAddressMask`][struct@crate::InetAddressMask]
124pub trait InetAddressMaskExt: IsA<InetAddressMask> + sealed::Sealed + 'static {
125    #[doc(alias = "g_inet_address_mask_equal")]
126    fn equal(&self, mask2: &impl IsA<InetAddressMask>) -> bool {
127        unsafe {
128            from_glib(ffi::g_inet_address_mask_equal(
129                self.as_ref().to_glib_none().0,
130                mask2.as_ref().to_glib_none().0,
131            ))
132        }
133    }
134
135    /// Gets @self's base address
136    ///
137    /// # Returns
138    ///
139    /// @self's base address
140    #[doc(alias = "g_inet_address_mask_get_address")]
141    #[doc(alias = "get_address")]
142    fn address(&self) -> InetAddress {
143        unsafe {
144            from_glib_none(ffi::g_inet_address_mask_get_address(
145                self.as_ref().to_glib_none().0,
146            ))
147        }
148    }
149
150    /// Gets the #GSocketFamily of @self's address
151    ///
152    /// # Returns
153    ///
154    /// the #GSocketFamily of @self's address
155    #[doc(alias = "g_inet_address_mask_get_family")]
156    #[doc(alias = "get_family")]
157    fn family(&self) -> SocketFamily {
158        unsafe {
159            from_glib(ffi::g_inet_address_mask_get_family(
160                self.as_ref().to_glib_none().0,
161            ))
162        }
163    }
164
165    /// Gets @self's length
166    ///
167    /// # Returns
168    ///
169    /// @self's length
170    #[doc(alias = "g_inet_address_mask_get_length")]
171    #[doc(alias = "get_length")]
172    fn length(&self) -> u32 {
173        unsafe { ffi::g_inet_address_mask_get_length(self.as_ref().to_glib_none().0) }
174    }
175
176    /// Tests if @address falls within the range described by @self.
177    /// ## `address`
178    /// a #GInetAddress
179    ///
180    /// # Returns
181    ///
182    /// whether @address falls within the range described by
183    /// @self.
184    #[doc(alias = "g_inet_address_mask_matches")]
185    fn matches(&self, address: &impl IsA<InetAddress>) -> bool {
186        unsafe {
187            from_glib(ffi::g_inet_address_mask_matches(
188                self.as_ref().to_glib_none().0,
189                address.as_ref().to_glib_none().0,
190            ))
191        }
192    }
193
194    /// Converts @self back to its corresponding string form.
195    ///
196    /// # Returns
197    ///
198    /// a string corresponding to @self.
199    #[doc(alias = "g_inet_address_mask_to_string")]
200    #[doc(alias = "to_string")]
201    fn to_str(&self) -> glib::GString {
202        unsafe {
203            from_glib_full(ffi::g_inet_address_mask_to_string(
204                self.as_ref().to_glib_none().0,
205            ))
206        }
207    }
208
209    /// The base address.
210    fn set_address<P: IsA<InetAddress>>(&self, address: Option<&P>) {
211        ObjectExt::set_property(self.as_ref(), "address", address)
212    }
213
214    /// The prefix length, in bytes.
215    fn set_length(&self, length: u32) {
216        ObjectExt::set_property(self.as_ref(), "length", length)
217    }
218
219    #[doc(alias = "address")]
220    fn connect_address_notify<F: Fn(&Self) + Send + Sync + 'static>(
221        &self,
222        f: F,
223    ) -> SignalHandlerId {
224        unsafe extern "C" fn notify_address_trampoline<
225            P: IsA<InetAddressMask>,
226            F: Fn(&P) + Send + Sync + 'static,
227        >(
228            this: *mut ffi::GInetAddressMask,
229            _param_spec: glib::ffi::gpointer,
230            f: glib::ffi::gpointer,
231        ) {
232            let f: &F = &*(f as *const F);
233            f(InetAddressMask::from_glib_borrow(this).unsafe_cast_ref())
234        }
235        unsafe {
236            let f: Box_<F> = Box_::new(f);
237            connect_raw(
238                self.as_ptr() as *mut _,
239                b"notify::address\0".as_ptr() as *const _,
240                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
241                    notify_address_trampoline::<Self, F> as *const (),
242                )),
243                Box_::into_raw(f),
244            )
245        }
246    }
247
248    #[doc(alias = "family")]
249    fn connect_family_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
250        unsafe extern "C" fn notify_family_trampoline<
251            P: IsA<InetAddressMask>,
252            F: Fn(&P) + Send + Sync + 'static,
253        >(
254            this: *mut ffi::GInetAddressMask,
255            _param_spec: glib::ffi::gpointer,
256            f: glib::ffi::gpointer,
257        ) {
258            let f: &F = &*(f as *const F);
259            f(InetAddressMask::from_glib_borrow(this).unsafe_cast_ref())
260        }
261        unsafe {
262            let f: Box_<F> = Box_::new(f);
263            connect_raw(
264                self.as_ptr() as *mut _,
265                b"notify::family\0".as_ptr() as *const _,
266                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
267                    notify_family_trampoline::<Self, F> as *const (),
268                )),
269                Box_::into_raw(f),
270            )
271        }
272    }
273
274    #[doc(alias = "length")]
275    fn connect_length_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
276        unsafe extern "C" fn notify_length_trampoline<
277            P: IsA<InetAddressMask>,
278            F: Fn(&P) + Send + Sync + 'static,
279        >(
280            this: *mut ffi::GInetAddressMask,
281            _param_spec: glib::ffi::gpointer,
282            f: glib::ffi::gpointer,
283        ) {
284            let f: &F = &*(f as *const F);
285            f(InetAddressMask::from_glib_borrow(this).unsafe_cast_ref())
286        }
287        unsafe {
288            let f: Box_<F> = Box_::new(f);
289            connect_raw(
290                self.as_ptr() as *mut _,
291                b"notify::length\0".as_ptr() as *const _,
292                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
293                    notify_length_trampoline::<Self, F> as *const (),
294                )),
295                Box_::into_raw(f),
296            )
297        }
298    }
299}
300
301impl<O: IsA<InetAddressMask>> InetAddressMaskExt for O {}