Skip to content

Commit 9ae1e46

Browse files
committed
add pathbuf implementation to common
1 parent 02f9073 commit 9ae1e46

File tree

5 files changed

+55
-147
lines changed

5 files changed

+55
-147
lines changed

library/std/src/sys/fs/common.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#![allow(dead_code)] // not used on all platforms
22

3-
use crate::fs;
3+
use crate::fmt;
4+
use crate::fs::{self, File, OpenOptions, create_dir, remove_dir, remove_file, rename};
45
use crate::io::{self, Error, ErrorKind};
5-
use crate::path::Path;
6+
use crate::path::{Path, PathBuf};
67
use crate::sys_common::ignore_notfound;
78

89
pub(crate) const NOT_FILE_ERROR: Error = io::const_error!(
@@ -58,3 +59,52 @@ pub fn exists(path: &Path) -> io::Result<bool> {
5859
Err(error) => Err(error),
5960
}
6061
}
62+
63+
pub struct Dir {
64+
path: PathBuf,
65+
}
66+
67+
impl Dir {
68+
pub fn new<P: AsRef<Path>>(path: P) -> io::Result<Self> {
69+
Ok(Self { path: path.as_ref().to_path_buf() })
70+
}
71+
72+
pub fn new_with<P: AsRef<Path>>(path: P, _opts: &OpenOptions) -> io::Result<Self> {
73+
Ok(Self { path: path.as_ref().to_path_buf() })
74+
}
75+
76+
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> {
77+
File::open(self.path.join(path))
78+
}
79+
80+
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> {
81+
opts.open(self.path.join(path))
82+
}
83+
84+
pub fn create_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
85+
create_dir(self.path.join(path))
86+
}
87+
88+
pub fn remove_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
89+
remove_file(self.path.join(path))
90+
}
91+
92+
pub fn remove_dir<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
93+
remove_dir(self.path.join(path))
94+
}
95+
96+
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
97+
&self,
98+
from: P,
99+
to_dir: &Self,
100+
to: Q,
101+
) -> io::Result<()> {
102+
rename(self.path.join(from), to_dir.path.join(to))
103+
}
104+
}
105+
106+
impl fmt::Debug for Dir {
107+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108+
f.debug_struct("Dir").field("path", &self.path).finish()
109+
}
110+
}

library/std/src/sys/fs/hermit.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::path::{Path, PathBuf};
1111
use crate::sync::Arc;
1212
use crate::sys::common::small_c_string::run_path_with_cstr;
1313
use crate::sys::fd::FileDesc;
14-
pub use crate::sys::fs::common::{copy, exists};
14+
pub use crate::sys::fs::common::{Dir, copy, exists};
1515
use crate::sys::time::SystemTime;
1616
use crate::sys::{cvt, unsupported, unsupported_err};
1717
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
@@ -20,53 +20,6 @@ use crate::{fmt, mem};
2020
#[derive(Debug)]
2121
pub struct File(FileDesc);
2222

