From 4a9209424763d2c768bd8855f660594dcc4410c0 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:21:07 +0100 Subject: [PATCH 1/4] Add File::create_new wrapper --- src/file.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/file.rs b/src/file.rs index fafc830..cdc17f7 100644 --- a/src/file.rs +++ b/src/file.rs @@ -57,6 +57,20 @@ 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(); + match fs::File::create_new(&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 dad1899e599865a5d8b482acf6584e7147a36a8a Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:22:41 +0100 Subject: [PATCH 2/4] Add File::options wrapper --- src/file.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/file.rs b/src/file.rs index cdc17f7..7e17db0 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. @@ -71,6 +72,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 55ff084d1426e0f69108ea0d7659a678a64f7b1c Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:23:33 +0100 Subject: [PATCH 3/4] 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 d7584cd221674d0dcf43c7af704045f1611f0e47 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jan 2025 14:24:07 +0100 Subject: [PATCH 4/4] 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`].