pub struct FileEdit(/* private fields */);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>(path: P, options: OpenOptions) -> Result<FileEdit, Error>
pub fn open<P>(path: P, options: OpenOptions) -> Result<FileEdit, Error>
Open the file with the given OpenOptions but always sets read and write to true.
Sourcepub fn create<P>(path: P) -> Result<FileEdit, Error>
pub fn create<P>(path: P) -> Result<FileEdit, Error>
Open the file in editing mode, truncating it first if it exists and creating it otherwise.
Sourcepub fn append<P>(path: P) -> Result<FileEdit, Error>
pub fn append<P>(path: P) -> Result<FileEdit, Error>
Open the file for appending, creating it if it doesn’t exist.
Sourcepub fn edit<P>(path: P) -> Result<FileEdit, Error>
pub fn edit<P>(path: P) -> Result<FileEdit, Error>
Open the file for editing (reading and writing) but do not create it if it doesn’t exist.
Sourcepub fn sync_all(&self) -> Result<(), Error>
pub fn sync_all(&self) -> Result<(), Error>
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<(), Error>
pub fn sync_data(&self) -> Result<(), Error>
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<(), Error>
pub fn set_len(&mut self, size: u64) -> Result<(), Error>
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 selfinstead of&self.
Sourcepub fn set_permissions(&mut self, perm: Permissions) -> Result<(), Error>
pub fn set_permissions(&mut self, perm: Permissions) -> Result<(), Error>
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 selfinstead of&self.
Sourcepub fn read_string(&mut self) -> Result<String, Error>
pub fn read_string(&mut self) -> Result<String, Error>
Read what remains of the file to a String.
Methods from Deref<Target = FileOpen>§
Trait Implementations§
Source§impl Read for FileEdit
impl Read for FileEdit
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
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>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> 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<'_, u8>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(
&mut self,
cursor: BorrowedCursor<'_, u8>,
) -> Result<(), Error>
fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> 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 more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
1.0.0 · Source§fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
limit bytes from it. Read moreSource§fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
read_array)Source§impl Seek for FileEdit
impl Seek for FileEdit
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)Source§impl Write for FileEdit
impl Write for FileEdit
Source§fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
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)Auto Trait Implementations§
impl Freeze for FileEdit
impl RefUnwindSafe for FileEdit
impl Send for FileEdit
impl Sync for FileEdit
impl Unpin for FileEdit
impl UnsafeUnpin for FileEdit
impl UnwindSafe for FileEdit
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<T> Erased for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more