gio/auto/
socket_address_enumerator.rs1use crate::{AsyncResult, Cancellable, SocketAddress, ffi};
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 unsafe {
69 let mut error = std::ptr::null_mut();
70 let ret = ffi::g_socket_address_enumerator_next_finish(
71 _source_object as *mut _,
72 res,
73 &mut error,
74 );
75 let result = if error.is_null() {
76 Ok(from_glib_full(ret))
77 } else {
78 Err(from_glib_full(error))
79 };
80 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
81 Box_::from_raw(user_data as *mut _);
82 let callback: P = callback.into_inner();
83 callback(result);
84 }
85 }
86 let callback = next_async_trampoline::<P>;
87 unsafe {
88 ffi::g_socket_address_enumerator_next_async(
89 self.as_ref().to_glib_none().0,
90 cancellable.map(|p| p.as_ref()).to_glib_none().0,
91 Some(callback),
92 Box_::into_raw(user_data) as *mut _,
93 );
94 }
95 }
96
97 fn next_future(
98 &self,
99 ) -> Pin<
100 Box_<
101 dyn std::future::Future<Output = Result<Option<SocketAddress>, glib::Error>> + 'static,
102 >,
103 > {
104 Box_::pin(crate::GioFuture::new(
105 self,
106 move |obj, cancellable, send| {
107 obj.next_async(Some(cancellable), move |res| {
108 send.resolve(res);
109 });
110 },
111 ))
112 }
113}
114
115impl<O: IsA<SocketAddressEnumerator>> SocketAddressEnumeratorExt for O {}