Rust implementation of sigval get sival_int

Viewed 50

In the rust implementation of sigval from C only the sival_ptr is present, is there a way to get sival_int?
This is how sigval looks in C:

union sigval {
               int   sival_int;
               void *sival_ptr;
           };

And this is how it looks in rust:

#[repr(C)]
pub struct sigval {
    pub sival_ptr: *mut c_void,
}
1 Answers

Given that the C version is a union, you can cast the *mut c_void pointer to c_int:

use std::ffi::c_void;
use std::os::raw::c_int;

//  A dummy struct for the sake of the test. 
#[repr(C)]
union Signal {
    sival_int: c_int,
    sival_ptr: *mut c_void,
}

fn main() {
    let x = Signal {
        sival_int: 0x01020304,
    };

    unsafe {
        let x = x.sival_ptr as c_int;
        println!("{:0X}", x);
    }
}
Related