Skip to content

Commit 93af76f

Browse files
authored
Merge pull request #1235 from nix-rust/termios_constructor
Limit internal termios API to pub(crate)
2 parents a777389 + 28b4ef4 commit 93af76f

File tree

3 files changed

+22
-57
lines changed

3 files changed

+22
-57
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
6262

6363
- Removed `sys::socket::addr::from_libc_sockaddr` from the public API.
6464
(#[1215](https://github.com/nix-rust/nix/pull/1215))
65+
- Removed `sys::termios::{get_libc_termios, get_libc_termios_mut, update_wrapper`
66+
from the public API. These were previously hidden in the docs but still usable
67+
by downstream.
68+
(#[1235](https://github.com/nix-rust/nix/pull/1235))
6569

6670
- Nix no longer implements `NixPath` for `Option<P> where P: NixPath`. Most
6771
Nix functions that accept `NixPath` arguments can't do anything useful with

src/sys/termios.rs

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//! ```
2626
//! # use self::nix::sys::termios::SpecialCharacterIndices::VEOF;
2727
//! # use self::nix::sys::termios::{_POSIX_VDISABLE, Termios};
28-
//! # let mut termios = unsafe { Termios::default_uninit() };
28+
//! # let mut termios: Termios = unsafe { std::mem::zeroed() };
2929
//! termios.control_chars[VEOF as usize] = _POSIX_VDISABLE;
3030
//! ```
3131
//!
@@ -38,7 +38,7 @@
3838
//!
3939
//! ```
4040
//! # use self::nix::sys::termios::{ControlFlags, Termios};
41-
//! # let mut termios = unsafe { Termios::default_uninit() };
41+
//! # let mut termios: Termios = unsafe { std::mem::zeroed() };
4242
//! termios.control_flags & ControlFlags::CSIZE == ControlFlags::CS5;
4343
//! termios.control_flags |= ControlFlags::CS5;
4444
//! ```
@@ -61,10 +61,9 @@
6161
//! platforms:
6262
//!
6363
//! ```rust
64-
//! # #[macro_use] extern crate nix;
6564
//! # use nix::sys::termios::{BaudRate, cfsetispeed, cfsetospeed, cfsetspeed, Termios};
6665
//! # fn main() {
67-
//! # let mut t = unsafe { Termios::default_uninit() };
66+
//! # let mut t: Termios = unsafe { std::mem::zeroed() };
6867
//! cfsetispeed(&mut t, BaudRate::B9600);
6968
//! cfsetospeed(&mut t, BaudRate::B9600);
7069
//! cfsetspeed(&mut t, BaudRate::B9600);
@@ -76,7 +75,7 @@
7675
//! ```rust
7776
//! # use nix::sys::termios::{BaudRate, cfgetispeed, cfgetospeed, cfsetispeed, cfsetspeed, Termios};
7877
//! # fn main() {
79-
//! # let mut t = unsafe { Termios::default_uninit() };
78+
//! # let mut t: Termios = unsafe { std::mem::zeroed() };
8079
//! # cfsetspeed(&mut t, BaudRate::B9600);
8180
//! let speed = cfgetispeed(&t);
8281
//! assert_eq!(speed, cfgetospeed(&t));
@@ -94,7 +93,7 @@
9493
doc = " ```rust")]
9594
//! # use nix::sys::termios::{BaudRate, cfgetispeed, cfgetospeed, cfsetspeed, Termios};
9695
//! # fn main() {
97-
//! # let mut t = unsafe { Termios::default_uninit() };
96+
//! # let mut t: Termios = unsafe { std::mem::zeroed() };
9897
//! # cfsetspeed(&mut t, BaudRate::B9600);
9998
//! assert_eq!(cfgetispeed(&t), BaudRate::B9600);
10099
//! assert_eq!(cfgetospeed(&t), BaudRate::B9600);
@@ -111,7 +110,7 @@
111110
doc = " ```rust,ignore")]
112111
//! # use nix::sys::termios::{BaudRate, cfgetispeed, cfgetospeed, cfsetspeed, Termios};
113112
//! # fn main() {
114-
//! # let mut t = unsafe { Termios::default_uninit() };
113+
//! # let mut t: Termios = unsafe { std::mem::zeroed() };
115114
//! # cfsetspeed(&mut t, 9600u32);
116115
//! assert_eq!(cfgetispeed(&t), 9600u32);
117116
//! assert_eq!(cfgetospeed(&t), 9600u32);
@@ -128,7 +127,7 @@
128127
doc = " ```rust,ignore")]
129128
//! # use nix::sys::termios::{BaudRate, cfgetispeed, cfsetspeed, Termios};
130129
//! # fn main() {
131-
//! # let mut t = unsafe { Termios::default_uninit() };
130+
//! # let mut t: Termios = unsafe { std::mem::zeroed() };
132131
//! # cfsetspeed(&mut t, 9600u32);
133132
//! assert_eq!(cfgetispeed(&t), BaudRate::B9600.into());
134133
//! assert_eq!(u32::from(BaudRate::B9600), 9600u32);
@@ -146,7 +145,7 @@
146145
doc = " ```rust,ignore")]
147146
//! # use nix::sys::termios::{cfsetispeed, cfsetospeed, cfsetspeed, Termios};
148147
//! # fn main() {
149-
//! # let mut t = unsafe { Termios::default_uninit() };
148+
//! # let mut t: Termios = unsafe { std::mem::zeroed() };
150149
//! cfsetispeed(&mut t, 9600u32);
151150
//! cfsetospeed(&mut t, 9600u32);
152151
//! cfsetspeed(&mut t, 9600u32);
@@ -186,22 +185,9 @@ pub struct Termios {
186185
impl Termios {
187186
/// Exposes an immutable reference to the underlying `libc::termios` data structure.
188187
///
189-
/// This can be used for interfacing with other FFI functions like:
190-
///
191-
/// ```rust
192-
/// # fn main() {
193-
/// # use nix::sys::termios::Termios;
194-
/// # let mut termios = unsafe { Termios::default_uninit() };
195-
/// let inner_termios = termios.get_libc_termios();
196-
/// unsafe { libc::cfgetispeed(&*inner_termios) };
197-
/// # }
198-
/// ```
199-
///
200-
/// There is no public API exposed for functions that modify the underlying `libc::termios`
201-
/// data because it requires additional work to maintain type safety.
202-
// FIXME: Switch this over to use pub(crate)
203-
#[doc(hidden)]
204-
pub fn get_libc_termios(&self) -> Ref<libc::termios> {
188+
/// This is not part of `nix`'s public API because it requires additional work to maintain type
189+
/// safety.
190+
pub(crate) fn get_libc_termios(&self) -> Ref<libc::termios> {
205191
{
206192
let mut termios = self.inner.borrow_mut();
207193
termios.c_iflag = self.input_flags.bits();
@@ -215,12 +201,11 @@ impl Termios {
215201

216202
/// Exposes the inner `libc::termios` datastore within `Termios`.
217203
///
218-
/// This is unsafe because if this is used to modify the inner libc::termios struct, it will not
219-
/// automatically update the safe wrapper type around it. Therefore we disable docs to
220-
/// effectively limit its use to nix internals. In this case it should also be paired with a
221-
/// call to `update_wrapper()` so that the wrapper-type and internal representation stay
222-
/// consistent.
223-
unsafe fn get_libc_termios_mut(&mut self) -> *mut libc::termios {
204+
/// This is unsafe because if this is used to modify the inner `libc::termios` struct, it will
205+
/// not automatically update the safe wrapper type around it. In this case it should also be
206+
/// paired with a call to `update_wrapper()` so that the wrapper-type and internal
207+
/// representation stay consistent.
208+
pub(crate) unsafe fn get_libc_termios_mut(&mut self) -> *mut libc::termios {
224209
{
225210
let mut termios = self.inner.borrow_mut();
226211
termios.c_iflag = self.input_flags.bits();
@@ -232,26 +217,8 @@ impl Termios {
232217
self.inner.as_ptr()
233218
}
234219

235-
/// Allows for easily creating new `Termios` structs that will be overwritten with real data.
236-
///
237-
/// This should only be used when the inner libc::termios struct will be overwritten before it's
238-
/// read.
239-
// FIXME: Switch this over to use pub(crate)
240-
#[doc(hidden)]
241-
pub unsafe fn default_uninit() -> Self {
242-
Termios {
243-
inner: RefCell::new(mem::zeroed()),
244-
input_flags: InputFlags::empty(),
245-
output_flags: OutputFlags::empty(),
246-
control_flags: ControlFlags::empty(),
247-
local_flags: LocalFlags::empty(),
248-
control_chars: [0 as libc::cc_t; NCCS],
249-
}
250-
}
251-
252220
/// Updates the wrapper values from the internal `libc::termios` data structure.
253-
#[doc(hidden)]
254-
pub fn update_wrapper(&mut self) {
221+
pub(crate) fn update_wrapper(&mut self) {
255222
let termios = *self.inner.borrow_mut();
256223
self.input_flags = InputFlags::from_bits_truncate(termios.c_iflag);
257224
self.output_flags = OutputFlags::from_bits_truncate(termios.c_oflag);

test/sys/test_termios.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tempfile::tempfile;
44
use nix::{Error, fcntl};
55
use nix::errno::Errno;
66
use nix::pty::openpty;
7-
use nix::sys::termios::{self, LocalFlags, OutputFlags, Termios, tcgetattr};
7+
use nix::sys::termios::{self, LocalFlags, OutputFlags, tcgetattr};
88
use nix::unistd::{read, write, close};
99

1010
/// Helper function analogous to `std::io::Write::write_all`, but for `RawFd`s
@@ -128,9 +128,3 @@ fn test_local_flags() {
128128
close(pty.slave).unwrap();
129129
assert_eq!(read, Error::Sys(Errno::EAGAIN));
130130
}
131-
132-
#[test]
133-
fn test_cfmakeraw() {
134-
let mut termios = unsafe { Termios::default_uninit() };
135-
termios::cfmakeraw(&mut termios);
136-
}

0 commit comments

Comments
 (0)