FileWrite

Struct FileWrite 

Source
pub struct FileWrite(/* private fields */);
Expand description

A write-only file handle with path() attached and improved error messages. Contains only the methods and trait implementations which are allowed by a write-only file.

§Examples

use std::io::Write;
use path_abs::{PathFile, FileWrite};

let example = "example.txt";

let expected = "foo\nbar";
let mut write = FileWrite::create(example)?;
write.write_all(expected.as_bytes())?;
write.flush();

let file = PathFile::new(example)?;
assert_eq!(expected, file.read_string()?);

Implementations§

Source§

impl FileWrite

Source

pub fn open<P>(path: P, options: OpenOptions) -> Result<FileWrite, Error>
where P: AsRef<Path>,

Open the file with the given OpenOptions but always sets write to true.

Source

pub fn create<P>(path: P) -> Result<FileWrite, Error>
where P: AsRef<Path>,

Open the file in write-only mode, truncating it first if it exists and creating it otherwise.

Source

pub fn append<P>(path: P) -> Result<FileWrite, Error>
where P: AsRef<Path>,

Open the file for appending, creating it if it doesn’t exist.

Source

pub fn edit<P>(path: P) -> Result<FileWrite, Error>
where P: AsRef<Path>,

Open the file for editing (reading and writing) but do not create it if it doesn’t exist.

Source

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.

Source

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.

Source

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 self instead of &self.
Source

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 self instead of &self.
Source

pub fn write_str(&mut self, s: &str) -> Result<(), Error>

Shortcut to self.write_all(s.as_bytes()) with slightly improved error message.

Source

pub fn flush(&mut self) -> Result<(), Error>

std::io::File::flush buth with the new error type.

Methods from Deref<Target = FileOpen>§

Source

pub fn path(&self) -> &PathFile

Get the path associated with the open file.

Source

pub fn metadata(&self) -> Result<Metadata, Error>

Queries metadata about the underlying file.

This function is identical to std::fs::File::metadata except it has error messages which include the action and the path.

Source

pub fn try_clone(&self) -> Result<FileOpen, Error>

Creates a new independently owned handle to the underlying file.

This function is identical to std::fs::File::try_clone except it has error messages which include the action and the path and it returns a FileOpen object.

Trait Implementations§

Source§

impl Debug for FileWrite

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Deref for FileWrite

Source§

type Target = FileOpen

The resulting type after dereferencing.
Source§

fn deref(&self) -> &FileOpen

Dereferences the value.
Source§

impl Into<File> for FileWrite

Source§

fn into(self) -> File

Converts this type into the (usually inferred) input type.
Source§

impl Write for FileWrite

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
Source§

impl Seek for FileWrite

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Erased for T