Skip to content

gio: Return glib::ExitCode from gio::Application signal handlers #1694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- stable
- beta
- nightly
- "1.80.0"
- "1.83.0"
conf:
- { name: "cairo", features: "png,pdf,svg,ps,use_glib,v1_18,freetype,script,xcb,xlib,win32-surface", nightly: "--features 'png,pdf,svg,ps,use_glib,v1_18,freetype,script,xcb,xlib,win32-surface'", test_sys: true }
- { name: "gdk-pixbuf", features: "v2_42", nightly: "--all-features", test_sys: true }
Expand Down Expand Up @@ -104,7 +104,7 @@ jobs:
- stable
- beta
- nightly
- "1.80.0"
- "1.83.0"
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repository = "https://github.com/gtk-rs/gtk-rs-core"
license = "MIT"
exclude = ["gir-files/*"]
edition = "2021"
rust-version = "1.80"
rust-version = "1.83"

[workspace.dependencies]
libc = "0.2"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ information about each crate, please refer to their `README.md` file in their di

## Minimum supported Rust version

Currently, the minimum supported Rust version is `1.80.0`.
Currently, the minimum supported Rust version is `1.83.0`.

## Documentation

Expand Down
2 changes: 1 addition & 1 deletion cairo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Cairo __1.14__ is the lowest supported version for the underlying library.

## Minimum supported Rust version

Currently, the minimum supported Rust version is `1.80.0`.
Currently, the minimum supported Rust version is `1.83.0`.

## Default-on features

Expand Down
2 changes: 1 addition & 1 deletion gdk-pixbuf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GDK-PixBuf __2.36.8__ is the lowest supported version for the underlying library

## Minimum supported Rust version

Currently, the minimum supported Rust version is `1.80.0`.
Currently, the minimum supported Rust version is `1.83.0`.

## Documentation

Expand Down
8 changes: 8 additions & 0 deletions gio/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ generate_builder = true
name = "open"
manual = true
doc_trait_name = "ApplicationExtManual"
[[object.signal]]
name = "command-line"
manual = true
doc_trait_name = "ApplicationExtManual"
[[object.signal]]
name = "handle-local-options"
manual = true
doc_trait_name = "ApplicationExtManual"
[[object.function]]
name = "run"
manual = true
Expand Down
2 changes: 1 addition & 1 deletion gio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GIO __2.56__ is the lowest supported version for the underlying library.

## Minimum supported Rust version

Currently, the minimum supported Rust version is `1.80.0`.
Currently, the minimum supported Rust version is `1.83.0`.

## Documentation

Expand Down
79 changes: 76 additions & 3 deletions gio/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{boxed::Box as Box_, mem::transmute};
use std::{boxed::Box as Box_, mem::transmute, ops::ControlFlow};

use glib::{
prelude::*,
Expand All @@ -9,22 +9,25 @@
ExitCode, GString,
};

use crate::{ffi, Application, File};
use crate::{ffi, Application, ApplicationCommandLine, File};

pub trait ApplicationExtManual: IsA<Application> {
#[doc(alias = "g_application_run")]
fn run(&self) -> ExitCode {
self.run_with_args(&std::env::args().collect::<Vec<_>>())
}

#[doc(alias = "g_application_run")]
fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> ExitCode {
let argv: Vec<&str> = args.iter().map(|a| a.as_ref()).collect();
let argc = argv.len() as i32;
let exit_code = unsafe {
ffi::g_application_run(self.as_ref().to_glib_none().0, argc, argv.to_glib_none().0)
};
ExitCode::from(exit_code)
ExitCode::try_from(exit_code).unwrap()
}

#[doc(alias = "open")]
fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn open_trampoline<P, F: Fn(&P, &[File], &str) + 'static>(
this: *mut ffi::GApplication,
Expand Down Expand Up @@ -56,6 +59,76 @@
}
}

