sigval get sival_int 的 Rust 实现
Rust implementation of sigval get sival_int
在 C 的 sigval 的 Rust 实现中,只有 sival_ptr 存在,有没有办法得到 sival_int?
这就是 sigval 在 C:
中的样子
union sigval {
int sival_int;
void *sival_ptr;
};
这是生锈的样子:
#[repr(C)]
pub struct sigval {
pub sival_ptr: *mut c_void,
}
鉴于 C 版本是 union
,您可以将 *mut c_void
指针转换为 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);
}
}
在 C 的 sigval 的 Rust 实现中,只有 sival_ptr 存在,有没有办法得到 sival_int?
这就是 sigval 在 C:
union sigval {
int sival_int;
void *sival_ptr;
};
这是生锈的样子:
#[repr(C)]
pub struct sigval {
pub sival_ptr: *mut c_void,
}
鉴于 C 版本是 union
,您可以将 *mut c_void
指针转换为 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);
}
}