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