Skip to content

Commit

Permalink
glib: Replace Continue with ControlFlow
Browse files Browse the repository at this point in the history
Fixes #1063
  • Loading branch information
bilelmoussaoui committed Mar 24, 2023
1 parent 3e6519c commit ec31031
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 128 deletions.
12 changes: 6 additions & 6 deletions gio/src/datagram_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait DatagramBasedExtManual: Sized {
func: F,
) -> glib::Source
where
F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static,
F: FnMut(&Self, glib::IOCondition) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>;

fn create_source_future<C: IsA<Cancellable>>(
Expand Down Expand Up @@ -72,12 +72,12 @@ impl<O: IsA<DatagramBased>> DatagramBasedExtManual for O {
func: F,
) -> glib::Source
where
F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static,
F: FnMut(&Self, glib::IOCondition) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>,
{
unsafe extern "C" fn trampoline<
O: IsA<DatagramBased>,
F: FnMut(&O, glib::IOCondition) -> glib::Continue + 'static,
F: FnMut(&O, glib::IOCondition) -> glib::ControlFlow + 'static,
>(
datagram_based: *mut ffi::GDatagramBased,
condition: glib::ffi::GIOCondition,
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<O: IsA<DatagramBased>> DatagramBasedExtManual for O {
priority,
move |_, condition| {
let _ = send.take().unwrap().send(condition);
glib::Continue(false)
glib::ControlFlow::Break
},
)
}))
Expand All @@ -164,9 +164,9 @@ impl<O: IsA<DatagramBased>> DatagramBasedExtManual for O {
priority,
move |_, condition| {
if send.as_ref().unwrap().unbounded_send(condition).is_err() {
glib::Continue(false)
glib::ControlFlow::Break
} else {
glib::Continue(true)
glib::ControlFlow::Continue
}
},
)
Expand Down
14 changes: 7 additions & 7 deletions gio/src/pollable_input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait PollableInputStreamExtManual: Sized {
func: F,
) -> glib::Source
where
F: FnMut(&Self) -> glib::Continue + 'static,
F: FnMut(&Self) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>;

fn create_source_future<C: IsA<Cancellable>>(
Expand Down Expand Up @@ -64,12 +64,12 @@ impl<O: IsA<PollableInputStream>> PollableInputStreamExtManual for O {
func: F,
) -> glib::Source
where
F: FnMut(&Self) -> glib::Continue + 'static,
F: FnMut(&Self) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>,
{
unsafe extern "C" fn trampoline<
O: IsA<PollableInputStream>,
F: FnMut(&O) -> glib::Continue + 'static,
F: FnMut(&O) -> glib::ControlFlow + 'static,
>(
stream: *mut ffi::GPollableInputStream,
func: glib::ffi::gpointer,
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<O: IsA<PollableInputStream>> PollableInputStreamExtManual for O {
let mut send = Some(send);
obj.create_source(cancellable.as_ref(), None, priority, move |_| {
let _ = send.take().unwrap().send(());
glib::Continue(false)
glib::ControlFlow::Break
})
}))
}
Expand All @@ -162,9 +162,9 @@ impl<O: IsA<PollableInputStream>> PollableInputStreamExtManual for O {
Box::pin(glib::SourceStream::new(move |send| {
obj.create_source(cancellable.as_ref(), None, priority, move |_| {
if send.unbounded_send(()).is_err() {
glib::Continue(false)
glib::ControlFlow::Break
} else {
glib::Continue(true)
glib::ControlFlow::Continue
}
})
}))
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<T: IsA<PollableInputStream>> AsyncRead for InputStreamAsyncRead<T> {
if let Some(waker) = waker.take() {
waker.wake();
}
glib::Continue(false)
glib::ControlFlow::Break
},
);
let main_context = glib::MainContext::ref_thread_default();
Expand Down
16 changes: 8 additions & 8 deletions gio/src/pollable_output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait PollableOutputStreamExtManual {
func: F,
) -> glib::Source
where
F: FnMut(&Self) -> glib::Continue + 'static,
F: FnMut(&Self) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>;

