Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing File / OpenOptions constructors #69

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -57,6 +58,33 @@ 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<P>(path: P) -> Result<Self, io::Error>
where
P: Into<PathBuf>,
{
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)),
}
}

/// 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).
Expand Down Expand Up @@ -178,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<usize> {
(&self.file)
.read(buf)
Expand Down Expand Up @@ -206,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<u64> {
(&self.file)
.seek(pos)
Expand Down Expand Up @@ -234,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<usize> {
(&self.file)
.write(buf)
Expand Down
20 changes: 20 additions & 0 deletions src/tokio/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -41,6 +43,24 @@ impl File {
}
}

/// Opens a file in read-write mode.
///
/// Wrapper for [`tokio::fs::File::create_new`].
pub async fn create_new(path: impl Into<PathBuf>) -> Result<Self, io::Error> {
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)),
}
}

/// 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`].
Expand Down
Loading