-
Notifications
You must be signed in to change notification settings - Fork 12
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
Inherent methods for PathOps and PathInfo #48
Comments
I'm curious: is it implemented for The reason we moved these to traits is so that these methods could be used on |
It does the same as It has been around in my crate since before I started using Currently, the trait permeates a great portion of my codebase and I would not look forward to determining whether each use site needs use std::path::Path;
use std::path::PathBuf;
/// AsRef<Path> with more general impls on smart pointer types.
///
/// (for instance, `Box<AsPath>` and `Rc<TempDir>` both implement the trait)
pub trait AsPath {
fn as_path(&self) -> &Path;
fn to_path_buf(&self) -> PathBuf
{ self.as_path().to_path_buf() }
fn join(&self, path: impl AsPath) -> PathBuf
where Self: Sized, // generic functions are not object-safe
{ self.as_path().join(path.as_path()) }
}
macro_rules! as_path_impl {
---- (snip) ----
}
as_path_impl!{
(by AsRef) [] std::path::Path;
(by Deref) [] std::path::PathBuf;
(by AsRef) [] rsp2_fs_util::ActualTempDir;
(by AsRef) [] rsp2_fs_util::TempDir;
(by AsRef) [] std::ffi::OsString;
(by AsRef) [] std::ffi::OsStr;
(by AsRef) [] str;
(by AsRef) [] String;
(by AsRef) ['a] std::path::Iter<'a>;
(by AsRef) ['a] std::path::Components<'a>;
(by Deref) ['p, P: AsPath + ?Sized] &'p mut P;
(by Deref) [P: AsPath + ?Sized] Box<P>;
(by Deref) [P: AsPath + ?Sized] std::rc::Rc<P>;
(by Deref) [P: AsPath + ?Sized] std::sync::Arc<P>;
(by Deref) ['p, P: AsPath + ToOwned + ?Sized] std::borrow::Cow<'p, P>;
(by AsRef) [] path_abs::PathAbs;
(by AsRef) [] path_abs::PathFile;
(by AsRef) [] path_abs::PathDir;
}
impl<'p, P: AsPath + ?Sized> AsPath for &'p P
{ fn as_path(&self) -> &Path { P::as_path(self) } } |
Just so I understand the ask, are you proposing:
If it is the second, is there precedence for that in other libraries? I empathize with the breaking changes causing problems. I also empathize that there is no way to implement PathInfo etc for types you don't own. When working with Personally I don't think it makes much sense to implement the traits for |
Option 2. The trouble with finding existing examples is that if a library does it right, you don't notice. I opened this thread: https://users.rust-lang.org/t/crates-that-use-the-inherent-trait-method-pattern/30381
|
But you can already use it for the RHS, since https://github.com/vitiral/path_abs/blob/master/tests/test_absolute.rs#L49 |
Yes, granted, the RHS of My crate no longer needs the |
It feels extremely silly to have to import traits to get methods that could sensibly be provided on the types themselves, especially traits that have so many methods (
PathInfo
), and especially when I have no intention to use the traits generically. (I already have my own trait with a method namedjoin
, and importing yours would cause a conflict)Most notably:
{PathAbs,PathDir}::{join,concat}
{PathAbs,PathFile}::{file_name,with_file_name,with_extension}
(and maybe stuff for PathDir; though the termbase_name
might be cause less confusion thanfile_name
)PathAbs::{exists,is_file,is_dir,parent...}
... in fact, nearly all of thePath
methods are useful forPathAbs
, making it really hard for me to see why you removedimpl Deref for PathAbs
. MaybeTarget=PathArc
was too much, but isn't at leastTarget=Path
reasonable?The text was updated successfully, but these errors were encountered: