Skip to content

Commit 3a880a9

Browse files
committed
gio: implement DatagramBased
1 parent b9d2746 commit 3a880a9

File tree

7 files changed

+342
-1
lines changed

7 files changed

+342
-1
lines changed

gio/Gir.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,31 @@ status = "generate"
451451
name = "win32_pid"
452452
version = "2.72"
453453

454+
[[object]]
455+
name = "Gio.DatagramBased"
456+
status = "generate"
457+
manual_traits = ["DatagramBasedExtManual"]
458+
[[object.function]]
459+
name = "condition_wait"
460+
# needs Duration
461+
manual = true
462+
doc_trait_name = "DatagramBasedExtManual"
463+
[[object.function]]
464+
name = "create_source"
465+
# manual source implementation
466+
manual = true
467+
doc_trait_name = "DatagramBasedExtManual"
468+
[[object.function]]
469+
name = "receive_messages"
470+
# InputMessage is not a gtype
471+
manual = true
472+
doc_trait_name = "DatagramBasedExtManual"
473+
[[object.function]]
474+
name = "send_messages"
475+
# OutputMessage is not a gtype
476+
manual = true
477+
doc_trait_name = "DatagramBasedExtManual"
478+
454479
[[object]]
455480
name = "Gio.DataInputStream"
456481
manual_traits = ["DataInputStreamExtManual"]

gio/src/auto/datagram_based.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
5+
use glib::object::IsA;
6+
use glib::translate::*;
7+
use std::fmt;
8+
9+
glib::wrapper! {
10+
#[doc(alias = "GDatagramBased")]
11+
pub struct DatagramBased(Interface<ffi::GDatagramBased, ffi::GDatagramBasedInterface>);
12+
13+
match fn {
14+
type_ => || ffi::g_datagram_based_get_type(),
15+
}
16+
}
17+
18+
impl DatagramBased {
19+
pub const NONE: Option<&'static DatagramBased> = None;
20+
}
21+
22+
pub trait DatagramBasedExt: 'static {
23+
#[doc(alias = "g_datagram_based_condition_check")]
24+
fn condition_check(&self, condition: glib::IOCondition) -> glib::IOCondition;
25+
}
26+
27+
impl<O: IsA<DatagramBased>> DatagramBasedExt for O {
28+
fn condition_check(&self, condition: glib::IOCondition) -> glib::IOCondition {
29+
unsafe {
30+
from_glib(ffi::g_datagram_based_condition_check(
31+
self.as_ref().to_glib_none().0,
32+
condition.into_glib(),
33+
))
34+
}
35+
}
36+
}
37+
38+
impl fmt::Display for DatagramBased {
39+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40+
f.write_str("DatagramBased")
41+
}
42+
}

gio/src/auto/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ pub use self::data_input_stream::DataInputStream;
9898
mod data_output_stream;
9999
pub use self::data_output_stream::DataOutputStream;
100100