fn create_source_future<C: IsA<Cancellable>>(
Expand Down Expand Up @@ -70,12 +70,12 @@ impl<O: IsA<PollableOutputStream>> PollableOutputStreamExtManual for O {
func: F,
) -> glib::Source
where
F: FnMut(&Self) -> glib::Continue + 'static,
F: FnMut(&Self) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>,
{
unsafe extern "C" fn trampoline<
O: IsA<PollableOutputStream>,
F: FnMut(&O) -> glib::Continue + 'static,
F: FnMut(&O) -> glib::ControlFlow + 'static,
>(
stream: *mut ffi::GPollableOutputStream,
func: glib::ffi::gpointer,
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<O: IsA<PollableOutputStream>> PollableOutputStreamExtManual for O {
let mut send = Some(send);
obj.create_source(cancellable.as_ref(), None, priority, move |_| {
let _ = send.take().unwrap().send(());
glib::Continue(false)
glib::ControlFlow::Break
})
}))
}
Expand All @@ -144,9 +144,9 @@ impl<O: IsA<PollableOutputStream>> PollableOutputStreamExtManual for O {
let send = Some(send);
obj.create_source(cancellable.as_ref(), None, priority, move |_| {
if send.as_ref().unwrap().unbounded_send(()).is_err() {
glib::Continue(false)
glib::ControlFlow::Break
} else {
glib::Continue(true)
glib::ControlFlow::Continue
}
})
}))
Expand Down Expand Up @@ -218,7 +218,7 @@ impl<T: IsA<PollableOutputStream>> AsyncWrite for OutputStreamAsyncWrite<T> {
if let Some(waker) = waker.take() {
waker.wake();
}
glib::Continue(false)
glib::ControlFlow::Break
},
);
let main_context = glib::MainContext::ref_thread_default();
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<T: IsA<PollableOutputStream>> AsyncWrite for OutputStreamAsyncWrite<T> {
if let Some(waker) = waker.take() {
waker.wake();
}
glib::Continue(false)
glib::ControlFlow::Break
},
);
let main_context = glib::MainContext::ref_thread_default();
Expand Down
12 changes: 6 additions & 6 deletions gio/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ pub trait SocketExtManual: Sized {
func: F,
) -> glib::Source
where
F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static,
F: FnMut(&Self, glib::IOCondition) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>;

fn create_source_future<C: IsA<Cancellable>>(
Expand Down Expand Up @@ -775,12 +775,12 @@ impl<O: IsA<Socket>> SocketExtManual for O {
func: F,
) -> glib::Source
where
F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static,
F: FnMut(&Self, glib::IOCondition) -> glib::ControlFlow + 'static,
C: IsA<Cancellable>,
{
unsafe extern "C" fn trampoline<
O: IsA<Socket>,
F: FnMut(&O, glib::IOCondition) -> glib::Continue + 'static,
F: FnMut(&O, glib::IOCondition) -> glib::ControlFlow + 'static,
>(
socket: *mut ffi::GSocket,
condition: glib::ffi::GIOCondition,
Expand Down Expand Up @@ -843,7 +843,7 @@ impl<O: IsA<Socket>> SocketExtManual for O {
priority,
move |_, condition| {
let _ = send.take().unwrap().send(condition);
glib::Continue(false)
glib::ControlFlow::Break
},
)
}))
Expand All @@ -867,9 +867,9 @@ impl<O: IsA<Socket>> SocketExtManual for O {
priority,
move |_, condition| {
if send.as_ref().unwrap().unbounded_send(condition).is_err() {
glib::Continue(false)
glib::ControlFlow::Break
} else {
glib::Continue(true)
glib::ControlFlow::Continue
}
},
)
Expand Down
36 changes: 18 additions & 18 deletions glib/src/main_context_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use crate::{
thread_guard::ThreadGuard, translate::*, Continue, MainContext, Priority, Source, SourceId,
thread_guard::ThreadGuard, translate::*, ControlFlow, MainContext, Priority, Source, SourceId,
};

enum ChannelSourceState {
Expand Down Expand Up @@ -197,14 +197,14 @@ impl<T> Channel<T> {
}

#[repr(C)]
struct ChannelSource<T, F: FnMut(T) -> Continue + 'static> {
struct ChannelSource<T, F: FnMut(T) -> ControlFlow + 'static> {
source: ffi::GSource,
source_funcs: Box<ffi::GSourceFuncs>,
channel: Channel<T>,
callback: ThreadGuard<F>,
}

unsafe extern "C" fn dispatch<T, F: FnMut(T) -> Continue + 'static>(
unsafe extern "C" fn dispatch<T, F: FnMut(T) -> ControlFlow + 'static>(
source: *mut ffi::GSource,
callback: ffi::GSourceFunc,
_user_data: ffi::gpointer,
Expand All @@ -228,7 +228,7 @@ unsafe extern "C" fn dispatch<T, F: FnMut(T) -> Continue + 'static>(
Err(mpsc::TryRecvError::Empty) => break,
Err(mpsc::TryRecvError::Disconnected) => return ffi::G_SOURCE_REMOVE,
Ok(item) => {
if callback(item) == Continue(false) {
if callback(item).is_break() {
return ffi::G_SOURCE_REMOVE;
}
}
Expand All @@ -239,7 +239,7 @@ unsafe extern "C" fn dispatch<T, F: FnMut(T) -> Continue + 'static>(
}

#[cfg(feature = "v2_64")]
unsafe extern "C" fn dispose<T, F: FnMut(T) -> Continue + 'static>(source: *mut ffi::GSource) {
unsafe extern "C" fn dispose<T, F: FnMut(T) -> ControlFlow + 'static>(source: *mut ffi::GSource) {
let source = &mut *(source as *mut ChannelSource<T, F>);

// Set the source inside the channel to None so that all senders know that there
Expand All @@ -251,7 +251,7 @@ unsafe extern "C" fn dispose<T, F: FnMut(T) -> Continue + 'static>(source: *mut
}
}

unsafe extern "C" fn finalize<T, F: FnMut(T) -> Continue + 'static>(source: *mut ffi::GSource) {
unsafe extern "C" fn finalize<T, F: FnMut(T) -> ControlFlow + 'static>(source: *mut ffi::GSource) {
let source = &mut *(source as *mut ChannelSource<T, F>);

// Drop all memory we own by taking it out of the Options
Expand Down Expand Up @@ -453,7 +453,7 @@ impl<T> Receiver<T> {
///
/// This function panics if called from a thread that is not the owner of the provided
/// `context`, or, if `None` is provided, of the thread default main context.
pub fn attach<F: FnMut(T) -> Continue + 'static>(
pub fn attach<F: FnMut(T) -> ControlFlow + 'static>(
mut self,
context: Option<&MainContext>,
func: F,
Expand Down Expand Up @@ -599,9 +599,9 @@ mod tests {
*sum_clone.borrow_mut() += item;
if *sum_clone.borrow() == 6 {
l_clone.quit();
Continue(false)
ControlFlow::Break
} else {
Continue(true)
ControlFlow::Continue
}
});

Expand Down Expand Up @@ -633,7 +633,7 @@ mod tests {
let helper = Helper(l.clone());
receiver.attach(Some(&c), move |_| {
let _helper = &helper;
Continue(true)
ControlFlow::Continue
});

drop(sender);
Expand All @@ -657,7 +657,7 @@ mod tests {

let (sender, receiver) = MainContext::channel::<i32>(Priority::default());

let source_id = receiver.attach(Some(&c), move |_| Continue(true));
let source_id = receiver.attach(Some(&c), move |_| ControlFlow::Continue);

let source = c.find_source_by_id(&source_id).unwrap();
source.destroy();
Expand All @@ -684,7 +684,7 @@ mod tests {
let helper = Helper(dropped.clone());
let source_id = receiver.attach(Some(&c), move |_| {
let _helper = &helper;
Continue(true)
ControlFlow::Continue
});

let source = c.find_source_by_id(&source_id).unwrap();
Expand Down Expand Up @@ -713,9 +713,9 @@ mod tests {
*sum_clone.borrow_mut() += item;
if *sum_clone.borrow() == 6 {
l_clone.quit();
Continue(false)
ControlFlow::Break
} else {
Continue(true)
ControlFlow::Continue
}
});

Expand Down Expand Up @@ -762,9 +762,9 @@ mod tests {
*sum_clone.borrow_mut() += item;
if *sum_clone.borrow() == 6 {
l_clone.quit();
Continue(false)
ControlFlow::Break
} else {
Continue(true)
ControlFlow::Continue
}
});

Expand Down Expand Up @@ -873,15 +873,15 @@ mod tests {
Err(mpsc::RecvTimeoutError::Disconnected)
);
l_clone.quit();
Continue(false)
ControlFlow::Break
} else {
// But as we didn't consume the next one yet, there must be no
// other item available yet
assert_eq!(
wait_receiver.recv_timeout(time::Duration::from_millis(50)),
Err(mpsc::RecvTimeoutError::Timeout)
);
Continue(true)
ControlFlow::Continue
}
});
l.run();
Expand Down
4 changes: 2 additions & 2 deletions glib/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
//! Traits and essential types intended for blanket imports.

pub use crate::{
param_spec::ParamSpecBuilderExt, Cast, CastNone, Continue, IsA, ObjectExt, ObjectType,
ParamSpecType, StaticType, StaticTypeExt, StaticVariantType, ToSendValue, ToValue, ToVariant,
param_spec::ParamSpecBuilderExt, Cast, CastNone, IsA, ObjectExt, ObjectType, ParamSpecType,
StaticType, StaticTypeExt, StaticVariantType, ToSendValue, ToValue, ToVariant,
};
Loading

0 comments on commit ec31031

Please sign in to comment.