gio/auto/
socket_address_enumerator.rs1use crate::{ffi, AsyncResult, Cancellable, SocketAddress};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 #[doc(alias = "GSocketAddressEnumerator")]
11 pub struct SocketAddressEnumerator(Object<ffi::GSocketAddressEnumerator, ffi::GSocketAddressEnumeratorClass>);
12
13 match fn {
14 type_ => || ffi::g_socket_address_enumerator_get_type(),
15 }
16}
17
18impl SocketAddressEnumerator {
19 pub const NONE: Option<&'static SocketAddressEnumerator> = None;
20}
21
22pub trait SocketAddressEnumeratorExt: IsA<SocketAddressEnumerator> + 'static {
23 #[doc(alias = "g_socket_address_enumerator_next")]
24 fn next(
25 &self,
26 cancellable: Option<&impl IsA<Cancellable>>,
27 ) -> Result<Option<SocketAddress>, glib::Error> {
28 unsafe {
29 let mut error = std::ptr::null_mut();
30 let ret = ffi::g_socket_address_enumerator_next(
31 self.as_ref().to_glib_none().0,
32 cancellable.map(|p| p.as_ref()).to_glib_none().0,
33 &mut error,
34 );
35 if error.is_null() {
36 Ok(from_glib_full(ret))
37 } else {
38 Err(from_glib_full(error))
39 }
40 }
41 }
42
43 #[doc(alias = "g_socket_address_enumerator_next_async")]
44 fn next_async<P: FnOnce(Result<Option<SocketAddress>, glib::Error>) + 'static>(
45 &self,
46 cancellable: Option<&impl IsA<Cancellable>>,
47 callback: P,
48 ) {
49 let main_context = glib::MainContext::ref_thread_default();
50 let is_main_context_owner = main_context.is_owner();
51 let has_acquired_main_context = (!is_main_context_owner)
52 .then(|| main_context.acquire().ok())
53 .flatten();
54 assert!(
55 is_main_context_owner || has_acquired_main_context.is_some(),
56 "Async operations only allowed if the thread is owning the MainContext"
57 );
58
59 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
60 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
61 unsafe extern "C" fn next_async_trampoline<
62 P: FnOnce(Result<Option<SocketAddress>, glib::Error>) + 'static,
63 >(
64 _source_object: *mut glib::gobject_ffi::GObject,
65 res: *mut crate::ffi::GAsyncResult,
66 user_data: glib::ffi::gpointer,
67 ) {
68 let mut error = std::ptr::null_mut();
69 let ret = ffi::g_socket_address_enumerator_next_finish(
70 _source_object as *mut _,
71 res,
72 &mut error,
73 );
74 let result = if error.is_null() {
75 Ok(from_glib_full(ret))
76 } else {
77 Err(from_glib_full(error))
78 };
79 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
80 Box_::from_raw(user_data as *mut _);
81 let callback: P = callback.into_inner();
82 callback(result);
83 }
84 let callback = next_async_trampoline::<P>;
85 unsafe {
86 ffi::g_socket_address_enumerator_next_async(
87 self.as_ref().to_glib_none().0,
88 cancellable.map(|p| p.as_ref()).to_glib_none().0,
89 Some(callback),
90 Box_::into_raw(user_data) as *mut _,
91 );
92 }
93 }
94
95 fn next_future(
96 &self,
97 ) -> Pin<
98 Box_<
99 dyn std::future::Future<Output = Result<Option<SocketAddress>, glib::Error>> + 'static,
100 >,
101 > {
102 Box_::pin(crate::GioFuture::new(
103 self,
104 move |obj, cancellable, send| {
105 obj.next_async(Some(cancellable), move |res| {
106 send.resolve(res);
107 });
108 },
109 ))
110 }
111}
112
113impl<O: IsA<SocketAddressEnumerator>> SocketAddressEnumeratorExt for O {}