@@ -6,9 +6,9 @@ use crate::{Errno, Result};
66use libc:: { timespec, time_t, c_int, c_long, intptr_t, uintptr_t} ;
77#[ cfg( target_os = "netbsd" ) ]
88use libc:: { timespec, time_t, c_long, intptr_t, uintptr_t, size_t} ;
9+ use std:: convert:: TryInto ;
910use std:: os:: unix:: io:: RawFd ;
1011use std:: ptr;
11- use std:: mem;
1212
1313// Redefine kevent in terms of programmer-friendly enums and bitfields.
1414#[ repr( C ) ]
@@ -76,6 +76,7 @@ libc_enum! {
7676 EVFILT_VNODE ,
7777 EVFILT_WRITE ,
7878 }
79+ impl TryFrom <type_of_event_filter>
7980}
8081
8182#[ cfg( any( target_os = "dragonfly" , target_os = "freebsd" ,
@@ -233,8 +234,8 @@ impl KEvent {
233234 self . kevent . ident
234235 }
235236
236- pub fn filter ( & self ) -> EventFilter {
237- unsafe { mem :: transmute ( self . kevent . filter as type_of_event_filter ) }
237+ pub fn filter ( & self ) -> Result < EventFilter > {
238+ self . kevent . filter . try_into ( )
238239 }
239240
240241 pub fn flags ( & self ) -> EventFlag {
@@ -313,6 +314,8 @@ pub fn ev_set(ev: &mut KEvent,
313314
314315#[ test]
315316fn test_struct_kevent ( ) {
317+ use std:: mem;
318+
316319 let udata : intptr_t = 12345 ;
317320
318321 let actual = KEvent :: new ( 0xdead_beef ,
@@ -322,10 +325,24 @@ fn test_struct_kevent() {
322325 0x1337 ,
323326 udata) ;
324327 assert_eq ! ( 0xdead_beef , actual. ident( ) ) ;
325- assert_eq ! ( libc:: EVFILT_READ , actual. filter( ) as type_of_event_filter) ;
328+ let filter = actual. kevent . filter ;
329+ assert_eq ! ( libc:: EVFILT_READ , filter) ;
326330 assert_eq ! ( libc:: EV_ONESHOT | libc:: EV_ADD , actual. flags( ) . bits( ) ) ;
327331 assert_eq ! ( libc:: NOTE_CHILD | libc:: NOTE_EXIT , actual. fflags( ) . bits( ) ) ;
328332 assert_eq ! ( 0x1337 , actual. data( ) as type_of_data) ;
329333 assert_eq ! ( udata as type_of_udata, actual. udata( ) as type_of_udata) ;
330334 assert_eq ! ( mem:: size_of:: <libc:: kevent>( ) , mem:: size_of:: <KEvent >( ) ) ;
331335}
336+
337+ #[ test]
338+ fn test_kevent_filter ( ) {
339+ let udata : intptr_t = 12345 ;
340+
341+ let actual = KEvent :: new ( 0xdead_beef ,
342+ EventFilter :: EVFILT_READ ,
343+ EventFlag :: EV_ONESHOT | EventFlag :: EV_ADD ,
344+ FilterFlag :: NOTE_CHILD | FilterFlag :: NOTE_EXIT ,
345+ 0x1337 ,
346+ udata) ;
347+ assert_eq ! ( EventFilter :: EVFILT_READ , actual. filter( ) . unwrap( ) ) ;
348+ }
0 commit comments