Skip to content

Commit 77379e6

Browse files
committed
path_tools : AsPath, TryIntoPath, TryIntoCowPath
1 parent 5cb28d3 commit 77379e6

22 files changed

+1079
-153
lines changed

module/core/proper_path_tools/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ full = [
3535
"path_utf8",
3636
]
3737
no_std = []
38+
# qqq : xxx : negate no_std
39+
# use_std = []
3840
use_alloc = [ "no_std" ]
3941
enabled = [ "mod_interface/enabled" ]
4042

module/core/proper_path_tools/Readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ Collection of algorithms and structures to handle paths properly.
99

1010
All functions in the crate don't touch file system, but only process paths.
1111

12+
### Type `AbsolutePath`
13+
14+
The AbsolutePath type ensures that paths are absolute, which helps reduce issues and maintenance costs associated with relative paths. Relative paths can be problematic as they introduce additional variables and complexities, making code analysis, integration, refactoring, and testing more difficult. By using absolute paths, software architecture can be improved, similar to how avoiding global variables can enhance code quality. It is recommended to use relative paths only at the outskirts of an application.
15+
16+
### Trait `AsPath`
17+
18+
This trait is used to avoid redundant allocation of memory by providing a reference to a Path. It is implemented only for types that can either be referenced or are references to Path itself. Unlike `TryIntoPath`, it does not allocate memory on the heap. However, `TryIntoPath` is implemented for a wider range of types because it is not restricted from allocating memory. Unlike `AsRef< Path >`, `AsPath` is implemented for a wider number of types, including those that are not directly convertible to a Path using `AsRef`. This is because `AsPath` is designed to provide a more flexible interface for path-like types, accommodating various representations that can logically be treated as paths.
19+
20+
### Trait `TryIntoPath`
21+
22+
This trait is used to convert any path-like type into an owned PathBuf. Unlike `TryIntoCowPath`, it always returns an owned PathBuf, so there is no need to differentiate between borrowed and owned paths at runtime. Unlike `AsPath`, it is implemented for a wider range of path-like types, similar to `TryIntoCowPath`.
23+
24+
### Trait `TryIntoCowPath`
25+
26+
This trait is designed to avoid redundant memory allocation. Unlike TryIntoPath, it does not allocate memory on the heap if it’s not necessary. Unlike `AsPath`, it is implemented for a wider number of path-like types, similar to TryIntoPath. The drawback is the necessity to differentiate borrowed and owned paths at runtime.
27+
1228
<!-- ### Basic use-case
1329
1430
```rust
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/// Internal namespace.
2+
mod private
3+
{
4+
// use crate::*;
5+
#[cfg( feature = "no_std" )]
6+
extern crate std;
7+
8+
use std::path::Path;
9+
use camino::Utf8Path;
10+
11+
/// A trait for converting various types into a reference to a `Path`.
12+
///
13+
/// This trait is used to avoid redundant allocation of memory by providing a reference to a `Path`.
14+
/// It is implemented only for types that can either be referenced or are references to `Path` itself.
15+
/// Unlike `TryIntoPath`, it does not allocate memory on the heap. However, `TryIntoPath` is implemented for a wider range of types because it is not restricted from allocating memory.
16+
/// Unlike `AsRef<Path>`, `AsPath` is implemented for a wider number of types, including those that are not directly convertible to a `Path` using `AsRef`.
17+
/// This is because `AsPath` is designed to provide a more flexible interface for path-like types, accommodating various representations that can logically be treated as paths.
18+
pub trait AsPath
19+
{
20+
/// Converts the implementing type into a reference to a `Path`.
21+
///
22+
/// # Returns
23+
///
24+
/// A reference to a `Path`.
25+
fn as_path( &self ) -> &Path;
26+
}
27+
28+
/// Implementation of `AsPath` for `str`.
29+
impl AsPath for str
30+
{
31+
fn as_path( &self ) -> &Path
32+
{
33+
Path::new( self )
34+
}
35+
}
36+
37+
/// Implementation of `AsPath` for `Path`.
38+
impl AsPath for Path
39+
{
40+
fn as_path( &self ) -> &Path
41+
{
42+
self
43+
}
44+
}
45+
46+
/// Implementation of `AsPath` for `Utf8Path`.
47+
#[cfg( feature = "path_utf8" )]
48+
impl AsPath for Utf8Path
49+
{
50+
fn as_path( &self ) -> &Path
51+
{
52+
self.as_std_path()
53+
}
54+
}
55+
56+
/// Blanket implementation of `AsPath` for all types that implement `AsRef<Path>`.
57+
impl< T > AsPath for T
58+
where
59+
T : AsRef< Path >,
60+
{
61+
fn as_path( &self ) -> &Path
62+
{
63+
self.as_ref()
64+
}
65+
}
66+
}
67+
68+
crate::mod_interface!
69+
{
70+
orphan use AsPath;
71+
}

module/core/proper_path_tools/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,21 @@ mod_interface!
2020
/// Basic functionality.
2121
layer path;
2222

23+
/// AsPath trait.
24+
layer as_path;
25+
/// TryIntoPath trait.
26+
layer try_into_path;
27+
/// TryIntoPath trait.
28+
layer try_into_cow_path;
29+
2330
/// Transitive TryFrom and TryInto.
2431
layer transitive;
2532

2633
#[ cfg( feature = "path_utf8" ) ]
2734
own use ::camino::{ Utf8Path, Utf8PathBuf };
2835
#[ cfg( not( feature = "no_std" ) ) ]
29-
own use ::std::path::{ PathBuf, Path };
36+
own use ::std::path::{ PathBuf, Path, Component };
37+
#[ cfg( not( feature = "no_std" ) ) ]
38+
own use ::std::borrow::Cow;
3039

3140
}

module/core/proper_path_tools/src/path.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
mod private
44
{
5+
use crate::*;
6+
57
#[ cfg( feature = "no_std" ) ]
68
extern crate std;
79

@@ -321,11 +323,10 @@ mod private
321323
///
322324
/// ```
323325
// qqq : make macro paths_join!( ... )
324-
pub fn join_paths< 'a, I >( paths : I ) -> std::path::PathBuf
326+
pub fn join_paths< I, P >( paths : I ) -> PathBuf
325327
where
326-
// AsPath : AsRef< std::path::Path >,
327-
// I : Iterator< Item = AsPath >,
328-
I : Iterator< Item = &'a std::path::Path >,
328+
I : Iterator< Item = P >,
329+
P : AsPath,
329330
{
330331
#[ cfg( feature = "no_std" ) ]
331332
extern crate alloc;
@@ -338,8 +339,11 @@ mod private
338339

339340
for path in paths
340341
{
341-
let mut path = path.to_string_lossy().replace( '\\', "/" );
342-
path = path.replace( ':', "" );
342+
// let mut path = path.to_string_lossy().replace( '\\', "/" );
343+
let path = path.as_path().to_string_lossy().replace( '\\', "/" );
344+
// qqq : xxx : avoid converting to String, keep it Path
345+
346+
// path = path.replace( ':', "" );
343347
// qqq : this is a bug
344348

345349
let mut added_slah = false;
@@ -1072,4 +1076,7 @@ crate::mod_interface!
10721076
/// Describe native path. Use to pass path to the platfrom.
10731077
layer native_path;
10741078

1079+
/// Convenient joining.
1080+
layer join;
1081+
10751082
}

0 commit comments

Comments
 (0)