pub struct FileEdit(_);
Expand description
A read/write file handle with path()
attached and improved error messages. Contains methods
and trait implements for both readable and writeable files.
Examples
use std::io::{Read, Seek, Write, SeekFrom};
use path_abs::FileEdit;
let example = "example.txt";
let expected = "foo\nbar";
let mut edit = FileEdit::create(example)?;
let mut s = String::new();
edit.write_all(expected.as_bytes())?;
edit.seek(SeekFrom::Start(0))?;
edit.read_to_string(&mut s)?;
assert_eq!(expected, s);
Implementations§
source§impl FileEdit
impl FileEdit
sourcepub fn open<P: AsRef<Path>>(path: P, options: OpenOptions) -> Result<FileEdit>
pub fn open<P: AsRef<Path>>(path: P, options: OpenOptions) -> Result<FileEdit>
Open the file with the given OpenOptions
but always sets read
and write
to true.
sourcepub fn create<P: AsRef<Path>>(path: P) -> Result<FileEdit>
pub fn create<P: AsRef<Path>>(path: P) -> Result<FileEdit>
Open the file in editing mode, truncating it first if it exists and creating it otherwise.
sourcepub fn append<P: AsRef<Path>>(path: P) -> Result<FileEdit>
pub fn append<P: AsRef<Path>>(path: P) -> Result<FileEdit>
Open the file for appending, creating it if it doesn’t exist.
sourcepub fn edit<P: AsRef<Path>>(path: P) -> Result<FileEdit>
pub fn edit<P: AsRef<Path>>(path: P) -> Result<FileEdit>
Open the file for editing (reading and writing) but do not create it if it doesn’t exist.
sourcepub fn sync_all(&self) -> Result<()>
pub fn sync_all(&self) -> Result<()>
Attempts to sync all OS-internal metadata to disk.
This function will attempt to ensure that all in-core data reaches the filesystem before returning.
This function is identical to std::fs::File::sync_all except it has error messages which include the action and the path.
sourcepub fn sync_data(&self) -> Result<()>
pub fn sync_data(&self) -> Result<()>
This function is similar to sync_all, except that it may not synchronize file metadata to the filesystem.
This function is identical to std::fs::File::sync_data except it has error messages which include the action and the path.
sourcepub fn set_len(&mut self, size: u64) -> Result<()>
pub fn set_len(&mut self, size: u64) -> Result<()>
Truncates or extends the underlying file, updating the size of this file to become size.
This function is identical to std::fs::File::set_len except:
- It has error messages which include the action and the path.
- It takes
&mut self
instead of&self
.
sourcepub fn set_permissions(&mut self, perm: Permissions) -> Result<()>
pub fn set_permissions(&mut self, perm: Permissions) -> Result<()>
Changes the permissions on the underlying file.
This function is identical to std::fs::File::set_permissions except:
- It has error messages which include the action and the path.
- It takes
&mut self
instead of&self
.
sourcepub fn read_string(&mut self) -> Result<String>
pub fn read_string(&mut self) -> Result<String>
Read what remains of the file to a String
.
Trait Implementations§
source§impl Read for FileEdit
impl Read for FileEdit
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn 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>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
Read
. Read moresource§impl Seek for FileEdit
impl Seek for FileEdit
source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
source§impl Write for FileEdit
impl Write for FileEdit
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)