101+
mod datagram_based;
102+
pub use self::datagram_based::DatagramBased;
103+
101104
#[cfg(any(feature = "v2_72", feature = "dox"))]
102105
#[cfg_attr(feature = "dox", doc(cfg(feature = "v2_72")))]
103106
mod debug_controller;
@@ -387,6 +390,7 @@ mod unix_fd_message;
387390
#[cfg(any(unix, feature = "dox"))]
388391
#[cfg_attr(feature = "dox", doc(cfg(unix)))]
389392
pub use self::unix_fd_message::UnixFDMessage;
393+
390394
#[cfg(any(unix, feature = "dox"))]
391395
#[cfg_attr(feature = "dox", doc(cfg(unix)))]
392396
mod unix_input_stream;
@@ -723,6 +727,7 @@ pub mod traits {
723727
pub use super::converter_output_stream::ConverterOutputStreamExt;
724728
pub use super::data_input_stream::DataInputStreamExt;
725729
pub use super::data_output_stream::DataOutputStreamExt;
730+
pub use super::datagram_based::DatagramBasedExt;
726731
pub use super::dbus_interface::DBusInterfaceExt;
727732
pub use super::dbus_interface_skeleton::DBusInterfaceSkeletonExt;
728733
pub use super::dbus_object::DBusObjectExt;

gio/src/auto/socket.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use crate::Cancellable;
66
use crate::Credentials;
7+
use crate::DatagramBased;
78
use crate::InetAddress;
89
use crate::Initable;
910
use crate::SocketAddress;
@@ -25,7 +26,7 @@ use std::ptr;
2526

2627
glib::wrapper! {
2728
#[doc(alias = "GSocket")]
28-
pub struct Socket(Object<ffi::GSocket, ffi::GSocketClass>) @implements Initable;
29+
pub struct Socket(Object<ffi::GSocket, ffi::GSocketClass>) @implements DatagramBased, Initable;
2930

3031
match fn {
3132
type_ => || ffi::g_socket_get_type(),

gio/src/datagram_based.rs

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use crate::Cancellable;
4+
use crate::DatagramBased;
5+
use crate::InputMessage;
6+
use crate::OutputMessage;
7+
use futures_core::stream::Stream;
8+
use glib::object::{Cast, IsA};
9+
use glib::translate::*;
10+
use std::cell::RefCell;
11+
use std::mem::transmute;
12+
use std::pin::Pin;
13+
use std::ptr;
14+
use std::time::Duration;
15+
16+
pub trait DatagramBasedExtManual: Sized {
17+
#[doc(alias = "g_datagram_based_create_source")]
18+
fn create_source<F, C>(
19+
&self,
20+
condition: glib::IOCondition,
21+
cancellable: Option<&C>,
22+
name: Option<&str>,
23+
priority: glib::Priority,
24+
func: F,
25+
) -> glib::Source
26+
where
27+
F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static,
28+
C: IsA<Cancellable>;
29+
30+
fn create_source_future<C: IsA<Cancellable>>(
31+
&self,
32+
condition: glib::IOCondition,
33+
cancellable: Option<&C>,
34+
priority: glib::Priority,
35+
) -> Pin<Box<dyn std::future::Future<Output = glib::IOCondition> + 'static>>;
36+
37+
fn create_source_stream<C: IsA<Cancellable>>(
38+
&self,
39+
condition: glib::IOCondition,
40+
cancellable: Option<&C>,
41+
priority: glib::Priority,
42+
) -> Pin<Box<dyn Stream<Item = glib::IOCondition> + 'static>>;
43+
44+
#[doc(alias = "g_datagram_based_condition_wait")]
45+
fn condition_wait(
46+
&self,
47+
condition: glib::IOCondition,
48+
timeout: Option<Duration>,
49+
cancellable: Option<&impl IsA<Cancellable>>,
50+
) -> Result<(), glib::Error>;
51+
52+
#[doc(alias = "g_datagram_based_receive_messages")]
53+
fn receive_messages<'v, V: IntoIterator<Item = &'v mut [&'v mut [u8]]>, C: IsA<Cancellable>>(
54+
&self,
55+
messages: &mut [InputMessage],
56+
flags: i32,
57+
timeout: Option<Duration>,
58+
cancellable: Option<&C>,
59+
) -> Result<usize, glib::Error>;
60+
61+
#[doc(alias = "g_datagram_based_send_messages")]
62+
fn send_messages<C: IsA<Cancellable>>(
63+
&self,
64+
messages: &mut [OutputMessage],
65+
flags: i32,
66+
timeout: Option<Duration>,
67+
cancellable: Option<&C>,
68+
) -> Result<usize, glib::Error>;
69+
}
70+
71+
impl<O: IsA<DatagramBased>> DatagramBasedExtManual for O {
72+
fn create_source<F, C>(
73+
&self,
74+
condition: glib::IOCondition,
75+
cancellable: Option<&C>,
76+
name: Option<&str>,
77+
priority: glib::Priority,
78+
func: F,
79+
) -> glib::Source
80+
where
81+
F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static,
82+
C: IsA<Cancellable>,
83+
{
84+
unsafe extern "C" fn trampoline<
85+
O: IsA<DatagramBased>,
86+
F: FnMut(&O, glib::IOCondition) -> glib::Continue + 'static,
87+
>(
88+
datagram_based: *mut ffi::GDatagramBased,
89+
condition: glib::ffi::GIOCondition,
90+
func: glib::ffi::gpointer,
91+
) -> glib::ffi::gboolean {
92+
let func: &RefCell<F> = &*(func as *const RefCell<F>);
93+
let mut func = func.borrow_mut();
94+
(*func)(
95+
DatagramBased::from_glib_borrow(datagram_based).unsafe_cast_ref(),
96+
from_glib(condition),
97+
)
98+
.into_glib()
99+
}
100+
unsafe extern "C" fn destroy_closure<O, F>(ptr: glib::ffi::gpointer) {
101+
let _ = Box::<RefCell<F>>::from_raw(ptr as *mut _);
102+
}
103+
let cancellable = cancellable.map(|c| c.as_ref());
104+
let gcancellable = cancellable.to_glib_none();
105+
unsafe {
106+
let source = ffi::g_datagram_based_create_source(
107+
self.as_ref().to_glib_none().0,
108+
condition.into_glib(),
109+
gcancellable.0,
110+
);
111+
let trampoline = trampoline::<O, F> as glib::ffi::gpointer;
112+
glib::ffi::g_source_set_callback(
113+
source,
114+
Some(transmute::<
115+
_,
116+
unsafe extern "C" fn(glib::ffi::gpointer) -> glib::ffi::gboolean,
117+
>(trampoline)),
118+
Box::into_raw(Box::new(RefCell::new(func))) as glib::ffi::gpointer,
119+
Some(destroy_closure::<O, F>),
120+
);
121+
glib::ffi::g_source_set_priority(source, priority.into_glib());
122+
123+
if let Some(name) = name {
124+
glib::ffi::g_source_set_name(source, name.to_glib_none().0);
125+
}
126+
127+
from_glib_full(source)
128+
}
129+
}
130+
131+
fn create_source_future<C: IsA<Cancellable>>(
132+
&self,
133+
condition: glib::IOCondition,
134+
cancellable: Option<&C>,
135+
priority: glib::Priority,
136+
) -> Pin<Box<dyn std::future::Future<Output = glib::IOCondition> + 'static>> {
137+
let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned();
138+
139+
let obj = self.clone();
140+
Box::pin(glib::SourceFuture::new(move |send| {
141+
let mut send = Some(send);
142+
obj.create_source(
143+
condition,
144+
cancellable.as_ref(),
145+
None,
146+
priority,
147+
move |_, condition| {
148+
let _ = send.take().unwrap().send(condition);
149+
glib::Continue(false)
150+
},
151+
)
152+
}))
153+
}
154+
155+
fn create_source_stream<C: IsA<Cancellable>>(
156+
&self,
157+
condition: glib::IOCondition,
158+
cancellable: Option<&C>,
159+
priority: glib::Priority,
160+
) -> Pin<Box<dyn Stream<Item = glib::IOCondition> + 'static>> {
161+
let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned();
162+
163+
let obj = self.clone();
164+
Box::pin(glib::SourceStream::new(move |send| {
165+
let send = Some(send);
166+
obj.create_source(
167+
condition,
168+
cancellable.as_ref(),
169+
None,
170+
priority,
171+
move |_, condition| {
172+
if send.as_ref().unwrap().unbounded_send(condition).is_err() {
173+
glib::Continue(false)
174+
} else {
175+
glib::Continue(true)
176+
}
177+
},
178+
)
179+
}))
180+
}
181+
182+
fn condition_wait(
183+
&self,
184+
condition: glib::IOCondition,
185+
timeout: Option<Duration>,
186+
cancellable: Option<&impl IsA<Cancellable>>,
187+
) -> Result<(), glib::Error> {
188+
unsafe {
189+
let mut error = ptr::null_mut();
190+
let is_ok = ffi::g_datagram_based_condition_wait(
191+
self.as_ref().to_glib_none().0,
192+
condition.into_glib(),
193+
timeout
194+
.map(|t| t.as_micros().try_into().unwrap())
195+
.unwrap_or(-1),
196+
cancellable.map(|p| p.as_ref()).to_glib_none().0,
197+
&mut error,
198+
);
199+
assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
200+
if error.is_null() {
201+
Ok(())
202+
} else {
203+
Err(from_glib_full(error))
204+
}
205+
}
206+
}
207+
208+
fn receive_messages<'v, V: IntoIterator<Item = &'v mut [&'v mut [u8]]>, C: IsA<Cancellable>>(
209+
&self,
210+
messages: &mut [InputMessage],
211+
flags: i32,
212+
timeout: Option<Duration>,
213+
cancellable: Option<&C>,
214+
) -> Result<usize, glib::Error> {
215+
let cancellable = cancellable.map(|c| c.as_ref());
216+
unsafe {
217+
let mut error = ptr::null_mut();
218+
219+
let count = ffi::g_datagram_based_receive_messages(
220+
self.as_ref().to_glib_none().0,
221+
messages.as_mut_ptr() as *mut _,
222+
messages.len().try_into().unwrap(),
223+
flags,
224+
timeout
225+
.map(|t| t.as_micros().try_into().unwrap())
226+
.unwrap_or(-1),
227+
cancellable.to_glib_none().0,
228+
&mut error,
229+
);
230+
if error.is_null() {
231+
Ok(count as usize)
232+
} else {
233+
Err(from_glib_full(error))
234+
}
235+
}
236+
}
237+
238+
fn send_messages<C: IsA<Cancellable>>(
239+
&self,
240+
messages: &mut [OutputMessage],
241+
flags: i32,
242+
timeout: Option<Duration>,
243+
cancellable: Option<&C>,
244+
) -> Result<usize, glib::Error> {
245+
let cancellable = cancellable.map(|c| c.as_ref());
246+
unsafe {
247+
let mut error = ptr::null_mut();
248+
let count = ffi::g_datagram_based_send_messages(
249+
self.as_ref().to_glib_none().0,
250+
messages.as_mut_ptr() as *mut _,
251+
messages.len().try_into().unwrap(),
252+
flags,
253+
timeout
254+
.map(|t| t.as_micros().try_into().unwrap())
255+
.unwrap_or(-1),
256+
cancellable.to_glib_none().0,
257+
&mut error,
258+
);
259+
if error.is_null() {
260+
Ok(count as usize)
261+
} else {
262+
Err(from_glib_full(error))
263+
}
264+
}
265+
}
266+
}

gio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub use crate::cancellable_future::CancellableFuture;
2525
pub use crate::cancellable_future::Cancelled;
2626
mod converter;
2727
mod data_input_stream;
28+
mod datagram_based;
2829
mod dbus;
2930
pub use self::dbus::*;
3031
mod dbus_connection;

0 commit comments

Comments
 (0)