use std::fs; use std::io::{self, Read, Seek, Write}; use std::path::{Path, PathBuf};
usecrate::errors::{Error, ErrorKind};
/// Wrapper around [`std::fs::File`][std::fs::File] which adds more helpful /// information to all errors. /// /// [std::fs::File]: https://doc.rust-lang.org/stable/std/fs/struct.File.html #[derive(Debug)] pubstruct File {
file: fs::File,
path: PathBuf,
}
// Opens a std File and returns it or an error generator which only needs the path to produce the error. // Exists for the `crate::read*` functions so they don't unconditionally build a PathBuf. pub(crate) fn open(path: &Path) -> Result<std::fs::File, impl FnOnce(PathBuf) -> io::Error> {
fs::File::open(&path).map_err(|err| |path| Error::build(err, ErrorKind::OpenFile, path))
}
// like `open()` but for `crate::write` pub(crate) fn create(path: &Path) -> Result<std::fs::File, impl FnOnce(PathBuf) -> io::Error> {
fs::File::create(&path).map_err(|err| |path| Error::build(err, ErrorKind::CreateFile, path))
}
/// Methods added by fs-err that are not available on /// [`std::fs::File`][std::fs::File]. /// /// [std::fs::File]: https://doc.rust-lang.org/stable/std/fs/struct.File.html impl File { /// Creates a [`File`](struct.File.html) from a raw file and its path. pubfn from_parts<P>(file: fs::File, path: P) -> Self where
P: Into<PathBuf>,
{
File {
file,
path: path.into(),
}
}
/// Extract the raw file and its path from this [`File`](struct.File.html) pubfn into_parts(self) -> (fs::File, PathBuf) {
(self.file, self.path)
}
/// Returns a mutable reference to the underlying [`std::fs::File`][std::fs::File]. /// /// [std::fs::File]: https://doc.rust-lang.org/stable/std/fs/struct.File.html pubfn file_mut(&mutself) -> &mut fs::File {
&mutself.file
}
/// Returns a reference to the path that this file was created with. pubfn path(&self) -> &Path {
&self.path
}
/// Wrap the error in information specific to this `File` object. fn error(&self, source: io::Error, kind: ErrorKind) -> io::Error {
Error::build(source, kind, &self.path)
}
}
#[cfg(unix)] mod unix { usecrate::os::unix::fs::FileExt; usecrate::ErrorKind; use std::io; use std::os::unix::fs::FileExt as _; use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
#[cfg(windows)] mod windows { usecrate::os::windows::fs::FileExt; usecrate::ErrorKind; use std::io; use std::os::windows::{
fs::FileExt as _,
io::{AsRawHandle, IntoRawHandle, RawHandle},
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.