gio/auto/socket_address_enumerator.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, AsyncResult, Cancellable, SocketAddress};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 /// `GSocketAddressEnumerator` is an enumerator type for
11 /// [`SocketAddress`][crate::SocketAddress] instances. It is returned by enumeration functions
12 /// such as [`SocketConnectableExt::enumerate()`][crate::prelude::SocketConnectableExt::enumerate()], which returns a
13 /// `GSocketAddressEnumerator` to list each [`SocketAddress`][crate::SocketAddress] which could
14 /// be used to connect to that [`SocketConnectable`][crate::SocketConnectable].
15 ///
16 /// Enumeration is typically a blocking operation, so the asynchronous methods
17 /// [`SocketAddressEnumeratorExt::next_async()`][crate::prelude::SocketAddressEnumeratorExt::next_async()] and
18 /// `Gio::SocketAddressEnumerator::next_finish()` should be used where
19 /// possible.
20 ///
21 /// Each `GSocketAddressEnumerator` can only be enumerated once. Once
22 /// [`SocketAddressEnumeratorExt::next()`][crate::prelude::SocketAddressEnumeratorExt::next()] has returned `NULL`, further
23 /// enumeration with that `GSocketAddressEnumerator` is not possible, and it can
24 /// be unreffed.
25 ///
26 /// This is an Abstract Base Class, you cannot instantiate it.
27 ///
28 /// # Implements
29 ///
30 /// [`SocketAddressEnumeratorExt`][trait@crate::prelude::SocketAddressEnumeratorExt], [`trait@glib::ObjectExt`]
31 #[doc(alias = "GSocketAddressEnumerator")]
32 pub struct SocketAddressEnumerator(Object<ffi::GSocketAddressEnumerator, ffi::GSocketAddressEnumeratorClass>);
33
34 match fn {
35 type_ => || ffi::g_socket_address_enumerator_get_type(),
36 }
37}
38
39impl SocketAddressEnumerator {
40 pub const NONE: Option<&'static SocketAddressEnumerator> = None;
41}
42
43/// Trait containing all [`struct@SocketAddressEnumerator`] methods.
44///
45/// # Implementors
46///
47/// [`ProxyAddressEnumerator`][struct@crate::ProxyAddressEnumerator], [`SocketAddressEnumerator`][struct@crate::SocketAddressEnumerator]
48pub trait SocketAddressEnumeratorExt: IsA<SocketAddressEnumerator> + 'static {
49 /// Retrieves the next #GSocketAddress from @self. Note that this
50 /// may block for some amount of time. (Eg, a #GNetworkAddress may need
51 /// to do a DNS lookup before it can return an address.) Use
52 /// g_socket_address_enumerator_next_async() if you need to avoid
53 /// blocking.
54 ///
55 /// If @self is expected to yield addresses, but for some reason
56 /// is unable to (eg, because of a DNS error), then the first call to
57 /// g_socket_address_enumerator_next() will return an appropriate error
58 /// in `*error`. However, if the first call to
59 /// g_socket_address_enumerator_next() succeeds, then any further
60 /// internal errors (other than @cancellable being triggered) will be
61 /// ignored.
62 /// ## `cancellable`
63 /// optional #GCancellable object, [`None`] to ignore.
64 ///
65 /// # Returns
66 ///
67 /// a #GSocketAddress (owned by the caller), or [`None`] on
68 /// error (in which case `*error` will be set) or if there are no
69 /// more addresses.
70 #[doc(alias = "g_socket_address_enumerator_next")]
71 fn next(
72 &self,
73 cancellable: Option<&impl IsA<Cancellable>>,
74 ) -> Result<Option<SocketAddress>, glib::Error> {
75 unsafe {
76 let mut error = std::ptr::null_mut();
77 let ret = ffi::g_socket_address_enumerator_next(
78 self.as_ref().to_glib_none().0,
79 cancellable.map(|p| p.as_ref()).to_glib_none().0,
80 &mut error,
81 );
82 if error.is_null() {
83 Ok(from_glib_full(ret))
84 } else {
85 Err(from_glib_full(error))
86 }
87 }
88 }
89
90 /// Asynchronously retrieves the next #GSocketAddress from @self
91 /// and then calls @callback, which must call
92 /// g_socket_address_enumerator_next_finish() to get the result.
93 ///
94 /// It is an error to call this multiple times before the previous callback has finished.
95 /// ## `cancellable`
96 /// optional #GCancellable object, [`None`] to ignore.
97 /// ## `callback`
98 /// a #GAsyncReadyCallback to call
99 /// when the request is satisfied
100 #[doc(alias = "g_socket_address_enumerator_next_async")]
101 fn next_async<P: FnOnce(Result<Option<SocketAddress>, glib::Error>) + 'static>(
102 &self,
103 cancellable: Option<&impl IsA<Cancellable>>,
104 callback: P,
105 ) {
106 let main_context = glib::MainContext::ref_thread_default();
107 let is_main_context_owner = main_context.is_owner();
108 let has_acquired_main_context = (!is_main_context_owner)
109 .then(|| main_context.acquire().ok())
110 .flatten();
111 assert!(
112 is_main_context_owner || has_acquired_main_context.is_some(),
113 "Async operations only allowed if the thread is owning the MainContext"
114 );
115
116 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
117 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
118 unsafe extern "C" fn next_async_trampoline<
119 P: FnOnce(Result<Option<SocketAddress>, glib::Error>) + 'static,
120 >(
121 _source_object: *mut glib::gobject_ffi::GObject,
122 res: *mut crate::ffi::GAsyncResult,
123 user_data: glib::ffi::gpointer,
124 ) {
125 let mut error = std::ptr::null_mut();
126 let ret = ffi::g_socket_address_enumerator_next_finish(
127 _source_object as *mut _,
128 res,
129 &mut error,
130 );
131 let result = if error.is_null() {
132 Ok(from_glib_full(ret))
133 } else {
134 Err(from_glib_full(error))
135 };
136 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
137 Box_::from_raw(user_data as *mut _);
138 let callback: P = callback.into_inner();
139 callback(result);
140 }
141 let callback = next_async_trampoline::<P>;
142 unsafe {
143 ffi::g_socket_address_enumerator_next_async(
144 self.as_ref().to_glib_none().0,
145 cancellable.map(|p| p.as_ref()).to_glib_none().0,
146 Some(callback),
147 Box_::into_raw(user_data) as *mut _,
148 );
149 }
150 }
151
152 fn next_future(
153 &self,
154 ) -> Pin<
155 Box_<
156 dyn std::future::Future<Output = Result<Option<SocketAddress>, glib::Error>> + 'static,
157 >,
158 > {
159 Box_::pin(crate::GioFuture::new(
160 self,
161 move |obj, cancellable, send| {
162 obj.next_async(Some(cancellable), move |res| {
163 send.resolve(res);
164 });
165 },
166 ))
167 }
168}
169
170impl<O: IsA<SocketAddressEnumerator>> SocketAddressEnumeratorExt for O {}