diff options
Diffstat (limited to 'buffered-reader/src/file_generic.rs')
-rw-r--r-- | buffered-reader/src/file_generic.rs | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/buffered-reader/src/file_generic.rs b/buffered-reader/src/file_generic.rs index 135266dd..8ad42744 100644 --- a/buffered-reader/src/file_generic.rs +++ b/buffered-reader/src/file_generic.rs @@ -31,6 +31,20 @@ impl<C: fmt::Debug + Sync + Send> fmt::Debug for File<C> { } impl File<()> { + /// Wraps a [`fs::File`]. + /// + /// The given `path` should be the path that has been used to + /// obtain `file` from. It is used in error messages to provide + /// context to the user. + /// + /// While this is slightly less convenient than [`Self::open`], it + /// allows one to inspect or manipulate the [`fs::File`] object + /// before handing it off. For example, one can inspect the + /// metadata. + pub fn new<P: AsRef<Path>>(file: fs::File, path: P) -> io::Result<Self> { + Self::new_with_cookie(file, path.as_ref(), ()) + } + /// Opens the given file. pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> { Self::with_cookie(path, ()) @@ -38,11 +52,27 @@ impl File<()> { } impl<C: fmt::Debug + Sync + Send> File<C> { + /// Like [`Self::new`], but sets a cookie. + /// + /// The given `path` should be the path that has been used to + /// obtain `file` from. It is used in error messages to provide + /// context to the user. + /// + /// While this is slightly less convenient than + /// [`Self::with_cookie`], it allows one to inspect or manipulate + /// the [`fs::File`] object before handing it off. For example, + /// one can inspect the metadata. + pub fn new_with_cookie<P: AsRef<Path>>(file: fs::File, path: P, cookie: C) + -> io::Result<Self> { + let path = path.as_ref(); + Ok(File(Generic::with_cookie(file, None, cookie), path.into())) + } + /// Like [`Self::open`], but sets a cookie. pub fn with_cookie<P: AsRef<Path>>(path: P, cookie: C) -> io::Result<Self> { let path = path.as_ref(); let file = fs::File::open(path).map_err(|e| FileError::new(path, e))?; - Ok(File(Generic::with_cookie(file, None, cookie), path.into())) + Self::new_with_cookie(file, path, cookie) } } |