Skip to content

Add header + Remove unsafe usage #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 5 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ fn count_lines(content: String, sort_order: SortOrder) {

let stdout = io::stdout();
let mut handle = stdout.lock();
let _ = writeln!(handle, " Lines Copies Function name");
for row in data {
let _ = writeln!(
handle,
"{:7} {:4} {}",
"{:7} {:6} {}",
row.1.total_lines, row.1.copies, row.0
);
}
Expand Down Expand Up @@ -225,37 +226,6 @@ impl Drop for Wait {
}
}

// TODO: Any other targets that need a different implementation.
#[cfg(not(target_os = "windows"))]
unsafe fn create_stdios(child: &Child) -> (Stdio, Stdio) {
use std::os::unix::io::{AsRawFd, FromRawFd};
let stdout = Stdio::from_raw_fd(child.stdout.as_ref().map(AsRawFd::as_raw_fd).unwrap());
let stderr = Stdio::from_raw_fd(child.stderr.as_ref().map(AsRawFd::as_raw_fd).unwrap());

(stdout, stderr)
}

#[cfg(target_os = "windows")]
unsafe fn create_stdios(child: &Child) -> (Stdio, Stdio) {
use std::os::windows::io::{AsRawHandle, FromRawHandle};
let stdout = Stdio::from_raw_handle(
child
.stdout
.as_ref()
.map(AsRawHandle::as_raw_handle)
.unwrap(),
);
let stderr = Stdio::from_raw_handle(
child
.stderr
.as_ref()
.map(AsRawHandle::as_raw_handle)
.unwrap(),
);

(stdout, stderr)
}

trait PipeTo {
fn pipe_to(&mut self, out: &[&OsStr], err: &[&OsStr]) -> io::Result<Wait>;
}
Expand All @@ -265,9 +235,10 @@ impl PipeTo for Command {
self.stdout(Stdio::piped());
self.stderr(Stdio::piped());

let child = self.spawn()?;
let mut child = self.spawn()?;

let (stdout, stderr) = unsafe { create_stdios(&child) };
let stdout = child.stdout.take().ok_or(io::ErrorKind::BrokenPipe)?;
let stderr = child.stderr.take().ok_or(io::ErrorKind::BrokenPipe)?;

*self = Command::new(out[0]);
self.args(&out[1..]);
Expand Down