Struct path_abs::PathFile
[−]
[src]
pub struct PathFile(_);
a PathAbs
that was a file at the time of initialization, with associated methods.
Methods
impl PathFile
[src]
pub fn new<P: AsRef<Path>>(path: P) -> Result<PathFile>
[src]
Instantiate a new PathFile
. The file must exist or io::Error
will be returned.
Returns io::ErrorKind::InvalidInput
if the path exists but is not a file.
Examples
use path_abs::PathFile; let lib = PathFile::new("src/lib.rs")?;
pub fn from_abs(abs: PathAbs) -> Result<PathFile>
[src]
Consume the PathAbs
validating that the path is a file and returning PathFile
. The file
must exist or io::Error
will be returned.
If the path is actually a dir returns io::ErrorKind::InvalidInput
.
Examples
use path_abs::{PathAbs, PathFile}; let lib_abs = PathAbs::new("src/lib.rs")?; let lib_file = PathFile::from_abs(lib_abs)?;
pub fn from_abs_unchecked(abs: PathAbs) -> PathFile
[src]
Do the conversion without checking.
This is typically used by external libraries when the type is already known through some other means (to avoid a syscall).
pub fn create<P: AsRef<Path>>(path: P) -> Result<PathFile>
[src]
Instantiate a new PathFile
, creating an empty file if it doesn't exist.
Examples
use path_abs::PathFile; let example = "example.txt"; let file = PathFile::create(example)?; // It can be done twice with no effect. let _ = PathFile::create(example)?;
pub fn read_string(&self) -> Result<String>
[src]
Read the entire contents of the file into a String
.
Examples
use path_abs::PathFile; let example = "example.txt"; let file = PathFile::create(example)?; let expected = "foo\nbar"; file.write_str(expected)?; assert_eq!(expected, file.read_string()?);
pub fn write_str(&self, s: &str) -> Result<()>
[src]
Write the str
to a file, truncating it first if it exists and creating it otherwise.
Examples
use path_abs::PathFile; let example = "example.txt"; let file = PathFile::create(example)?; let expected = "foo\nbar"; file.write_str(expected)?; assert_eq!(expected, file.read_string()?);
pub fn append_str(&self, s: &str) -> Result<()>
[src]
Append the str
to a file, creating it if it doesn't exist.
Examples
use path_abs::PathFile; let example = "example.txt"; let file = PathFile::create(example)?; let expected = "foo\nbar\nbaz"; file.append_str("foo\nbar")?; file.append_str("\nbaz")?; assert_eq!(expected, file.read_string()?);
pub fn read(&self) -> Result<FileRead>
[src]
Open the file as read-only.
Examples
use std::io::Read; use path_abs::PathFile; let example = "example.txt"; let file = PathFile::create(example)?; let expected = "foo\nbar"; file.write_str(expected)?; let mut read = file.read()?; let mut s = String::new(); read.read_to_string(&mut s)?; assert_eq!(expected, s);
pub fn append(&self) -> Result<FileWrite>
[src]
Open the file as write-only in append mode.
Examples
use std::io::Write; use path_abs::PathFile; let example = "example.txt"; let file = PathFile::create(example)?; let expected = "foo\nbar\n"; file.write_str("foo\n")?; let mut append = file.append()?; append.write_all(b"bar\n")?; append.flush(); assert_eq!(expected, file.read_string()?);
pub fn edit(&self) -> Result<FileEdit>
[src]
Open the file for editing (reading and writing).
Examples
use std::io::{Read, Seek, Write, SeekFrom}; use path_abs::PathFile; let example = "example.txt"; let file = PathFile::create(example)?; let expected = "foo\nbar"; let mut edit = file.edit()?; 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);
pub fn copy<P: AsRef<Path>>(&self, path: P) -> Result<PathFile>
[src]
Copy the file to another location, including permission bits
Examples
use path_abs::PathFile; use std::path::Path; let example = "example.txt"; let example_bk = "example.txt.bk"; let file = PathFile::create(example)?; let contents = "This is some contents"; file.write_str(contents); let file_bk = file.copy(example_bk)?; assert_eq!(contents, file.read_string()?); assert_eq!(contents, file_bk.read_string()?);
pub fn rename<P: AsRef<Path>>(self, to: P) -> Result<PathFile>
[src]
Rename a file, replacing the original file if to
already exists.
This will not work if the new name is on a different mount point.
Examples
use path_abs::PathFile; use std::path::Path; let example = "example.txt"; let example_bk = "example.txt.bk"; let file = PathFile::create(example)?; let contents = "This is some contents"; file.write_str(contents); let file_bk = file.clone().rename(example_bk)?; assert!(!file.exists()); assert_eq!(contents, file_bk.read_string()?);
pub fn symlink<P: AsRef<Path>>(&self, dst: P) -> Result<PathFile>
[src]
Creates a new symbolic link on the filesystem to the dst.
This handles platform specific behavior correctly.
Examples
use path_abs::PathFile; use std::path::Path; let example = "example.txt"; let example_sym = "example.txt.sym"; let file = PathFile::create(example)?; let contents = "This is some contents"; file.write_str(contents); let file_sym = file.symlink(example_sym)?; // They have a different "absolute path" assert_ne!(file, file_sym); // But they can be canonicalized to the same file. let file_can = file_sym.canonicalize()?; assert_eq!(file, file_can);
pub fn remove(self) -> Result<()>
[src]
Remove (delete) the file from the filesystem, consuming self.
Examples
use path_abs::PathFile; use std::path::Path; let example = "example.txt"; let file = PathFile::create(example)?; assert!(file.exists()); file.remove()?; // file.exists() <--- COMPILER ERROR, `file` was consumed assert!(!Path::new(example).exists());
pub fn as_path(&self) -> &Path
[src]
Return a reference to a basic std::path::Path
pub fn canonicalize(&self) -> Result<PathFile>
[src]
Returns the canonical form of the path with all intermediate components normalized and symbolic links resolved.
pub fn mock<P: AsRef<Path>>(path: P) -> PathFile
[src]
Create a mock file type. For use in tests only.
See the docs for PathAbs::mock
Methods from Deref<Target = PathAbs>
pub fn parent_dir(&self) -> Option<PathDir>
[src]
Get the parent directory of this path as a PathDir
.
This does not make aditional syscalls, as the parent by definition must be a directory and exist.
Examples
use path_abs::{PathDir, PathFile}; let lib = PathFile::new("src/lib.rs")?; let src = lib.parent_dir().unwrap(); assert_eq!(PathDir::new("src")?, src);
pub fn as_path(&self) -> &Path
[src]
Return a reference to a basic std::path::Path
Trait Implementations
impl Clone for PathFile
[src]
fn clone(&self) -> PathFile
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Eq for PathFile
[src]
impl Hash for PathFile
[src]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl PartialEq for PathFile
[src]
fn eq(&self, __arg_0: &PathFile) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &PathFile) -> bool
[src]
This method tests for !=
.
impl PartialOrd for PathFile
[src]
fn partial_cmp(&self, __arg_0: &PathFile) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &PathFile) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &PathFile) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &PathFile) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &PathFile) -> bool
[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl Ord for PathFile
[src]
fn cmp(&self, __arg_0: &PathFile) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
impl Debug for PathFile
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl AsRef<PathAbs> for PathFile
[src]
impl AsRef<PathArc> for PathFile
[src]
impl AsRef<Path> for PathFile
[src]
impl AsRef<PathBuf> for PathFile
[src]
impl Borrow<PathAbs> for PathFile
[src]
impl Borrow<PathArc> for PathFile
[src]
impl Borrow<Path> for PathFile
[src]
impl Borrow<PathBuf> for PathFile
[src]
impl<'a> Borrow<PathAbs> for &'a PathFile
[src]
impl<'a> Borrow<PathArc> for &'a PathFile
[src]
impl<'a> Borrow<Path> for &'a PathFile
[src]
impl<'a> Borrow<PathBuf> for &'a PathFile
[src]
impl Deref for PathFile
[src]
type Target = PathAbs
The resulting type after dereferencing.
fn deref(&self) -> &PathAbs
[src]
Dereferences the value.
impl Into<PathAbs> for PathFile
[src]
fn into(self) -> PathAbs
[src]
Downgrades the PathFile
into a PathAbs
Examples
use std::path::PathBuf; use path_abs::{PathFile, PathAbs}; let file = PathFile::new("src/lib.rs")?; let abs: PathAbs = file.clone().into();
impl Into<PathArc> for PathFile
[src]
impl Into<PathBuf> for PathFile
[src]
fn into(self) -> PathBuf
[src]
Downgrades the PathFile
into a PathBuf
. Avoids a clone if this is the only reference.
Examples
use path_abs::PathFile; use std::path::PathBuf; let file = PathFile::new("src/lib.rs")?; let buf: PathBuf = file.into();
impl Serialize for PathFile
[src]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer,
[src]
S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl<'de> Deserialize<'de> for PathFile
[src]
fn deserialize<D>(deserializer: D) -> Result<PathFile, D::Error> where
D: Deserializer<'de>,
[src]
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more