rust: add bindings for signal conversion functions

This commit is contained in:
Xiretza 2023-02-11 17:36:08 +01:00 committed by Johannes Altmanninger
parent e6e866e455
commit 333056a9ec
2 changed files with 30 additions and 0 deletions

View File

@ -70,6 +70,9 @@ include_cpp! {
generate!("event_fire_generic")
generate!("escape_string")
generate!("sig2wcs")
generate!("wcs2sig")
generate!("signal_get_desc")
}
impl parser_t {

View File

@ -1,4 +1,8 @@
use widestring::U32CStr;
use crate::ffi;
use crate::topic_monitor::{generation_t, invalid_generations, topic_monitor_principal, topic_t};
use crate::wchar_ffi::{c_str, wstr};
/// A sigint_detector_t can be used to check if a SIGINT (or SIGHUP) has been delivered.
pub struct sigchecker_t {
@ -38,3 +42,26 @@ impl sigchecker_t {
tm.check(&mut gens, true /* wait */);
}
}
/// Get the integer signal value representing the specified signal.
pub fn wcs2sig(s: &wstr) -> Option<usize> {
let sig = ffi::wcs2sig(c_str!(s));
sig.0.try_into().ok()
}
/// Get string representation of a signal.
pub fn sig2wcs(sig: usize) -> &'static wstr {
let s = ffi::sig2wcs(i32::try_from(sig).expect("signal should be < 2^31").into());
let s = unsafe { U32CStr::from_ptr_str(s) };
wstr::from_ucstr(s).expect("signal name should be valid utf-32")
}
/// Returns a description of the specified signal.
pub fn signal_get_desc(sig: usize) -> &'static wstr {
let s = ffi::signal_get_desc(i32::try_from(sig).expect("signal should be < 2^31").into());
let s = unsafe { U32CStr::from_ptr_str(s) };
wstr::from_ucstr(s).expect("signal description should be valid utf-32")
}