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
114/// Trait containing all [`struct@InetAddressMask`] methods.
115///
116/// # Implementors
117///
118/// [`InetAddressMask`][struct@crate::InetAddressMask]
119pub trait InetAddressMaskExt: IsA<InetAddressMask> + 'static {
120    #[doc(alias = "g_inet_address_mask_equal")]
121    fn equal(&self, mask2: &impl IsA<InetAddressMask>) -> bool {
122        unsafe {
123            from_glib(ffi::g_inet_address_mask_equal(
124                self.as_ref().to_glib_none().0,
125                mask2.as_ref().to_glib_none().0,
126            ))
127        }
128    }
129
130    /// Gets @self's base address
131    ///
132    /// # Returns
133    ///
134    /// @self's base address
135    #[doc(alias = "g_inet_address_mask_get_address")]
136    #[doc(alias = "get_address")]
137    fn address(&self) -> InetAddress {
138        unsafe {
139            from_glib_none(ffi::g_inet_address_mask_get_address(
140                self.as_ref().to_glib_none().0,
141            ))
142        }
143    }
144
145    /// Gets the #GSocketFamily of @self's address
146    ///
147    /// # Returns
148    ///
149    /// the #GSocketFamily of @self's address
150    #[doc(alias = "g_inet_address_mask_get_family")]
151    #[doc(alias = "get_family")]
152    fn family(&self) -> SocketFamily {
153        unsafe {
154            from_glib(ffi::g_inet_address_mask_get_family(
155                self.as_ref().to_glib_none().0,
156            ))
157        }
158    }
159
160    /// Gets @self's length
161    ///
162    /// # Returns
163    ///
164    /// @self's length
165    #[doc(alias = "g_inet_address_mask_get_length")]
166    #[doc(alias = "get_length")]
167    fn length(&self) -> u32 {
168        unsafe { ffi::g_inet_address_mask_get_length(self.as_ref().to_glib_none().0) }
169    }
170
171    /// Tests if @address falls within the range described by @self.
172    /// ## `address`
173    /// a #GInetAddress
174    ///
175    /// # Returns
176    ///
177    /// whether @address falls within the range described by
178    /// @self.
179    #[doc(alias = "g_inet_address_mask_matches")]
180    fn matches(&self, address: &impl IsA<InetAddress>) -> bool {
181        unsafe {
182            from_glib(ffi::g_inet_address_mask_matches(
183                self.as_ref().to_glib_none().0,
184                address.as_ref().to_glib_none().0,
185            ))
186        }
187    }
188
189    /// Converts @self back to its corresponding string form.
190    ///
191    /// # Returns
192    ///
193    /// a string corresponding to @self.
194    #[doc(alias = "g_inet_address_mask_to_string")]
195    #[doc(alias = "to_string")]
196    fn to_str(&self) -> glib::GString {
197        unsafe {
198            from_glib_full(ffi::g_inet_address_mask_to_string(
199                self.as_ref().to_glib_none().0,
200            ))
201        }
202    }
203
204    /// The base address.
205    fn set_address<P: IsA<InetAddress>>(&self, address: Option<&P>) {
206        ObjectExt::set_property(self.as_ref(), "address", address)
207    }
208
209    /// The prefix length, in bytes.
210    fn set_length(&self, length: u32) {
211        ObjectExt::set_property(self.as_ref(), "length", length)
212    }
213
214    #[doc(alias = "address")]
215    fn connect_address_notify<F: Fn(&Self) + Send + Sync + 'static>(
216        &self,
217        f: F,
218    ) -> SignalHandlerId {
219        unsafe extern "C" fn notify_address_trampoline<
220            P: IsA<InetAddressMask>,
221            F: Fn(&P) + Send + Sync + 'static,
222        >(
223            this: *mut ffi::GInetAddressMask,
224            _param_spec: glib::ffi::gpointer,
225            f: glib::ffi::gpointer,
226        ) {
227            let f: &F = &*(f as *const F);
228            f(InetAddressMask::from_glib_borrow(this).unsafe_cast_ref())
229        }
230        unsafe {
231            let f: Box_<F> = Box_::new(f);
232            connect_raw(
233                self.as_ptr() as *mut _,
234                c"notify::address".as_ptr() as *const _,
235                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
236                    notify_address_trampoline::<Self, F> as *const (),
237                )),
238                Box_::into_raw(f),
239            )
240        }
241    }
242
243    #[doc(alias = "family")]
244    fn connect_family_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
245        unsafe extern "C" fn notify_family_trampoline<
246            P: IsA<InetAddressMask>,
247            F: Fn(&P) + Send + Sync + 'static,
248        >(
249            this: *mut ffi::GInetAddressMask,
250            _param_spec: glib::ffi::gpointer,
251            f: glib::ffi::gpointer,
252        ) {
253            let f: &F = &*(f as *const F);
254            f(InetAddressMask::from_glib_borrow(this).unsafe_cast_ref())
255        }
256        unsafe {
257            let f: Box_<F> = Box_::new(f);
258            connect_raw(
259                self.as_ptr() as *mut _,
260                c"notify::family".as_ptr() as *const _,
261                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
262                    notify_family_trampoline::<Self, F> as *const (),
263                )),
264                Box_::into_raw(f),
265            )
266        }
267    }
268
269    #[doc(alias = "length")]
270    fn connect_length_notify<F: Fn(&Self) + Send + Sync + 'static>(&self, f: F) -> SignalHandlerId {
271        unsafe extern "C" fn notify_length_trampoline<
272            P: IsA<InetAddressMask>,
273            F: Fn(&P) + Send + Sync + 'static,
274        >(
275            this: *mut ffi::GInetAddressMask,
276            _param_spec: glib::ffi::gpointer,
277            f: glib::ffi::gpointer,
278        ) {
279            let f: &F = &*(f as *const F);
280            f(InetAddressMask::from_glib_borrow(this).unsafe_cast_ref())
281        }
282        unsafe {
283            let f: Box_<F> = Box_::new(f);
284            connect_raw(
285                self.as_ptr() as *mut _,
286                c"notify::length".as_ptr() as *const _,
287                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
288                    notify_length_trampoline::<Self, F> as *const (),
289                )),
290                Box_::into_raw(f),
291            )
292        }
293    }
294}
295
296impl<O: IsA<InetAddressMask>> InetAddressMaskExt for O {}