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

Interopting with std::path::Path #28

Open
jabuwu opened this issue Jul 15, 2024 · 3 comments
Open

Interopting with std::path::Path #28

jabuwu opened this issue Jul 15, 2024 · 3 comments

Comments

@jabuwu
Copy link

jabuwu commented Jul 15, 2024

Hey there, excellent library. I'm wondering if there is a recommended way to convert back and forth between the std::path::Path and std::path::PathBuf types.

It's nice to store types as Utf8NativePath, because that has an AsRef<std::path::Path>. That type can easily be passed into other libraries that expect the std path types.

However, it seems quite cumbersome to create that from an std path:

let native_path = Utf8NativePath::from_bytes_path(NativePath::new(
    std_path.as_os_str().as_encoded_bytes(),
))
.expect("Path did not contain valid UTF-8 characters");

It seems to me that NativePath should have a From<std::path::Path>, since from_bytes_path is is infallable, and Utf8NativePath should have a fallable TryFrom<std::path::Path> with Utf8Error.

The library does an amazing job documenting and interopting with itself, but much of the Rust ecosystem uses std path, so I find this library hard to work with practically. Perhaps I missed something obvious?

Thanks for your time!

@chipsenkbeil
Copy link
Owner

Hey there! Thanks for reaching out :)

I took a look and the as_encoded_bytes is a newer function introduced in Rust 1.74.0. Before then, there was not a cross-platform, safe way to convert to a byte slice IIRC. Conversions weren't seamless because of the inability to convert in one direction, limited by Windows' wide word string implementation.

Been awhile since I took a look, so maybe there's a cleaner way to do this now. :)

@jabuwu
Copy link
Author

jabuwu commented Jul 16, 2024

Oh, I see. Indeed, as_encoded_bytes seems to do the trick. The code I posted works on Windows and Mac, according to my testing.

I'm happy to make a PR for this if I find the time. Wanted to make sure I wasn't missing something.

@chipsenkbeil
Copy link
Owner

One thing to keep in mind is that as_encoded_bytes exports a platform-dependent byte slice. If you want to share your path across machines over the network, it isn't a good idea to use it.

For my personal use case, I leverage this library specifically to have a platform-agnostic way to share paths over a network; so, if this library ends up leveraging the conversion to-and-from encoded bytes, there will need to be a disclaimer since it would cause issues for those like me that didn't realize this constraint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants