usecrate::errors::{Error, ErrorKind}; use std::fs::{Metadata, Permissions}; use std::io; use std::io::{IoSlice, SeekFrom}; use std::path::{Path, PathBuf}; use std::pin::Pin; use std::task::{ready, Context, Poll}; use tokio::fs; use tokio::fs::File as TokioFile; use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
/// Wrapper around [`tokio::fs::File`] which adds more helpful /// information to all errors. #[derive(Debug)] #[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] pubstruct File {
tokio: fs::File,
path: PathBuf,
}
impl File { /// Wrapper for [`tokio::fs::File::open`]. pubasyncfn open(path: impl Into<PathBuf>) -> io::Result<File> { let path = path.into(); let f = TokioFile::open(&path)
.await
.map_err(|err| Error::build(err, ErrorKind::OpenFile, &path))?;
Ok(File::from_parts(f, path))
}
/// Wrapper for [`tokio::fs::File::create`]. pubasyncfn create(path: impl Into<PathBuf>) -> io::Result<File> { let path = path.into(); match TokioFile::create(&path).await {
Ok(f) => Ok(File::from_parts(f, path)),
Err(err) => Err(Error::build(err, ErrorKind::CreateFile, &path)),
}
}
/// Wrapper for [`tokio::fs::File::from_std`]. pubfn from_std(std: crate::File) -> File { let (std, path) = std.into_parts();
File::from_parts(TokioFile::from_std(std), path)
}
/// Methods added by fs-err that are not available on /// [`tokio::fs::File`]. impl File { /// Creates a [`File`](struct.File.html) from a raw file and its path. pubfn from_parts<P>(file: TokioFile, path: P) -> Self where
P: Into<PathBuf>,
{
File {
tokio: file,
path: path.into(),
}
}
/// Extract the raw file and its path from this [`File`](struct.File.html). pubfn into_parts(self) -> (TokioFile, PathBuf) {
(self.tokio, self.path)
}
/// Returns a reference to the underlying [`tokio::fs::File`]. pubfn file(&self) -> &TokioFile {
&self.tokio
}
/// Returns a mutable reference to the underlying [`tokio::fs::File`]. pubfn file_mut(&mutself) -> &mut TokioFile {
&mutself.tokio
}
/// 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)
}
}
impl From<crate::File> for File { fn from(f: crate::File) -> Self { let (f, path) = f.into_parts();
File::from_parts(f.into(), path)
}
}
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.