Automatically close raw fd (#383)

This commit is contained in:
Jonathan Turner 2020-02-22 19:59:40 +13:00 committed by GitHub
parent e35d4d2c1c
commit 007063896e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 6 deletions

View File

@ -91,7 +91,7 @@ mod read;
mod source;
#[cfg(feature = "event-stream")]
mod stream;
mod sys;
pub(crate) mod sys;
mod timeout;
lazy_static! {

View File

@ -1,13 +1,13 @@
//! UNIX related logic for terminal manipulation.
use std::{io, mem, process, sync::Mutex};
use crate::event::sys::unix::file_descriptor::FileDesc;
use lazy_static::lazy_static;
use libc::{
cfmakeraw, ioctl, tcgetattr, tcsetattr, termios as Termios, winsize, STDIN_FILENO, TCSANOW,
TIOCGWINSZ,
};
use lazy_static::lazy_static;
use crate::error::{ErrorKind, Result};
use std::fs::File;
use std::os::unix::io::IntoRawFd;
@ -32,10 +32,10 @@ pub(crate) fn size() -> Result<(u16, u16)> {
ws_ypixel: 0,
};
let file = File::open("/dev/tty").unwrap();
let file = File::open("/dev/tty")?;
if let Ok(true) =
wrap_with_result(unsafe { ioctl(file.into_raw_fd(), TIOCGWINSZ.into(), &mut size) })
let fd = FileDesc::new(file.into_raw_fd(), true);
if let Ok(true) = wrap_with_result(unsafe { ioctl(fd.raw_fd(), TIOCGWINSZ.into(), &mut size) })
{
Ok((size.ws_col, size.ws_row))
} else {