Skip to content

Commit 3999fc5

Browse files
authored
Merge pull request #880 from A6GibKm/exit-code
application: Return ExitCode
2 parents f3374ed + 41dfd34 commit 3999fc5

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

gio/src/application.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ use glib::{
66
prelude::*,
77
signal::{connect_raw, SignalHandlerId},
88
translate::*,
9-
GString,
9+
ExitCode, GString,
1010
};
1111

1212
use crate::{Application, File};
1313

1414
pub trait ApplicationExtManual {
1515
#[doc(alias = "g_application_run")]
16-
fn run(&self) -> i32;
16+
fn run(&self) -> ExitCode;
1717
#[doc(alias = "g_application_run")]
18-
fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> i32;
18+
fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> ExitCode;
1919
fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId;
2020

2121
#[doc(alias = "g_application_hold")]
@@ -26,16 +26,17 @@ pub trait ApplicationExtManual {
2626
}
2727

2828
impl<O: IsA<Application>> ApplicationExtManual for O {
29-
fn run(&self) -> i32 {
29+
fn run(&self) -> ExitCode {
3030
self.run_with_args(&std::env::args().collect::<Vec<_>>())
3131
}
3232

33-
fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> i32 {
33+
fn run_with_args<S: AsRef<str>>(&self, args: &[S]) -> ExitCode {
3434
let argv: Vec<&str> = args.iter().map(|a| a.as_ref()).collect();
3535
let argc = argv.len() as i32;
36-
unsafe {
36+
let exit_code = unsafe {
3737
ffi::g_application_run(self.as_ref().to_glib_none().0, argc, argv.to_glib_none().0)
38-
}
38+
};
39+
ExitCode::from(exit_code)
3940
}
4041

4142
fn connect_open<F: Fn(&Self, &[File], &str) + 'static>(&self, f: F) -> SignalHandlerId {

gio/src/subclass/application.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::{ffi::OsString, fmt, ops::Deref, ptr};
44

5-
use glib::{subclass::prelude::*, translate::*, Cast, VariantDict};
5+
use glib::{subclass::prelude::*, translate::*, Cast, ExitCode, VariantDict};
66
use libc::{c_char, c_int, c_void};
77

88
use crate::Application;
@@ -77,11 +77,11 @@ pub trait ApplicationImpl: ObjectImpl + ApplicationImplExt {
7777
self.parent_before_emit(platform_data)
7878
}
7979

80-
fn command_line(&self, command_line: &crate::ApplicationCommandLine) -> i32 {
80+
fn command_line(&self, command_line: &crate::ApplicationCommandLine) -> ExitCode {
8181
self.parent_command_line(command_line)
8282
}
8383

84-
fn local_command_line(&self, arguments: &mut ArgumentList) -> Option<i32> {
84+
fn local_command_line(&self, arguments: &mut ArgumentList) -> Option<ExitCode> {
8585
self.parent_local_command_line(arguments)
8686
}
8787

@@ -105,7 +105,7 @@ pub trait ApplicationImpl: ObjectImpl + ApplicationImplExt {
105105
self.parent_startup()
106106
}
107107

108-
fn handle_local_options(&self, options: &VariantDict) -> i32 {
108+
fn handle_local_options(&self, options: &VariantDict) -> ExitCode {
109109
self.parent_handle_local_options(options)
110110
}
111111
}
@@ -114,14 +114,14 @@ pub trait ApplicationImplExt: ObjectSubclass {
114114
fn parent_activate(&self);
115115
fn parent_after_emit(&self, platform_data: &glib::Variant);
116116
fn parent_before_emit(&self, platform_data: &glib::Variant);
117-
fn parent_command_line(&self, command_line: &crate::ApplicationCommandLine) -> i32;
118-
fn parent_local_command_line(&self, arguments: &mut ArgumentList) -> Option<i32>;
117+
fn parent_command_line(&self, command_line: &crate::ApplicationCommandLine) -> ExitCode;
118+
fn parent_local_command_line(&self, arguments: &mut ArgumentList) -> Option<ExitCode>;
119119
fn parent_open(&self, files: &[crate::File], hint: &str);
120120
fn parent_quit_mainloop(&self);
121121
fn parent_run_mainloop(&self);
122122
fn parent_shutdown(&self);
123123
fn parent_startup(&self);
124-
fn parent_handle_local_options(&self, options: &VariantDict) -> i32;
124+
fn parent_handle_local_options(&self, options: &VariantDict) -> ExitCode;
125125
}
126126

127127
impl<T: ApplicationImpl> ApplicationImplExt for T {
@@ -164,7 +164,7 @@ impl<T: ApplicationImpl> ApplicationImplExt for T {
164164
}
165165
}
166166

167-
fn parent_command_line(&self, command_line: &crate::ApplicationCommandLine) -> i32 {
167+
fn parent_command_line(&self, command_line: &crate::ApplicationCommandLine) -> ExitCode {
168168
unsafe {
169169
let data = T::type_data();
170170
let parent_class = data.as_ref().parent_class() as *mut ffi::GApplicationClass;
@@ -175,10 +175,11 @@ impl<T: ApplicationImpl> ApplicationImplExt for T {
175175
self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
176176
command_line.to_glib_none().0,
177177
)
178+
.into()
178179
}
179180
}
180181

181-
fn parent_local_command_line(&self, arguments: &mut ArgumentList) -> Option<i32> {
182+
fn parent_local_command_line(&self, arguments: &mut ArgumentList) -> Option<ExitCode> {
182183
unsafe {
183184
let data = T::type_data();
184185
let parent_class = data.as_ref().parent_class() as *mut ffi::GApplicationClass;
@@ -196,7 +197,7 @@ impl<T: ApplicationImpl> ApplicationImplExt for T {
196197

197198
match res {
198199
glib::ffi::GFALSE => None,
199-
_ => Some(exit_status),
200+
_ => Some(exit_status.into()),
200201
}
201202
}
202203
}
@@ -261,7 +262,7 @@ impl<T: ApplicationImpl> ApplicationImplExt for T {
261262
}
262263
}
263264

264-
fn parent_handle_local_options(&self, options: &VariantDict) -> i32 {
265+
fn parent_handle_local_options(&self, options: &VariantDict) -> ExitCode {
265266
unsafe {
266267
let data = T::type_data();
267268
let parent_class = data.as_ref().parent_class() as *mut ffi::GApplicationClass;
@@ -270,9 +271,10 @@ impl<T: ApplicationImpl> ApplicationImplExt for T {
270271
self.obj().unsafe_cast_ref::<Application>().to_glib_none().0,
271272
options.to_glib_none().0,
272273
)
274+
.into()
273275
} else {
274276
// Continue default handling
275-
-1
277+
ExitCode::from(-1)
276278
}
277279
}
278280
}
@@ -329,7 +331,7 @@ unsafe extern "C" fn application_command_line<T: ApplicationImpl>(
329331
let instance = &*(ptr as *mut T::Instance);
330332
let imp = instance.imp();
331333

332-
imp.command_line(&from_glib_borrow(command_line))
334+
imp.command_line(&from_glib_borrow(command_line)).into()
333335
}
334336
unsafe extern "C" fn application_local_command_line<T: ApplicationImpl>(
335337
ptr: *mut ffi::GApplication,
@@ -340,7 +342,7 @@ unsafe extern "C" fn application_local_command_line<T: ApplicationImpl>(
340342
let imp = instance.imp();
341343

342344
let mut args = ArgumentList::new(arguments);
343-
let res = imp.local_command_line(&mut args);
345+
let res = imp.local_command_line(&mut args).map(i32::from);
344346
args.refresh();
345347

346348
match res {
@@ -395,7 +397,7 @@ unsafe extern "C" fn application_handle_local_options<T: ApplicationImpl>(
395397
let instance = &*(ptr as *mut T::Instance);
396398
let imp = instance.imp();
397399

398-
imp.handle_local_options(&from_glib_borrow(options))
400+
imp.handle_local_options(&from_glib_borrow(options)).into()
399401
}
400402

401403
#[cfg(test)]
@@ -421,7 +423,7 @@ mod tests {
421423
impl ObjectImpl for SimpleApplication {}
422424

423425
impl ApplicationImpl for SimpleApplication {
424-
fn command_line(&self, cmd_line: &crate::ApplicationCommandLine) -> i32 {
426+
fn command_line(&self, cmd_line: &crate::ApplicationCommandLine) -> ExitCode {
425427
let arguments = cmd_line.arguments();
426428

427429
for arg in arguments {
@@ -430,10 +432,10 @@ mod tests {
430432
assert!(!a.starts_with("--local-"))
431433
}
432434

433-
EXIT_STATUS
435+
EXIT_STATUS.into()
434436
}
435437

436-
fn local_command_line(&self, arguments: &mut ArgumentList) -> Option<i32> {
438+
fn local_command_line(&self, arguments: &mut ArgumentList) -> Option<ExitCode> {
437439
let mut rm = Vec::new();
438440

439441
for (i, line) in arguments.iter().enumerate() {
@@ -469,6 +471,6 @@ mod tests {
469471

470472
app.set_inactivity_timeout(10000);
471473

472-
assert_eq!(app.run_with_args(&["--local"]), EXIT_STATUS);
474+
assert_eq!(app.run_with_args(&["--local"]), EXIT_STATUS.into());
473475
}
474476
}

glib/src/exit_code.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use std::process::Termination;
4+
5+
#[derive(Clone, Copy, PartialEq, Eq, Debug, PartialOrd, Ord)]
6+
pub struct ExitCode(i32);
7+
8+
impl ExitCode {
9+
pub const SUCCESS: Self = Self(0);
10+
pub const FAILURE: Self = Self(1);
11+
12+
pub fn value(&self) -> i32 {
13+
self.0
14+
}
15+
}
16+
17+
impl From<i32> for ExitCode {
18+
fn from(value: i32) -> Self {
19+
Self(value)
20+
}
21+
}
22+
23+
impl From<ExitCode> for i32 {
24+
fn from(value: ExitCode) -> Self {
25+
value.0
26+
}
27+
}
28+
29+
impl Termination for ExitCode {
30+
fn report(self) -> std::process::ExitCode {
31+
std::process::ExitCode::from(self.0 as u8)
32+
}
33+
}

glib/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ pub mod object;
105105

106106
mod boxed_any_object;
107107
pub use boxed_any_object::BoxedAnyObject;
108+
mod exit_code;
109+
pub use exit_code::ExitCode;
108110

109111
pub mod collections;
110112
pub use collections::{

0 commit comments

Comments
 (0)