1
- #![ feature( trace_macros) ]
1
+ #![ feature( trace_macros, alloc) ]
2
+ #[ macro_use] extern crate enum_primitive;
2
3
pub mod signals {
3
4
extern crate libc;
4
- use std:: os ;
5
+ use std:: io ;
5
6
use std:: mem;
6
7
7
- #[ derive( Hash , Eq , PartialEq , Copy , Debug , FromPrimitive ) ]
8
+ enum_from_primitive ! {
9
+ #[ derive( Hash , Eq , PartialEq , Copy , Clone , Debug ) ]
8
10
pub enum Signal {
9
11
None = 0 ,
10
12
Hup ,
@@ -39,25 +41,26 @@ pub mod signals {
39
41
Pwr ,
40
42
Sys
41
43
}
44
+ }
42
45
43
46
impl Signal {
44
- pub fn raise ( self ) -> Result < ( ) , usize > {
47
+ pub fn raise ( self ) -> Result < ( ) , libc :: c_int > {
45
48
match unsafe { raise ( self as libc:: c_int ) } {
46
49
0 => Result :: Ok ( ( ) ) ,
47
- _ => Result :: Err ( os :: errno ( ) )
50
+ _ => Result :: Err ( io :: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) )
48
51
}
49
52
}
50
53
51
- pub fn kill ( self , pid : libc:: pid_t ) -> Result < ( ) , usize > {
54
+ pub fn kill ( self , pid : libc:: pid_t ) -> Result < ( ) , libc :: c_int > {
52
55
match unsafe { kill ( pid, self as libc:: c_int ) } {
53
56
0 => Result :: Ok ( ( ) ) ,
54
- _ => Result :: Err ( os :: errno ( ) )
57
+ _ => Result :: Err ( io :: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) )
55
58
}
56
59
}
57
60
58
- pub unsafe fn handle ( self , handler : Box < FnMut ( Signal ) > ) -> Result < ( ) , usize > {
59
- match unsafe { signal ( self as libc:: c_int , mem:: transmute ( glue:: rust_signal_handler) ) } {
60
- -1 => Result :: Err ( os :: errno ( ) ) ,
61
+ pub unsafe fn handle ( self , handler : Box < FnMut ( Signal ) > ) -> Result < ( ) , libc :: c_int > {
62
+ match signal ( self as libc:: c_int , mem:: transmute ( glue:: rust_signal_handler) ) {
63
+ -1 => Result :: Err ( io :: Error :: last_os_error ( ) . raw_os_error ( ) . unwrap ( ) ) ,
61
64
_ => { glue:: set_handler ( self , handler) ; Result :: Ok ( ( ) ) }
62
65
}
63
66
}
@@ -66,14 +69,12 @@ pub mod signals {
66
69
mod glue {
67
70
extern crate libc;
68
71
extern crate alloc;
72
+ extern crate num;
73
+ use self :: num:: FromPrimitive ;
69
74
use super :: Signal ;
70
- use std:: num:: FromPrimitive ;
71
- use self :: alloc:: arc:: Arc ;
72
- use std:: rc:: Rc ;
73
75
use std:: mem;
74
- use std:: ptr;
75
76
76
- #[ derive( Copy , Debug ) ]
77
+ #[ derive( Copy , Clone , Debug ) ]
77
78
struct FnPtr {
78
79
foo : usize ,
79
80
bar : usize
@@ -104,13 +105,11 @@ pub mod signals {
104
105
handlers[ sig as usize ] = mem:: transmute ( f) ;
105
106
}
106
107
107
- fn null_handler ( s : Signal ) { }
108
-
109
108
pub unsafe extern "C" fn rust_signal_handler ( sig : libc:: c_int ) {
110
109
let f: * mut FnMut ( Signal ) = mem:: transmute ( handlers[ sig as usize ] ) ;
111
110
let p: FnPtr = mem:: transmute ( f) ;
112
111
if p. foo != 0 && p. bar != 0 {
113
- match FromPrimitive :: from_i32 ( sig) {
112
+ match Signal :: from_i32 ( sig) {
114
113
Some ( s) => ( * f) ( s) ,
115
114
None => panic ! ( "Unknown signal {}" , sig)
116
115
}
0 commit comments