-
Notifications
You must be signed in to change notification settings - Fork 7
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
Thoughts on NativePath #32
Comments
Have you tried using You can use TypedPath::derive to create it from a string or series of bytes. |
I’ve used TypedPath to mark functions that should accept either encoding, but in this case, there’s no way to indicate in a type-safe way that a function expects to accept a path that’s always in the native encoding (for example, because it will deref it to std::path::Path internally) or that a function expects a UnixPath and should not accept a NativePath. |
@encounter do you have a proposal for the redesign of |
Sure, I was considering prototyping it in a PR if you were interested in the concept. My idea is to have Also, with I’m wondering if Just to clarify my use case: my application deals with native system paths and also Unix-style paths from configuration files. My desire is to use typed-path to ensure that native paths are converted into Unix-style when writing to configuration, and converted back to native style before being joined with native paths. The other benefit is that I use the |
IIRC, there was a previous request to support
Wondering if we could have NativePath be a newtype struct around UnixPath or WindowsPath, and implement Deref and DerefMut for the inner type. Reasoning being that I still want people to be able to import and seamlessly use an interface that behaves like std::path::Path (and associated). An example (not written to compile): pub struct NativeType(
#[cfg(unix)]
UnixPath,
#[cfg(windows)]
WindowsPath,
)
impl Deref for NativeType {
#[cfg(unix)]
type Target = UnixPath;
#[cfg(windows)]
type Target = WindowsPath;
fn deref(&self) -> &Self::Target {
&self.0
}
} |
I see, keeping NativePath seems fine then.
I think that means you can still end up doing: let path: &UnixPath = NativePath::new("path");
// or
fn foo(path: &UnixPath) {}
foo(NativePath::new("path"); But I haven’t tested. If so, NativePath may have to implement its own implementations of each Path method. Basically a copy of std::path::Path if I understand correctly. |
Yeah, this seems like the approach that would have to be taken. |
@encounter I think I've devised a solution that is less taxing than reimplementing the entire path and pathbuf types while still letting you have a distinct type that you can check without needing to use conditional config. #34 provides Let me know if this solves the problem for you, and if so I'll publish as |
Nice! I just tried it out, and it seems like a good solution. However, I think only fn foo(path: &UnixPath) -> _ {
std::fs::exists(path)
} which compiles on Unix, but not Windows. It's a definite improvement though! Also, the |
You mean removing the implementation from If so, I'm fine with that as part of the |
Yeah, I think it makes sense from a type safety perspective. If you wanted to bump MSRV to 1.74, I think these may be useful as well: #[cfg(all(feature = "std", not(target_family = "wasm")))]
impl<'a> From<&'a StdPath> for &'a PlatformPath {
fn from(std_path: &'a StdPath) -> &'a PlatformPath {
PlatformPath::new(std_path.as_os_str().as_encoded_bytes())
}
}
#[cfg(all(feature = "std", not(target_family = "wasm")))]
impl From<StdPathBuf> for PlatformPathBuf {
fn from(std_path_buf: StdPathBuf) -> PlatformPathBuf {
PlatformPathBuf::from(std_path_buf.into_os_string().into_encoded_bytes())
}
} |
@encounter sure, seems like 1.74 came out nearly a year ago. With 1.58 being near 3 years old, I'm fine with doing a bump in Rust version requirement. Care to submit a PR or two to make the changes you want? I'll take a look this weekend. 😄 |
Will do! |
Checking if you're planning to send in a PR 😄 |
Closing out as resolved. Tried to remove the |
First of all, thanks for the library! It’s been very useful.
One problem I’ve run into is the way that NativePath is implemented. It’s very easy to write code that compiles for one platform but fails to compile on another. For example, this code builds on Unix platforms, but not Windows:
Another example:
It’s the only time I’ve had to install x86_64-windows-pc-gnu just to make sure the code compiles correctly on Windows. 🙂
Instead of an alias, I think NativePath should be a distinct type that requires an explicit conversion to and from UnixPath and WindowsPath. This guarantees at a type level that proper conversions are made when necessary.
What do you think?
The text was updated successfully, but these errors were encountered: