@@ -122,8 +122,8 @@ fn flush_input_from_file(dev_file: &mut File, max: usize) -> io::Result<usize> {
122
122
/// Get the pin value from the provided file
123
123
fn get_value_from_file ( dev_file : & mut File ) -> Result < u8 > {
124
124
let mut s = String :: with_capacity ( 10 ) ;
125
- try! ( dev_file. seek ( SeekFrom :: Start ( 0 ) ) ) ;
126
- try! ( dev_file. read_to_string ( & mut s) ) ;
125
+ dev_file. seek ( SeekFrom :: Start ( 0 ) ) ? ;
126
+ dev_file. read_to_string ( & mut s) ? ;
127
127
match s[ ..1 ] . parse :: < u8 > ( ) {
128
128
Ok ( n) => Ok ( n) ,
129
129
Err ( _) => Err ( Error :: Unexpected ( format ! ( "Unexpected value file contents: {:?}" , s) ) ) ,
@@ -134,16 +134,16 @@ impl Pin {
134
134
/// Write all of the provided contents to the specified devFile
135
135
fn write_to_device_file ( & self , dev_file_name : & str , value : & str ) -> io:: Result < ( ) > {
136
136
let gpio_path = format ! ( "/sys/class/gpio/gpio{}/{}" , self . pin_num, dev_file_name) ;
137
- let mut dev_file = try! ( File :: create ( & gpio_path) ) ;
138
- try! ( dev_file. write_all ( value. as_bytes ( ) ) ) ;
137
+ let mut dev_file = File :: create ( & gpio_path) ? ;
138
+ dev_file. write_all ( value. as_bytes ( ) ) ? ;
139
139
Ok ( ( ) )
140
140
}
141
141
142
142
fn read_from_device_file ( & self , dev_file_name : & str ) -> io:: Result < String > {
143
143
let gpio_path = format ! ( "/sys/class/gpio/gpio{}/{}" , self . pin_num, dev_file_name) ;
144
- let mut dev_file = try! ( File :: open ( & gpio_path) ) ;
144
+ let mut dev_file = File :: open ( & gpio_path) ? ;
145
145
let mut s = String :: new ( ) ;
146
- try! ( dev_file. read_to_string ( & mut s) ) ;
146
+ dev_file. read_to_string ( & mut s) ? ;
147
147
Ok ( s)
148
148
}
149
149
@@ -168,12 +168,13 @@ impl Pin {
168
168
/// will return an error.
169
169
pub fn from_path < T : AsRef < Path > > ( path : T ) -> Result < Pin > {
170
170
// Resolve all symbolic links in the provided path
171
- let pb = try! ( fs:: canonicalize ( path. as_ref ( ) ) ) ;
171
+ let pb = fs:: canonicalize ( path. as_ref ( ) ) ? ;
172
172
173
173
// determine if this is valid and figure out the pin_num
174
- if !try! ( fs:: metadata ( & pb) ) . is_dir ( ) {
174
+ if !fs:: metadata ( & pb) ? . is_dir ( ) {
175
175
return Err ( Error :: Unexpected ( "Provided path not a directory or symlink to \
176
- a directory". to_owned ( ) ) ) ;
176
+ a directory"
177
+ . to_owned ( ) ) ) ;
177
178
}
178
179
179
180
let re = regex:: Regex :: new ( r"^/sys/.*?/gpio/gpio(\d+)$" ) . unwrap ( ) ;
@@ -183,10 +184,12 @@ impl Pin {
183
184
} ;
184
185
185
186
let num: u64 = match caps. at ( 1 ) {
186
- Some ( num) => match num. parse ( ) {
187
- Ok ( unum) => unum,
188
- Err ( _) => return Err ( Error :: InvalidPath ( format ! ( "{:?}" , pb) ) ) ,
189
- } ,
187
+ Some ( num) => {
188
+ match num. parse ( ) {
189
+ Ok ( unum) => unum,
190
+ Err ( _) => return Err ( Error :: InvalidPath ( format ! ( "{:?}" , pb) ) ) ,
191
+ }
192
+ }
190
193
None => return Err ( Error :: InvalidPath ( format ! ( "{:?}" , pb) ) ) ,
191
194
} ;
192
195
@@ -221,14 +224,14 @@ impl Pin {
221
224
#[ inline]
222
225
pub fn with_exported < F : FnOnce ( ) -> Result < ( ) > > ( & self , closure : F ) -> Result < ( ) > {
223
226
224
- try! ( self . export ( ) ) ;
227
+ self . export ( ) ? ;
225
228
match closure ( ) {
226
229
Ok ( ( ) ) => {
227
230
try!( self . unexport ( ) ) ;
228
231
Ok ( ( ) )
229
232
}
230
233
Err ( err) => {
231
- try! ( self . unexport ( ) ) ;
234
+ self . unexport ( ) ? ;
232
235
Err ( err)
233
236
}
234
237
}
@@ -269,8 +272,9 @@ impl Pin {
269
272
/// ```
270
273
pub fn export ( & self ) -> Result < ( ) > {
271
274
if fs:: metadata ( & format ! ( "/sys/class/gpio/gpio{}" , self . pin_num) ) . is_err ( ) {
272
- let mut export_file = try!( File :: create ( "/sys/class/gpio/export" ) ) ;
273
- try!( export_file. write_all ( format ! ( "{}" , self . pin_num) . as_bytes ( ) ) ) ;
275
+ let mut export_file = File :: create ( "/sys/class/gpio/export" ) ?;
276
+ export_file
277
+ . write_all ( format ! ( "{}" , self . pin_num) . as_bytes ( ) ) ?;
274
278
}
275
279
Ok ( ( ) )
276
280
}
@@ -283,8 +287,9 @@ impl Pin {
283
287
/// this function returns Ok, the GPIO is not exported.
284
288
pub fn unexport ( & self ) -> Result < ( ) > {
285
289
if fs:: metadata ( & format ! ( "/sys/class/gpio/gpio{}" , self . pin_num) ) . is_ok ( ) {
286
- let mut unexport_file = try!( File :: create ( "/sys/class/gpio/unexport" ) ) ;
287
- try!( unexport_file. write_all ( format ! ( "{}" , self . pin_num) . as_bytes ( ) ) ) ;
290
+ let mut unexport_file = File :: create ( "/sys/class/gpio/unexport" ) ?;
291
+ unexport_file
292
+ . write_all ( format ! ( "{}" , self . pin_num) . as_bytes ( ) ) ?;
288
293
}
289
294
Ok ( ( ) )
290
295
}
@@ -324,13 +329,12 @@ impl Pin {
324
329
/// not support changing the direction of a pin in userspace. If
325
330
/// this is the case, you will get an error.
326
331
pub fn set_direction ( & self , dir : Direction ) -> Result < ( ) > {
327
- try!( self . write_to_device_file ( "direction" ,
328
- match dir {
329
- Direction :: In => "in" ,
330
- Direction :: Out => "out" ,
331
- Direction :: High => "high" ,
332
- Direction :: Low => "low" ,
333
- } ) ) ;
332
+ self . write_to_device_file ( "direction" , match dir {
333
+ Direction :: In => "in" ,
334
+ Direction :: Out => "out" ,
335
+ Direction :: High => "high" ,
336
+ Direction :: Low => "low" ,
337
+ } ) ?;
334
338
335
339
Ok ( ( ) )
336
340
}
@@ -360,11 +364,10 @@ impl Pin {
360
364
/// A 0 value will set the pin low and any other value will
361
365
/// set the pin high (1 is typical).
362
366
pub fn set_value ( & self , value : u8 ) -> Result < ( ) > {
363
- try!( self . write_to_device_file ( "value" ,
364
- match value {
365
- 0 => "0" ,
366
- _ => "1" ,
367
- } ) ) ;
367
+ self . write_to_device_file ( "value" , match value {
368
+ 0 => "0" ,
369
+ _ => "1" ,
370
+ } ) ?;
368
371
369
372
Ok ( ( ) )
370
373
}
@@ -394,13 +397,12 @@ impl Pin {
394
397
/// result in `poll()` returning. This call will return an Error
395
398
/// if the pin does not allow interrupts.
396
399
pub fn set_edge ( & self , edge : Edge ) -> Result < ( ) > {
397
- try!( self . write_to_device_file ( "edge" ,
398
- match edge {
399
- Edge :: NoInterrupt => "none" ,
400
- Edge :: RisingEdge => "rising" ,
401
- Edge :: FallingEdge => "falling" ,
402
- Edge :: BothEdges => "both" ,
403
- } ) ) ;
400
+ self . write_to_device_file ( "edge" , match edge {
401
+ Edge :: NoInterrupt => "none" ,
402
+ Edge :: RisingEdge => "rising" ,
403
+ Edge :: FallingEdge => "falling" ,
404
+ Edge :: BothEdges => "both" ,
405
+ } ) ?;
404
406
405
407
Ok ( ( ) )
406
408
}
@@ -449,7 +451,7 @@ impl Pin {
449
451
/// This method is only available when the `tokio` crate feature is enabled.
450
452
#[ cfg( feature = "tokio" ) ]
451
453
pub fn get_value_stream ( & self , handle : & Handle ) -> Result < PinValueStream > {
452
- Ok ( PinValueStream ( try! ( PinStream :: init ( self . clone ( ) , handle) ) ) )
454
+ Ok ( PinValueStream ( PinStream :: init ( self . clone ( ) , handle) ? ) )
453
455
}
454
456
}
455
457
@@ -472,9 +474,9 @@ impl PinPoller {
472
474
/// Create a new PinPoller for the provided pin number
473
475
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
474
476
pub fn new ( pin_num : u64 ) -> Result < PinPoller > {
475
- let devfile: File = try! ( File :: open ( & format ! ( "/sys/class/gpio/gpio{}/value" , pin_num) ) ) ;
477
+ let devfile: File = File :: open ( & format ! ( "/sys/class/gpio/gpio{}/value" , pin_num) ) ? ;
476
478
let devfile_fd = devfile. as_raw_fd ( ) ;
477
- let epoll_fd = try! ( epoll_create ( ) ) ;
479
+ let epoll_fd = epoll_create ( ) ? ;
478
480
let events = EPOLLPRI | EPOLLET ;
479
481
let info = EpollEvent {
480
482
events : events,
@@ -484,10 +486,10 @@ impl PinPoller {
484
486
match epoll_ctl ( epoll_fd, EpollOp :: EpollCtlAdd , devfile_fd, & info) {
485
487
Ok ( _) => {
486
488
Ok ( PinPoller {
487
- pin_num : pin_num,
488
- devfile : devfile,
489
- epoll_fd : epoll_fd,
490
- } )
489
+ pin_num : pin_num,
490
+ devfile : devfile,
491
+ epoll_fd : epoll_fd,
492
+ } )
491
493
}
492
494
Err ( err) => {
493
495
let _ = close ( epoll_fd) ; // cleanup
@@ -520,17 +522,17 @@ impl PinPoller {
520
522
/// occurred and the current time.
521
523
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
522
524
pub fn poll ( & mut self , timeout_ms : isize ) -> Result < Option < u8 > > {
523
- try! ( flush_input_from_file ( & mut self . devfile , 255 ) ) ;
525
+ flush_input_from_file ( & mut self . devfile , 255 ) ? ;
524
526
let dummy_event = EpollEvent {
525
527
events : EPOLLPRI | EPOLLET ,
526
528
data : 0u64 ,
527
529
} ;
528
530
let mut events: [ EpollEvent ; 1 ] = [ dummy_event] ;
529
- let cnt = try! ( epoll_wait ( self . epoll_fd , & mut events, timeout_ms) ) ;
531
+ let cnt = epoll_wait ( self . epoll_fd , & mut events, timeout_ms) ? ;
530
532
Ok ( match cnt {
531
- 0 => None , // timeout
532
- _ => Some ( try! ( get_value_from_file ( & mut self . devfile ) ) ) ,
533
- } )
533
+ 0 => None , // timeout
534
+ _ => Some ( get_value_from_file ( & mut self . devfile ) ? ) ,
535
+ } )
534
536
}
535
537
536
538
#[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
@@ -544,7 +546,7 @@ impl Drop for PinPoller {
544
546
// we implement drop to close the underlying epoll fd as
545
547
// it does not implement drop itself. This is similar to
546
548
// how mio works
547
- close ( self . epoll_fd ) . unwrap ( ) ; // panic! if close files
549
+ close ( self . epoll_fd ) . unwrap ( ) ; // panic! if close files
548
550
}
549
551
}
550
552
@@ -557,7 +559,7 @@ pub struct AsyncPinPoller {
557
559
#[ cfg( feature = "mio-evented" ) ]
558
560
impl AsyncPinPoller {
559
561
fn new ( pin_num : u64 ) -> Result < Self > {
560
- let devfile = try! ( File :: open ( & format ! ( "/sys/class/gpio/gpio{}/value" , pin_num) ) ) ;
562
+ let devfile = File :: open ( & format ! ( "/sys/class/gpio/gpio{}/value" , pin_num) ) ? ;
561
563
Ok ( AsyncPinPoller { devfile : devfile } )
562
564
}
563
565
}
@@ -597,9 +599,9 @@ pub struct PinStream {
597
599
impl PinStream {
598
600
pub fn init ( pin : Pin , handle : & Handle ) -> Result < Self > {
599
601
Ok ( PinStream {
600
- evented : try! ( PollEvented :: new ( try! ( pin. get_async_poller ( ) ) , & handle) ) ,
601
- skipped_first_event : false ,
602
- } )
602
+ evented : PollEvented :: new ( pin. get_async_poller ( ) ? , & handle) ? ,
603
+ skipped_first_event : false ,
604
+ } )
603
605
}
604
606
}
605
607
@@ -610,7 +612,7 @@ impl Stream for PinStream {
610
612
611
613
fn poll ( & mut self ) -> Poll < Option < Self :: Item > , Self :: Error > {
612
614
Ok ( match self . evented . poll_read ( ) {
613
- Async :: Ready ( ( ) ) => {
615
+ Async :: Ready ( ( ) ) => {
614
616
self . evented . need_read ( ) ;
615
617
if self . skipped_first_event {
616
618
Async :: Ready ( Some ( ( ) ) )
@@ -619,8 +621,8 @@ impl Stream for PinStream {
619
621
Async :: NotReady
620
622
}
621
623
}
622
- Async :: NotReady => Async :: NotReady ,
623
- } )
624
+ Async :: NotReady => Async :: NotReady ,
625
+ } )
624
626
}
625
627
}
626
628
0 commit comments