1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-26 16:34:23 +03:00
wezterm/filedescriptor
2019-06-18 07:39:24 -07:00
..
src pull in the poll implementation from wezterm+termwiz 2019-06-18 07:39:24 -07:00
Cargo.toml filedescriptor: fix a silly typo, enhance docs 2019-06-01 08:19:18 -07:00
README.md filedescriptor: fix a silly typo, enhance docs 2019-06-01 08:19:18 -07:00

The purpose of this crate is to make it a bit more ergonomic for portable applications that need to work with the platform level RawFd and RawHandle types.

Rather than conditionally using RawFd and RawHandle, the FileDescriptor type can be used to manage ownership, duplicate, read and write.

FileDescriptor

This is a bit of a contrived example, but demonstrates how to avoid the conditional code that would otherwise be required to deal with calling as_raw_fd and as_raw_handle:

use filedescriptor::{FileDescriptor, FromRawFileDescriptor};
use failure::Fallible;
use std::io::Write;

fn get_stdout() -> Fallible<FileDescriptor> {
  let stdout = std::io::stdout();
  let handle = stdout.lock();
  FileDescriptor::dup(&handle)
}

fn print_something() -> Fallible<()> {
   get_stdout()?.write(b"hello")?;
   Ok(())
}

Pipe

The Pipe type makes it more convenient to create a pipe and manage the lifetime of both the read and write ends of that pipe.

use filedescriptor::Pipe;
use std::io::{Read,Write};
use failure::Error;

let mut pipe = Pipe::new()?;
pipe.write.write(b"hello")?;
drop(pipe.write);

let mut s = String::new();
pipe.read.read_to_string(&mut s)?;
assert_eq!(s, "hello");
# Ok::<(), Error>(())