Skip to content

Commit 0ff4212

Browse files
committed
fs-err: Use AsRef<Path> to match std::fs
In order for fs-err to be a drop-in replacement for std::fs, it needs to match the std::fs signature exactly. Note that this can result in an extra allocation when the path provided is already a PathBuf, but keeping the same API as std::fs is better than over-optimizing for that case.
1 parent 5f9b60c commit 0ff4212

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed

src/dir.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use std::ffi::OsString;
22
use std::fs;
33
use std::io;
4-
use std::path::PathBuf;
4+
use std::path::{Path, PathBuf};
55

66
use crate::errors::{Error, ErrorKind};
77

88
/// Returns an iterator over the entries within a directory.
99
///
1010
/// Wrapper for [`fs::read_dir`](https://doc.rust-lang.org/stable/std/fs/fn.read_dir.html).
11-
pub fn read_dir<P: Into<PathBuf>>(path: P) -> io::Result<ReadDir> {
12-
let path = path.into();
11+
pub fn read_dir<P: AsRef<Path>>(path: P) -> io::Result<ReadDir> {
12+
let path = path.as_ref();
1313

14-
match fs::read_dir(&path) {
15-
Ok(inner) => Ok(ReadDir { inner, path }),
16-
Err(source) => Err(Error::build(source, ErrorKind::ReadDir, path)),
14+
match fs::read_dir(path) {
15+
Ok(inner) => Ok(ReadDir { inner, path: path.to_path_buf() }),
16+
Err(source) => Err(Error::build(source, ErrorKind::ReadDir, path.to_path_buf())),
1717
}
1818
}
1919

src/file.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,47 +33,38 @@ impl File {
3333
/// Attempts to open a file in read-only mode.
3434
///
3535
/// Wrapper for [`File::open`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.open).
36-
pub fn open<P>(path: P) -> Result<Self, io::Error>
37-
where
38-
P: Into<PathBuf>,
39-
{
40-
let path = path.into();
41-
match open(&path) {
42-
Ok(file) => Ok(File::from_parts(file, path)),
43-
Err(err_gen) => Err(err_gen(path)),
36+
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
37+
let path = path.as_ref();
38+
match open(path) {
39+
Ok(file) => Ok(File::from_parts(file, path.to_path_buf())),
40+
Err(err_gen) => Err(err_gen(path.to_path_buf())),
4441
}
4542
}
4643

4744
/// Opens a file in write-only mode.
4845
///
4946
/// Wrapper for [`File::create`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.create).
50-
pub fn create<P>(path: P) -> Result<Self, io::Error>
51-
where
52-
P: Into<PathBuf>,
53-
{
54-
let path = path.into();
55-
match create(&path) {
56-
Ok(file) => Ok(File::from_parts(file, path)),
57-
Err(err_gen) => Err(err_gen(path)),
47+
pub fn create<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
48+
let path = path.as_ref();
49+
match create(path) {
50+
Ok(file) => Ok(File::from_parts(file, path.to_path_buf())),
51+
Err(err_gen) => Err(err_gen(path.to_path_buf())),
5852
}
5953
}
6054

6155
/// Opens a file in read-write mode.
6256
///
6357
/// 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();
58+
pub fn create_new<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
59+
let path = path.as_ref();
6960
// TODO: Use fs::File::create_new once MSRV is at least 1.77
7061
match fs::OpenOptions::new()
7162
.read(true)
7263
.write(true)
7364
.create_new(true)
74-
.open(&path)
65+
.open(path)
7566
{
76-
Ok(file) => Ok(File::from_parts(file, path)),
67+
Ok(file) => Ok(File::from_parts(file, path.to_path_buf())),
7768
Err(err) => Err(Error::build(err, ErrorKind::CreateFile, path)),
7869
}
7970
}

src/open_options.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fs, io, path::PathBuf};
1+
use std::{fs, io, path::{Path, PathBuf}};
22

33
use crate::errors::{Error, ErrorKind};
44

@@ -66,13 +66,10 @@ impl OpenOptions {
6666
/// Opens a file at `path` with the options specified by `self`.
6767
///
6868
/// Wrapper for [`std::fs::OpenOptions::open`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open)
69-
pub fn open<P>(&self, path: P) -> io::Result<crate::File>
70-
where
71-
P: Into<PathBuf>,
72-
{
73-
let path = path.into();
69+
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<crate::File> {
70+
let path = path.as_ref();
7471
match self.0.open(&path) {
75-
Ok(file) => Ok(crate::File::from_parts(file, path)),
72+
Ok(file) => Ok(crate::File::from_parts(file, path.to_path_buf())),
7673
Err(source) => Err(Error::build(source, ErrorKind::OpenFile, path)),
7774
}
7875
}

0 commit comments

Comments
 (0)