#[doc(alias = "command-line")]
fn connect_command_line<F: Fn(&Self, &ApplicationCommandLine) -> ExitCode + 'static>(
&self,
f: F,
) -> SignalHandlerId {
unsafe extern "C" fn command_line_trampoline<
P: IsA<Application>,
F: Fn(&P, &ApplicationCommandLine) -> ExitCode + 'static,

Check warning on line 69 in gio/src/application.rs

View workflow job for this annotation

GitHub Actions / build

unresolved link to `0`
>(
this: *mut ffi::GApplication,
command_line: *mut ffi::GApplicationCommandLine,
f: glib::ffi::gpointer,
) -> std::ffi::c_int {
let f: &F = &*(f as *const F);
f(
Application::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(command_line),
)
.into()
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"command-line".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
command_line_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}

#[doc(alias = "handle-local-options")]
fn connect_handle_local_options<
F: Fn(&Self, &glib::VariantDict) -> ControlFlow<ExitCode> + 'static,
>(
&self,
f: F,
) -> SignalHandlerId {
unsafe extern "C" fn handle_local_options_trampoline<
P: IsA<Application>,
F: Fn(&P, &glib::VariantDict) -> ControlFlow<ExitCode> + 'static,
>(
this: *mut ffi::GApplication,
options: *mut glib::ffi::GVariantDict,
f: glib::ffi::gpointer,
) -> std::ffi::c_int {
let f: &F = &*(f as *const F);
f(
Application::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(options),
)
.break_value()
.map(i32::from)
.unwrap_or(-1)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"handle-local-options".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
handle_local_options_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}

#[doc(alias = "g_application_hold")]
fn hold(&self) -> ApplicationHoldGuard {
unsafe {
Expand Down
67 changes: 1 addition & 66 deletions gio/src/auto/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// DO NOT EDIT

use crate::{
ffi, ActionGroup, ActionMap, ApplicationCommandLine, ApplicationFlags, Cancellable,
DBusConnection, File, Notification,
ffi, ActionGroup, ActionMap, ApplicationFlags, Cancellable, DBusConnection, File, Notification,
};
use glib::{
object::ObjectType as _,
Expand Down Expand Up @@ -464,70 +463,6 @@
}
}

#[doc(alias = "command-line")]
fn connect_command_line<F: Fn(&Self, &ApplicationCommandLine) -> i32 + 'static>(
&self,
f: F,
) -> SignalHandlerId {
unsafe extern "C" fn command_line_trampoline<
P: IsA<Application>,
F: Fn(&P, &ApplicationCommandLine) -> i32 + 'static,
>(
this: *mut ffi::GApplication,
command_line: *mut ffi::GApplicationCommandLine,
f: glib::ffi::gpointer,
) -> std::ffi::c_int {
let f: &F = &*(f as *const F);
f(
Application::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(command_line),
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"command-line".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
command_line_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}

#[doc(alias = "handle-local-options")]
fn connect_handle_local_options<F: Fn(&Self, &glib::VariantDict) -> i32 + 'static>(
&self,
f: F,
) -> SignalHandlerId {
unsafe extern "C" fn handle_local_options_trampoline<
P: IsA<Application>,
F: Fn(&P, &glib::VariantDict) -> i32 + 'static,
>(
this: *mut ffi::GApplication,
options: *mut glib::ffi::GVariantDict,
f: glib::ffi::gpointer,
) -> std::ffi::c_int {
let f: &F = &*(f as *const F);
f(
Application::from_glib_borrow(this).unsafe_cast_ref(),
&from_glib_borrow(options),
)
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"handle-local-options".as_ptr() as *const _,
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
handle_local_options_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}

#[cfg(feature = "v2_60")]
#[cfg_attr(docsrs, doc(cfg(feature = "v2_60")))]
#[doc(alias = "name-lost")]
Expand Down
2 changes: 1 addition & 1 deletion gio/src/gio_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ where
&& self
.receiver
.as_ref()
.map_or(true, |receiver| receiver.is_terminated())
.is_none_or(|receiver| receiver.is_terminated())
}
}

Expand Down
Loading
Loading