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]

[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")?;

[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)?;

[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).

[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)?;

[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()?);

[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()?);

[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()?);

[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);

[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()?);

[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);

[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()?);

[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()?);

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);

[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());

[src]

Return a reference to a basic std::path::Path

[src]

Returns the canonical form of the path with all intermediate components normalized and symbolic links resolved.

See PathAbs::canonicalize

[src]

Create a mock file type. For use in tests only.

See the docs for PathAbs::mock

Methods from Deref<Target = PathAbs>

[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);

[src]

Return a reference to a basic std::path::Path

Trait Implementations

impl Clone for PathFile
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl Eq for PathFile
[src]

impl Hash for PathFile
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl PartialEq for PathFile
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl PartialOrd for PathFile
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

[src]

This method tests less than (for self and other) and is used by the < operator. Read more

[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

[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]

[src]

This method returns an Ordering between self and other. Read more

1.21.0
[src]

Compares and returns the maximum of two values. Read more

1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl Debug for PathFile
[src]

[src]

Formats the value using the given formatter. Read more

impl AsRef<PathAbs> for PathFile
[src]

[src]

Performs the conversion.

impl AsRef<PathArc> for PathFile
[src]

[src]

Performs the conversion.

impl AsRef<Path> for PathFile
[src]

[src]

Performs the conversion.

impl AsRef<PathBuf> for PathFile
[src]

[src]

Performs the conversion.

impl Borrow<PathAbs> for PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl Borrow<PathArc> for PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl Borrow<Path> for PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl Borrow<PathBuf> for PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl<'a> Borrow<PathAbs> for &'a PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl<'a> Borrow<PathArc> for &'a PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl<'a> Borrow<Path> for &'a PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl<'a> Borrow<PathBuf> for &'a PathFile
[src]

[src]

Immutably borrows from an owned value. Read more

impl Deref for PathFile
[src]

The resulting type after dereferencing.

[src]

Dereferences the value.

impl Into<PathAbs> for PathFile
[src]

[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]

[src]

Downgrades the PathFile into a PathArc

impl Into<PathBuf> for PathFile
[src]

[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]

[src]

Serialize this value into the given Serde serializer. Read more

impl<'de> Deserialize<'de> for PathFile
[src]

[src]

Deserialize this value from the given Serde deserializer. Read more

Auto Trait Implementations

impl Send for PathFile

impl Sync for PathFile