161
161
//! cfsetspeed(&mut t, 9600u32);
162
162
//! # }
163
163
//! ```
164
- use Result ;
164
+ use { Error , Result } ;
165
165
use errno:: Errno ;
166
166
use libc:: { self , c_int, tcflag_t} ;
167
167
use std:: cell:: { Ref , RefCell } ;
168
- use std:: convert:: From ;
168
+ use std:: convert:: { From , TryFrom } ;
169
169
use std:: mem;
170
170
use std:: os:: unix:: io:: RawFd ;
171
171
@@ -376,9 +376,10 @@ libc_enum!{
376
376
}
377
377
}
378
378
379
- impl From < libc:: speed_t > for BaudRate {
380
- fn from ( s : libc :: speed_t ) -> BaudRate {
379
+ impl TryFrom < libc:: speed_t > for BaudRate {
380
+ type Error = Error ;
381
381
382
+ fn try_from ( s : libc:: speed_t ) -> Result < BaudRate > {
382
383
use libc:: { B0 , B50 , B75 , B110 , B134 , B150 , B200 , B300 , B600 , B1200 , B1800 , B2400 , B4800 ,
383
384
B9600 , B19200 , B38400 , B57600 , B115200 , B230400 } ;
384
385
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
@@ -398,85 +399,84 @@ impl From<libc::speed_t> for BaudRate {
398
399
use libc:: { B460800 , B921600 } ;
399
400
400
401
match s {
401
- B0 => BaudRate :: B0 ,
402
- B50 => BaudRate :: B50 ,
403
- B75 => BaudRate :: B75 ,
404
- B110 => BaudRate :: B110 ,
405
- B134 => BaudRate :: B134 ,
406
- B150 => BaudRate :: B150 ,
407
- B200 => BaudRate :: B200 ,
408
- B300 => BaudRate :: B300 ,
409
- B600 => BaudRate :: B600 ,
410
- B1200 => BaudRate :: B1200 ,
411
- B1800 => BaudRate :: B1800 ,
412
- B2400 => BaudRate :: B2400 ,
413
- B4800 => BaudRate :: B4800 ,
402
+ B0 => Ok ( BaudRate :: B0 ) ,
403
+ B50 => Ok ( BaudRate :: B50 ) ,
404
+ B75 => Ok ( BaudRate :: B75 ) ,
405
+ B110 => Ok ( BaudRate :: B110 ) ,
406
+ B134 => Ok ( BaudRate :: B134 ) ,
407
+ B150 => Ok ( BaudRate :: B150 ) ,
408
+ B200 => Ok ( BaudRate :: B200 ) ,
409
+ B300 => Ok ( BaudRate :: B300 ) ,
410
+ B600 => Ok ( BaudRate :: B600 ) ,
411
+ B1200 => Ok ( BaudRate :: B1200 ) ,
412
+ B1800 => Ok ( BaudRate :: B1800 ) ,
413
+ B2400 => Ok ( BaudRate :: B2400 ) ,
414
+ B4800 => Ok ( BaudRate :: B4800 ) ,
414
415
#[ cfg( any( target_os = "dragonfly" ,
415
416
target_os = "freebsd" ,
416
417
target_os = "macos" ,
417
418
target_os = "netbsd" ,
418
419
target_os = "openbsd" ) ) ]
419
- B7200 => BaudRate :: B7200 ,
420
- B9600 => BaudRate :: B9600 ,
420
+ B7200 => Ok ( BaudRate :: B7200 ) ,
421
+ B9600 => Ok ( BaudRate :: B9600 ) ,
421
422
#[ cfg( any( target_os = "dragonfly" ,
422
423
target_os = "freebsd" ,
423
424
target_os = "macos" ,
424
425
target_os = "netbsd" ,
425
426
target_os = "openbsd" ) ) ]
426
- B14400 => BaudRate :: B14400 ,
427
- B19200 => BaudRate :: B19200 ,
427
+ B14400 => Ok ( BaudRate :: B14400 ) ,
428
+ B19200 => Ok ( BaudRate :: B19200 ) ,
428
429
#[ cfg( any( target_os = "dragonfly" ,
429
430
target_os = "freebsd" ,
430
431
target_os = "macos" ,
431
432
target_os = "netbsd" ,
432
433
target_os = "openbsd" ) ) ]
433
- B28800 => BaudRate :: B28800 ,
434
- B38400 => BaudRate :: B38400 ,
435
- B57600 => BaudRate :: B57600 ,
434
+ B28800 => Ok ( BaudRate :: B28800 ) ,
435
+ B38400 => Ok ( BaudRate :: B38400 ) ,
436
+ B57600 => Ok ( BaudRate :: B57600 ) ,
436
437
#[ cfg( any( target_os = "dragonfly" ,
437
438
target_os = "freebsd" ,
438
439
target_os = "macos" ,
439
440
target_os = "netbsd" ,
440
441
target_os = "openbsd" ) ) ]
441
- B76800 => BaudRate :: B76800 ,
442
- B115200 => BaudRate :: B115200 ,
443
- B230400 => BaudRate :: B230400 ,
442
+ B76800 => Ok ( BaudRate :: B76800 ) ,
443
+ B115200 => Ok ( BaudRate :: B115200 ) ,
444
+ B230400 => Ok ( BaudRate :: B230400 ) ,
444
445
#[ cfg( any( target_os = "android" ,
445
446
target_os = "freebsd" ,
446
447
target_os = "linux" ,
447
448
target_os = "netbsd" ) ) ]
448
- B460800 => BaudRate :: B460800 ,
449
+ B460800 => Ok ( BaudRate :: B460800 ) ,
449
450
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
450
- B500000 => BaudRate :: B500000 ,
451
+ B500000 => Ok ( BaudRate :: B500000 ) ,
451
452
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
452
- B576000 => BaudRate :: B576000 ,
453
+ B576000 => Ok ( BaudRate :: B576000 ) ,
453
454
#[ cfg( any( target_os = "android" ,
454
455
target_os = "freebsd" ,
455
456
target_os = "linux" ,
456
457
target_os = "netbsd" ) ) ]
457
- B921600 => BaudRate :: B921600 ,
458
+ B921600 => Ok ( BaudRate :: B921600 ) ,
458
459
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
459
- B1000000 => BaudRate :: B1000000 ,
460
+ B1000000 => Ok ( BaudRate :: B1000000 ) ,
460
461
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
461
- B1152000 => BaudRate :: B1152000 ,
462
+ B1152000 => Ok ( BaudRate :: B1152000 ) ,
462
463
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
463
- B1500000 => BaudRate :: B1500000 ,
464
+ B1500000 => Ok ( BaudRate :: B1500000 ) ,
464
465
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
465
- B2000000 => BaudRate :: B2000000 ,
466
+ B2000000 => Ok ( BaudRate :: B2000000 ) ,
466
467
#[ cfg( any( target_os = "android" , all( target_os = "linux" , not( target_arch = "sparc64" ) ) ) ) ]
467
- B2500000 => BaudRate :: B2500000 ,
468
+ B2500000 => Ok ( BaudRate :: B2500000 ) ,
468
469
#[ cfg( any( target_os = "android" , all( target_os = "linux" , not( target_arch = "sparc64" ) ) ) ) ]
469
- B3000000 => BaudRate :: B3000000 ,
470
+ B3000000 => Ok ( BaudRate :: B3000000 ) ,
470
471
#[ cfg( any( target_os = "android" , all( target_os = "linux" , not( target_arch = "sparc64" ) ) ) ) ]
471
- B3500000 => BaudRate :: B3500000 ,
472
+ B3500000 => Ok ( BaudRate :: B3500000 ) ,
472
473
#[ cfg( any( target_os = "android" , all( target_os = "linux" , not( target_arch = "sparc64" ) ) ) ) ]
473
- B4000000 => BaudRate :: B4000000 ,
474
- b => unreachable ! ( "Invalid baud constant: {}" , b ) ,
474
+ B4000000 => Ok ( BaudRate :: B4000000 ) ,
475
+ _ => Err ( Error :: invalid_argument ( ) )
475
476
}
476
477
}
477
478
}
478
479
479
- // TODO: Include `TryFrom<u32> for BaudRate` once that API stabilizes
480
480
#[ cfg( any( target_os = "freebsd" ,
481
481
target_os = "dragonfly" ,
482
482
target_os = "ios" ,
@@ -963,13 +963,15 @@ cfg_if!{
963
963
Errno :: result( res) . map( drop)
964
964
}
965
965
} else {
966
+ use std:: convert:: TryInto ;
967
+
966
968
/// Get input baud rate (see
967
969
/// [cfgetispeed(3p)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/cfgetispeed.html)).
968
970
///
969
971
/// `cfgetispeed()` extracts the input baud rate from the given `Termios` structure.
970
972
pub fn cfgetispeed( termios: & Termios ) -> BaudRate {
971
973
let inner_termios = termios. get_libc_termios( ) ;
972
- unsafe { libc:: cfgetispeed( & * inner_termios) } . into ( )
974
+ unsafe { libc:: cfgetispeed( & * inner_termios) } . try_into ( ) . unwrap ( )
973
975
}
974
976
975
977
/// Get output baud rate (see
@@ -978,7 +980,7 @@ cfg_if!{
978
980
/// `cfgetospeed()` extracts the output baud rate from the given `Termios` structure.
979
981
pub fn cfgetospeed( termios: & Termios ) -> BaudRate {
980
982
let inner_termios = termios. get_libc_termios( ) ;
981
- unsafe { libc:: cfgetospeed( & * inner_termios) } . into ( )
983
+ unsafe { libc:: cfgetospeed( & * inner_termios) } . try_into ( ) . unwrap ( )
982
984
}
983
985
984
986
/// Set input baud rate (see
@@ -1111,3 +1113,14 @@ pub fn tcgetsid(fd: RawFd) -> Result<Pid> {
1111
1113
1112
1114
Errno :: result ( res) . map ( Pid :: from_raw)
1113
1115
}
1116
+
1117
+ #[ cfg( test) ]
1118
+ mod test {
1119
+ use super :: * ;
1120
+
1121
+ #[ test]
1122
+ fn try_from ( ) {
1123
+ assert_eq ! ( Ok ( BaudRate :: B0 ) , BaudRate :: try_from( libc:: B0 ) ) ;
1124
+ assert ! ( BaudRate :: try_from( 999999999 ) . is_err( ) ) ;
1125
+ }
1126
+ }
0 commit comments