Skip to content

Commit cb1c915

Browse files
committed
Auto merge of rust-lang#518 - berkowski:baud_constants, r=posborne
Added BaudRate enum for termios Issue rust-lang#514 Does not provide `BaudRate::EXTA` or `BaudRate::EXTB` constants. These seem to alias to `B19200` and `B38400` respectively and so break the 1:1 mapping needed by `From`. I don't know their historic use.
2 parents 4c9e42f + 8b6242e commit cb1c915

File tree

2 files changed

+174
-8
lines changed

2 files changed

+174
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased]
77

88
### Added
9+
- Added `::nix::sys::termios::BaudRate` enum to provide portable baudrate
10+
values. ([#518](https://github.com/nix-rust/nix/pull/518))
911
- Added a new `WaitStatus::PtraceEvent` to support ptrace events on Linux
1012
and Android ([([#438](https://github.com/nix-rust/nix/pull/438))
1113
- Added support for POSIX AIO
@@ -41,6 +43,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4143
([#503](https://github.com/nix-rust/nix/pull/503))
4244

4345
### Changed
46+
- `::nix::sys::termios::{cfgetispeed, cfsetispeed, cfgetospeed, cfsetospeed}`
47+
switched to use `BaudRate` enum from `speed_t`.
48+
([#518](https://github.com/nix-rust/nix/pull/518))
4449
- `epoll_ctl` now could accept None as argument `event`
4550
when op is `EpollOp::EpollCtlDel`.
4651
([#480](https://github.com/nix-rust/nix/pull/480))

src/sys/termios.rs

Lines changed: 169 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,83 @@ mod ffi {
125125
pub c_ospeed: speed_t
126126
}
127127

128+
#[derive(Clone, Copy, Debug, PartialEq)]
129+
pub enum BaudRate {
130+
B0,
131+
B50,
132+
B75,
133+
B110,
134+
B134,
135+
B150,
136+
B200,
137+
B300,
138+
B600,
139+
B1200,
140+
B1800,
141+
B2400,
142+
B4800,
143+
B9600,
144+
B19200,
145+
B38400,
146+
B7200,
147+
B14400,
148+
B28800,
149+
B57600,
150+
B76800,
151+
B115200,
152+
B230400,
153+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
154+
B460800,
155+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
156+
B921600,
157+
}
158+
159+
impl From<speed_t> for BaudRate {
160+
fn from(s: speed_t) -> BaudRate {
161+
162+
use libc::{
163+
B0, B50, B75, B110, B134, B150,
164+
B200, B300, B600, B1200, B1800, B2400,
165+
B4800, B9600, B19200, B38400,
166+
B7200, B14400, B28800, B57600,
167+
B76800, B115200, B230400};
168+
169+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
170+
use libc::{B460800, B921600};
171+
172+
match s {
173+
B0 => BaudRate::B0,
174+
B50 => BaudRate::B50,
175+
B75 => BaudRate::B75,
176+
B110 => BaudRate::B110,
177+
B134 => BaudRate::B134,
178+
B150 => BaudRate::B150,
179+
B200 => BaudRate::B200,
180+
B300 => BaudRate::B300,
181+
B600 => BaudRate::B600,
182+
B1200 => BaudRate::B1200,
183+
B1800 => BaudRate::B1800,
184+
B2400 => BaudRate::B2400,
185+
B4800 => BaudRate::B4800,
186+
B9600 => BaudRate::B9600,
187+
B19200 => BaudRate::B19200,
188+
B38400 => BaudRate::B38400,
189+
B7200 => BaudRate::B7200,
190+
B14400 => BaudRate::B14400,
191+
B28800 => BaudRate::B28800,
192+
B57600 => BaudRate::B57600,
193+
B76800 => BaudRate::B76800,
194+
B115200 => BaudRate::B115200,
195+
B230400 => BaudRate::B230400,
196+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
197+
B460800 => BaudRate::B460800,
198+
#[cfg(any(target_os = "netbsd", target_os = "freebsd"))]
199+
B921600 => BaudRate::B921600,
200+
b @ _ => unreachable!("Invalid baud constant: {}", b),
201+
}
202+
}
203+
}
204+
128205
pub const VEOF: usize = 0;
129206
pub const VEOL: usize = 1;
130207
pub const VEOL2: usize = 2;
@@ -293,6 +370,90 @@ mod ffi {
293370
pub c_ospeed: speed_t
294371
}
295372

373+
#[derive(Clone, Copy, Debug, PartialEq)]
374+
pub enum BaudRate {
375+
B0,
376+
B50,
377+
B75,
378+
B110,
379+
B134,
380+
B150,
381+
B200,
382+
B300,
383+
B600,
384+
B1200,
385+
B1800,
386+
B2400,
387+
B4800,
388+
B9600,
389+
B19200,
390+
B38400,
391+
B57600,
392+
B115200,
393+
B230400,
394+
B460800,
395+
B500000,
396+
B576000,
397+
B921600,
398+
B1000000,
399+
B1152000,
400+
B1500000,
401+
B2000000,
402+
B2500000,
403+
B3000000,
404+
B3500000,
405+
B4000000,
406+
}
407+
408+
impl From<speed_t> for BaudRate {
409+
fn from(s: speed_t) -> BaudRate {
410+
411+
use libc::{
412+
B0, B50, B75, B110, B134, B150,
413+
B200, B300, B600, B1200, B1800, B2400,
414+
B4800, B9600, B19200, B38400, B57600,
415+
B115200, B230400, B460800, B500000,
416+
B576000, B921600, B1000000, B1152000,
417+
B1500000, B2000000, B2500000, B3000000,
418+
B3500000, B4000000};
419+
420+
match s {
421+
B0 => BaudRate::B0,
422+
B50 => BaudRate::B50,
423+
B75 => BaudRate::B75,
424+
B110 => BaudRate::B110,
425+
B134 => BaudRate::B134,
426+
B150 => BaudRate::B150,
427+
B200 => BaudRate::B200,
428+
B300 => BaudRate::B300,
429+
B600 => BaudRate::B600,
430+
B1200 => BaudRate::B1200,
431+
B1800 => BaudRate::B1800,
432+
B2400 => BaudRate::B2400,
433+
B4800 => BaudRate::B4800,
434+
B9600 => BaudRate::B9600,
435+
B19200 => BaudRate::B19200,
436+
B38400 => BaudRate::B38400,
437+
B57600 => BaudRate::B57600,
438+
B115200 => BaudRate::B115200,
439+
B230400 => BaudRate::B230400,
440+
B460800 => BaudRate::B460800,
441+
B500000 => BaudRate::B500000,
442+
B576000 => BaudRate::B576000,
443+
B921600 => BaudRate::B921600,
444+
B1000000 => BaudRate::B1000000,
445+
B1152000 => BaudRate::B1152000,
446+
B1500000 => BaudRate::B1500000,
447+
B2000000 => BaudRate::B2000000,
448+
B2500000 => BaudRate::B2500000,
449+
B3000000 => BaudRate::B3000000,
450+
B3500000 => BaudRate::B3500000,
451+
B4000000 => BaudRate::B4000000,
452+
b @ _ => unreachable!("Invalid baud constant: {}", b),
453+
}
454+
}
455+
}
456+
296457
pub const VEOF: usize = 4;
297458
pub const VEOL: usize = 11;
298459
pub const VEOL2: usize = 16;
@@ -426,27 +587,27 @@ mod ffi {
426587
}
427588
}
428589

429-
pub fn cfgetispeed(termios: &Termios) -> speed_t {
590+
pub fn cfgetispeed(termios: &Termios) -> BaudRate {
430591
unsafe {
431-
ffi::cfgetispeed(termios)
592+
ffi::cfgetispeed(termios).into()
432593
}
433594
}
434595

435-
pub fn cfgetospeed(termios: &Termios) -> speed_t {
596+
pub fn cfgetospeed(termios: &Termios) -> BaudRate {
436597
unsafe {
437-
ffi::cfgetospeed(termios)
598+
ffi::cfgetospeed(termios).into()
438599
}
439600
}
440601

441-
pub fn cfsetispeed(termios: &mut Termios, speed: speed_t) -> Result<()> {
602+
pub fn cfsetispeed(termios: &mut Termios, baud: BaudRate) -> Result<()> {
442603
Errno::result(unsafe {
443-
ffi::cfsetispeed(termios, speed)
604+
ffi::cfsetispeed(termios, baud as speed_t)
444605
}).map(drop)
445606
}
446607

447-
pub fn cfsetospeed(termios: &mut Termios, speed: speed_t) -> Result<()> {
608+
pub fn cfsetospeed(termios: &mut Termios, baud: BaudRate) -> Result<()> {
448609
Errno::result(unsafe {
449-
ffi::cfsetospeed(termios, speed)
610+
ffi::cfsetospeed(termios, baud as speed_t)
450611
}).map(drop)
451612
}
452613

0 commit comments

Comments
 (0)