Skip to content

Commit 5ba87e1

Browse files
authored
Add missing File / OpenOptions constructors (#69)
* Add File::create_new wrapper * Add File::options wrapper * Add tokio File::create_new wrapper * Add tokio File::options wrapper * Fix latest clippy lints
1 parent 5cefbe6 commit 5ba87e1

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/file.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io::{self, Read, Seek, Write};
33
use std::path::{Path, PathBuf};
44

55
use crate::errors::{Error, ErrorKind};
6+
use crate::OpenOptions;
67

78
/// Wrapper around [`std::fs::File`][std::fs::File] which adds more helpful
89
/// information to all errors.
@@ -57,6 +58,33 @@ impl File {
5758
}
5859
}
5960

61+
/// Opens a file in read-write mode.
62+
///
63+
/// Wrapper for [`File::create_new`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.create_new).
64+
pub fn create_new<P>(path: P) -> Result<Self, io::Error>
65+
where
66+
P: Into<PathBuf>,
67+
{
68+
let path = path.into();
69+
// TODO: Use fs::File::create_new once MSRV is at least 1.77
70+
match fs::OpenOptions::new()
71+
.read(true)
72+
.write(true)
73+
.create_new(true)
74+
.open(&path)
75+
{
76+
Ok(file) => Ok(File::from_parts(file, path)),
77+
Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)),
78+
}
79+
}
80+
81+
/// Returns a new `OpenOptions` object.
82+
///
83+
/// Wrapper for [`File::options`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.options).
84+
pub fn options() -> OpenOptions {
85+
OpenOptions::new()
86+
}
87+
6088
/// Attempts to sync all OS-internal metadata to disk.
6189
///
6290
/// Wrapper for [`File::sync_all`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.sync_all).
@@ -178,7 +206,7 @@ impl Read for File {
178206
}
179207
}
180208

181-
impl<'a> Read for &'a File {
209+
impl Read for &File {
182210
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
183211
(&self.file)
184212
.read(buf)
@@ -206,7 +234,7 @@ impl Seek for File {
206234
}
207235
}
208236

209-
impl<'a> Seek for &'a File {
237+
impl Seek for &File {
210238
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
211239
(&self.file)
212240
.seek(pos)
@@ -234,7 +262,7 @@ impl Write for File {
234262
}
235263
}
236264

237-
impl<'a> Write for &'a File {
265+
impl Write for &File {
238266
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
239267
(&self.file)
240268
.write(buf)

src/tokio/file.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use tokio::fs;
99
use tokio::fs::File as TokioFile;
1010
use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
1111

12+
use super::OpenOptions;
13+
1214
/// Wrapper around [`tokio::fs::File`] which adds more helpful
1315
/// information to all errors.
1416
#[derive(Debug)]
@@ -41,6 +43,24 @@ impl File {
4143
}
4244
}
4345

46+
/// Opens a file in read-write mode.
47+
///
48+
/// Wrapper for [`tokio::fs::File::create_new`].
49+
pub async fn create_new(path: impl Into<PathBuf>) -> Result<Self, io::Error> {
50+
let path = path.into();
51+
match fs::File::create_new(&path).await {
52+
Ok(file) => Ok(File::from_parts(file, path)),
53+
Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)),
54+
}
55+
}
56+
57+
/// Returns a new `OpenOptions` object.
58+
///
59+
/// Wrapper for [`tokio::fs::File::options`].
60+
pub fn options() -> OpenOptions {
61+
OpenOptions::new()
62+
}
63+
4464
/// Converts a [`crate::File`] to a [`tokio::fs::File`].
4565
///
4666
/// Wrapper for [`tokio::fs::File::from_std`].

0 commit comments

Comments
 (0)