#[macro_use]
pub(crate) mod common;
pub mod dtrace;
pub mod guess;
pub mod perf;
pub(crate) mod matcher;
pub mod sample;
pub mod vtune;
#[doc(hidden)]
pub use self::common::DEFAULT_NTHREADS;
use std::fs::File;
use std::io;
use std::path::Path;
use self::common::{CollapsePrivate, CAPACITY_READER};
pub trait Collapse {
fn collapse<R, W>(&mut self, reader: R, writer: W) -> io::Result<()>
where
R: io::BufRead,
W: io::Write;
fn collapse_file<P, W>(&mut self, infile: Option<P>, writer: W) -> io::Result<()>
where
P: AsRef<Path>,
W: io::Write,
{
match infile {
Some(ref path) => {
let file = File::open(path)?;
let reader = io::BufReader::with_capacity(CAPACITY_READER, file);
self.collapse(reader, writer)
}
None => {
let stdin = io::stdin();
let stdin_guard = stdin.lock();
let reader = io::BufReader::with_capacity(CAPACITY_READER, stdin_guard);
self.collapse(reader, writer)
}
}
}
fn collapse_file_to_stdout<P>(&mut self, infile: Option<P>) -> io::Result<()>
where
P: AsRef<Path>,
{
if atty::is(atty::Stream::Stdout) {
self.collapse_file(infile, io::stdout().lock())
} else {
self.collapse_file(infile, io::BufWriter::new(io::stdout().lock()))
}
}
fn is_applicable(&mut self, input: &str) -> Option<bool>;
}
impl<T> Collapse for T
where
T: CollapsePrivate,
{
fn collapse<R, W>(&mut self, reader: R, writer: W) -> io::Result<()>
where
R: io::BufRead,
W: io::Write,
{
<Self as CollapsePrivate>::collapse(self, reader, writer)
}
fn is_applicable(&mut self, input: &str) -> Option<bool> {
<Self as CollapsePrivate>::is_applicable(self, input)
}
}