Struct clio::CachedInput
source · [−]pub struct CachedInput { /* private fields */ }
Expand description
A struct that contains all the connents of a command line input stream, either std in or a file
It is designed to be used with the clap
crate when taking a file name as an
argument to CLI app
use clap::Parser;
use clio::CachedInput;
#[derive(Parser)]
struct Opt {
/// path to file, use '-' for stdin
#[clap(value_parser)]
input_file: CachedInput,
}
Implementations
sourceimpl CachedInput
impl CachedInput
sourcepub fn new<S: AsRef<OsStr>>(path: S) -> Result<Self>
pub fn new<S: AsRef<OsStr>>(path: S) -> Result<Self>
Reads all the data from an file (stdin for “-”) into memmory and stores it in a new CachedInput.
Useful if you want to use the input twice (see reset), or need to know the size.
sourcepub fn std() -> Result<Self>
pub fn std() -> Result<Self>
Reads all the data from stdin into memmory and stores it in a new CachedInput.
This will block until std in is closed.
sourcepub fn try_from_os_str(path: &OsStr) -> Result<Self, OsString>
pub fn try_from_os_str(path: &OsStr) -> Result<Self, OsString>
Contructs a new CachedInput
either by opening the file or for ‘-’ stdin and reading
all the data into memory.
The error is converted to a OsString
so that stuctopt can show it to the user.
It is recomended that you use TryFrom::try_from
and clap 3.0 instead.
sourcepub fn len(&self) -> u64
pub fn len(&self) -> u64
Returns the size of the file in bytes.
Examples
let file = clio::CachedInput::try_from_os_str("foo.txt".as_ref()).unwrap();
assert_eq!(3, file.len());
Trait Implementations
sourceimpl BufRead for CachedInput
impl BufRead for CachedInput
sourcefn fill_buf(&mut self) -> IoResult<&[u8]>
fn fill_buf(&mut self) -> IoResult<&[u8]>
Returns the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
sourcefn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
Tells this buffer that amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read more
sourcefn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Check if the underlying Read
has any data left to be read. Read more
1.0.0 · sourcefn read_until(
&mut self,
byte: u8,
buf: &mut Vec<u8, Global>
) -> Result<usize, Error>
fn read_until(
&mut self,
byte: u8,
buf: &mut Vec<u8, Global>
) -> Result<usize, Error>
Read all bytes into buf
until the delimiter byte
or EOF is reached. Read more
1.0.0 · sourcefn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until a newline (the 0xA
byte) is reached, and append
them to the provided buffer. You do not need to clear the buffer before
appending. Read more
sourceimpl Clone for CachedInput
impl Clone for CachedInput
sourcefn clone(&self) -> CachedInputⓘNotable traits for CachedInputimpl Read for CachedInput
fn clone(&self) -> CachedInputⓘNotable traits for CachedInputimpl Read for CachedInput
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for CachedInput
impl Debug for CachedInput
sourceimpl Display for CachedInput
impl Display for CachedInput
formats the CachedInput
as the path it was created from
sourceimpl Read for CachedInput
impl Read for CachedInput
sourcefn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like read
, except that it reads into a slice of buffers. Read more
sourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
1.0.0 · sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
1.0.0 · sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
sourcefn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Pull some bytes from this source into the specified buffer. Read more
sourcefn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Read the exact number of bytes required to fill buf
. Read more
1.0.0 · sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read
. Read more
sourceimpl Seek for CachedInput
impl Seek for CachedInput
sourcefn seek(&mut self, pos: SeekFrom) -> IoResult<u64>
fn seek(&mut self, pos: SeekFrom) -> IoResult<u64>
Seek to an offset, in bytes, in a stream. Read more
1.55.0 · sourcefn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Rewind to the beginning of a stream. Read more
Auto Trait Implementations
impl RefUnwindSafe for CachedInput
impl Send for CachedInput
impl Sync for CachedInput
impl Unpin for CachedInput
impl UnwindSafe for CachedInput
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more