23-
pub struct Dir(!);
24-
25-
impl Dir {
26-
pub fn new<P: AsRef<Path>>(_path: P) -> io::Result<Self> {
27-
unsupported()
28-
}
29-
30-
pub fn new_with<P: AsRef<Path>>(_path: P, opts: &OpenOptions) -> io::Result<Self> {
31-
unsupported()
32-
}
33-
34-
pub fn open<P: AsRef<Path>>(&self, _path: P) -> io::Result<File> {
35-
self.0
36-
}
37-
38-
pub fn open_with<P: AsRef<Path>>(&self, _path: P, opts: &OpenOptions) -> io::Result<File> {
39-
self.0
40-
}
41-
42-
pub fn create_dir<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
43-
self.0
44-
}
45-
46-
pub fn remove_file<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
47-
self.0
48-
}
49-
50-
pub fn remove_dir<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
51-
self.0
52-
}
53-
54-
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
55-
&self,
56-
_from: P,
57-
_to_dir: &Self,
58-
_to: Q,
59-
) -> io::Result<()> {
60-
self.0
61-
}
62-
}
63-
64-
impl fmt::Debug for Dir {
65-
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
66-
self.0
67-
}
68-
}
69-
7023
#[derive(Clone)]
7124
pub struct FileAttr {
7225
stat_val: stat_struct,

library/std/src/sys/fs/solid.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::os::raw::{c_int, c_short};
99
use crate::os::solid::ffi::OsStrExt;
1010
use crate::path::{Path, PathBuf};
1111
use crate::sync::Arc;
12-
pub use crate::sys::fs::common::exists;
12+
pub use crate::sys::fs::common::{Dir, exists};
1313
use crate::sys::pal::{abi, error};
1414
use crate::sys::time::SystemTime;
1515
use crate::sys::{unsupported, unsupported_err};
@@ -85,8 +85,6 @@ pub struct FileType(c_short);
8585
#[derive(Debug)]
8686
pub struct DirBuilder {}
8787

88-
pub struct Dir(!);
89-
9088
impl FileAttr {
9189
pub fn size(&self) -> u64 {
9290
self.stat.st_size as u64
@@ -195,51 +193,6 @@ impl Drop for InnerReadDir {
195193
}
196194
}
197195

198-
impl Dir {
199-
pub fn new<P: AsRef<Path>>(_path: P) -> io::Result<Self> {
200-
unsupported()
201-
}
202-
203-
pub fn new_with<P: AsRef<Path>>(_path: P, opts: &OpenOptions) -> io::Result<Self> {
204-
unsupported()
205-
}
206-
207-
pub fn open<P: AsRef<Path>>(&self, _path: P) -> io::Result<File> {
208-
self.0
209-
}
210-
211-
pub fn open_with<P: AsRef<Path>>(&self, _path: P, opts: &OpenOptions) -> io::Result<File> {
212-
self.0
213-
}
214-
215-
pub fn create_dir<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
216-
self.0
217-
}
218-
219-
pub fn remove_file<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
220-
self.0
221-
}
222-
223-
pub fn remove_dir<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
224-
self.0
225-
}
226-
227-
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
228-
&self,
229-
_from: P,
230-
_to_dir: &Self,
231-
_to: Q,
232-
) -> io::Result<()> {
233-
self.0
234-
}
235-
}
236-
237-
impl fmt::Debug for Dir {
238-
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
239-
self.0
240-
}
241-
}
242-
243196
impl DirEntry {
244197
pub fn path(&self) -> PathBuf {
245198
self.inner.root.join(OsStr::from_bytes(

library/std/src/sys/fs/uefi.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub struct FileAttr {
2020
size: u64,
2121
}
2222

23-
#[derive(Debug)]
2423
pub struct Dir(!);
2524

2625
pub struct ReadDir(!);

library/std/src/sys/fs/wasi.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::path::{Path, PathBuf};
99
use crate::sync::Arc;
1010
use crate::sys::common::small_c_string::run_path_with_cstr;
1111
use crate::sys::fd::WasiFd;
12-
pub use crate::sys::fs::common::exists;
12+
pub use crate::sys::fs::common::{Dir, exists};
1313
use crate::sys::time::SystemTime;
1414
use crate::sys::{unsupported, unsupported_err};
1515
use crate::sys_common::{AsInner, FromInner, IntoInner, ignore_notfound};
@@ -24,8 +24,6 @@ pub struct FileAttr {
2424
meta: wasi::Filestat,
2525
}
2626

27-
pub struct Dir(!);
28-
2927
pub struct ReadDir {
3028
inner: Arc<ReadDirInner>,
3129
state: ReadDirState,
@@ -162,51 +160,6 @@ impl FileType {
162160
}
163161
}
164162

165-
impl Dir {
166-
pub fn new<P: AsRef<Path>>(_path: P) -> io::Result<Self> {
167-
unsupported()
168-
}
169-
170-
pub fn new_with<P: AsRef<Path>>(_path: P, opts: &OpenOptions) -> io::Result<Self> {
171-
unsupported()
172-
}
173-
174-
pub fn open<P: AsRef<Path>>(&self, _path: P) -> io::Result<File> {
175-
self.0
176-
}
177-
178-
pub fn open_with<P: AsRef<Path>>(&self, _path: P, opts: &OpenOptions) -> io::Result<File> {
179-
self.0
180-
}
181-
182-
pub fn create_dir<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
183-
self.0
184-
}
185-
186-
pub fn remove_file<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
187-
self.0
188-
}
189-
190-
pub fn remove_dir<P: AsRef<Path>>(&self, _path: P) -> io::Result<()> {
191-
self.0
192-
}
193-
194-
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(
195-
&self,
196-
_from: P,
197-
_to_dir: &Self,
198-
_to: Q,
199-
) -> io::Result<()> {
200-
self.0
201-
}
202-
}
203-
204-
impl fmt::Debug for Dir {
205-
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
206-
self.0
207-
}
208-
}
209-
210163
impl ReadDir {
211164
fn new(dir: File, root: PathBuf) -> ReadDir {
212165
ReadDir {

0 commit comments

Comments
 (0)