From ef0e6a6a736d8c1ddc8f40265c698aa0746a252f Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:21:07 +0100 Subject: [PATCH 1/5] Add File::create_new wrapper --- src/file.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/file.rs b/src/file.rs index fafc830..32e47b3 100644 --- a/src/file.rs +++ b/src/file.rs @@ -57,6 +57,26 @@ impl File { } } + /// Opens a file in read-write mode. + /// + /// Wrapper for [`File::create_new`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.create_new). + pub fn create_new

(path: P) -> Result + where + P: Into, + { + let path = path.into(); + // TODO: Use fs::File::create_new once MSRV is at least 1.77 + match fs::OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(&path) + { + Ok(file) => Ok(File::from_parts(file, path)), + Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)), + } + } + /// Attempts to sync all OS-internal metadata to disk. /// /// Wrapper for [`File::sync_all`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.sync_all). From d13518c039e9887318b715fce0222f6a35a9fde6 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:22:41 +0100 Subject: [PATCH 2/5] Add File::options wrapper --- src/file.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/file.rs b/src/file.rs index 32e47b3..b906ab1 100644 --- a/src/file.rs +++ b/src/file.rs @@ -3,6 +3,7 @@ use std::io::{self, Read, Seek, Write}; use std::path::{Path, PathBuf}; use crate::errors::{Error, ErrorKind}; +use crate::OpenOptions; /// Wrapper around [`std::fs::File`][std::fs::File] which adds more helpful /// information to all errors. @@ -77,6 +78,13 @@ impl File { } } + /// Returns a new `OpenOptions` object. + /// + /// Wrapper for [`File::options`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.options). + pub fn options() -> OpenOptions { + OpenOptions::new() + } + /// Attempts to sync all OS-internal metadata to disk. /// /// Wrapper for [`File::sync_all`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.sync_all). From aad99bb88e21b36d0c363ac4e212331503e0df68 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:23:33 +0100 Subject: [PATCH 3/5] Add tokio File::create_new wrapper --- src/tokio/file.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/tokio/file.rs b/src/tokio/file.rs index 610cfdc..be13e18 100644 --- a/src/tokio/file.rs +++ b/src/tokio/file.rs @@ -41,6 +41,17 @@ impl File { } } + /// Opens a file in read-write mode. + /// + /// Wrapper for [`tokio::fs::File::create_new`]. + pub async fn create_new(path: impl Into) -> Result { + let path = path.into(); + match fs::File::create_new(&path).await { + Ok(file) => Ok(File::from_parts(file, path)), + Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)), + } + } + /// Converts a [`crate::File`] to a [`tokio::fs::File`]. /// /// Wrapper for [`tokio::fs::File::from_std`]. From 3e1d1bdb350d1bf98aedad44fbd43dfc73fbdea4 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:24:07 +0100 Subject: [PATCH 4/5] Add tokio File::options wrapper --- src/tokio/file.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tokio/file.rs b/src/tokio/file.rs index be13e18..3308a2c 100644 --- a/src/tokio/file.rs +++ b/src/tokio/file.rs @@ -9,6 +9,8 @@ use tokio::fs; use tokio::fs::File as TokioFile; use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf}; +use super::OpenOptions; + /// Wrapper around [`tokio::fs::File`] which adds more helpful /// information to all errors. #[derive(Debug)] @@ -52,6 +54,13 @@ impl File { } } + /// Returns a new `OpenOptions` object. + /// + /// Wrapper for [`tokio::fs::File::options`]. + pub fn options() -> OpenOptions { + OpenOptions::new() + } + /// Converts a [`crate::File`] to a [`tokio::fs::File`]. /// /// Wrapper for [`tokio::fs::File::from_std`]. From f2456637f10332d230654fbc1f2dc4eb84712d66 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 21 Jan 2025 21:27:49 +0100 Subject: [PATCH 5/5] Fix latest clippy lints --- src/file.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file.rs b/src/file.rs index b906ab1..32440c0 100644 --- a/src/file.rs +++ b/src/file.rs @@ -206,7 +206,7 @@ impl Read for File { } } -impl<'a> Read for &'a File { +impl Read for &File { fn read(&mut self, buf: &mut [u8]) -> std::io::Result { (&self.file) .read(buf) @@ -234,7 +234,7 @@ impl Seek for File { } } -impl<'a> Seek for &'a File { +impl Seek for &File { fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { (&self.file) .seek(pos) @@ -262,7 +262,7 @@ impl Write for File { } } -impl<'a> Write for &'a File { +impl Write for &File { fn write(&mut self, buf: &[u8]) -> std::io::Result { (&self.file) .write(buf)