gio/auto/
socket_address_enumerator.rs1#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib::object::IsA;
9use glib::translate::*;
10use glib_sys;
11use gobject_sys;
12#[cfg(feature = "futures")]
13use std::boxed::Box as Box_;
14use std::fmt;
15use std::ptr;
16use Cancellable;
17use Error;
18use SocketAddress;
19
20glib_wrapper! {
21 pub struct SocketAddressEnumerator(Object<gio_sys::GSocketAddressEnumerator, gio_sys::GSocketAddressEnumeratorClass, SocketAddressEnumeratorClass>);
22
23 match fn {
24 get_type => || gio_sys::g_socket_address_enumerator_get_type(),
25 }
26}
27
28pub const NONE_SOCKET_ADDRESS_ENUMERATOR: Option<&SocketAddressEnumerator> = None;
29
30pub trait SocketAddressEnumeratorExt: 'static {
31 fn next<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<SocketAddress, Error>;
32
33 fn next_async<P: IsA<Cancellable>, Q: FnOnce(Result<SocketAddress, Error>) + Send + 'static>(
34 &self,
35 cancellable: Option<&P>,
36 callback: Q,
37 );
38
39 #[cfg(feature = "futures")]
40 fn next_async_future(
41 &self,
42 ) -> Box_<dyn future::Future<Output = Result<SocketAddress, Error>> + std::marker::Unpin>;
43}
44
45impl<O: IsA<SocketAddressEnumerator>> SocketAddressEnumeratorExt for O {
46 fn next<P: IsA<Cancellable>>(&self, cancellable: Option<&P>) -> Result<SocketAddress, Error> {
47 unsafe {
48 let mut error = ptr::null_mut();
49 let ret = gio_sys::g_socket_address_enumerator_next(
50 self.as_ref().to_glib_none().0,
51 cancellable.map(|p| p.as_ref()).to_glib_none().0,
52 &mut error,
53 );
54 if error.is_null() {
55 Ok(from_glib_full(ret))
56 } else {
57 Err(from_glib_full(error))
58 }
59 }
60 }
61
62 fn next_async<P: IsA<Cancellable>, Q: FnOnce(Result<SocketAddress, Error>) + Send + 'static>(
63 &self,
64 cancellable: Option<&P>,
65 callback: Q,
66 ) {
67 let user_data: Box<Q> = Box::new(callback);
68 unsafe extern "C" fn next_async_trampoline<
69 Q: FnOnce(Result<SocketAddress, Error>) + Send + 'static,
70 >(
71 _source_object: *mut gobject_sys::GObject,
72 res: *mut gio_sys::GAsyncResult,
73 user_data: glib_sys::gpointer,
74 ) {
75 let mut error = ptr::null_mut();
76 let ret = gio_sys::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<Q> = Box::from_raw(user_data as *mut _);
87 callback(result);
88 }
89 let callback = next_async_trampoline::<Q>;
90 unsafe {
91 gio_sys::g_socket_address_enumerator_next_async(
92 self.as_ref().to_glib_none().0,
93 cancellable.map(|p| p.as_ref()).to_glib_none().0,
94 Some(callback),
95 Box::into_raw(user_data) as *mut _,
96 );
97 }
98 }
99
100 #[cfg(feature = "futures")]
101 fn next_async_future(
102 &self,
103 ) -> Box_<dyn future::Future<Output = Result<SocketAddress, Error>> + std::marker::Unpin> {
104 use fragile::Fragile;
105 use GioFuture;
106
107 GioFuture::new(self, move |obj, send| {
108 let cancellable = Cancellable::new();
109 let send = Fragile::new(send);
110 obj.next_async(Some(&cancellable), move |res| {
111 let _ = send.into_inner().send(res);
112 });
113
114 cancellable
115 })
116 }
117}
118
119impl fmt::Display for SocketAddressEnumerator {
120 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121 write!(f, "SocketAddressEnumerator")
122 }
123}