-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Figure out a better way to accept both Path and String arguments #632
Comments
One option is to do away with |
This trait isn't really useful given we already have ToPath. It also makes it more difficult to choose between taking a Path, ToPath, or IntoPath as an argument type. See #632 for more details. Changelog: changed
4047c8b removes The decision to keep Whether this poses a risk is difficult to answer. Many file paths will be string literals, in which case there's no benefit to using What is clear is that this:
is more verbose compared to this:
|
We actually have a similar problem with methods such as |
Another potential challenge: if you accept a |
This problem/pattern also surfaces with the socket module: some methods take a I'm leaning more and more towards just expecting the final concrete types (e.g. |
Description
If a method wants to accept both a
Path
andString
, then turn those into aPath
, there are a few ways of doing this:Path
and leave the conversion up to the user, and take over ownershipref Path
and clone itIntoPath
and move it into aPath
ref ToPath
and create a newPath
from itOption 1 isn't great because it means the user has to convert the types every time, which is tedious.
Option 2 means that if I already have a
Path
and I'm OK with giving up ownership, I now run into a redundant clone. In addition, if I have aString
I have to first convert it.Option 3 allows for both
Path
andString
arguments, but if aPath
is given we'll take over ownership. This is annoying if the caller wants to reuse thePath
, as they now have to explicitly clone it first. In addition, I really want to get red ofInto*
traits, as we currently only have few legitimate use cases for them, and it's just confusing for users to determine if they should useToX
orIntoX
.Option 4 means always creating a new
Path
, which may be redundant if the caller doesn't make further use of its inputPath
.In its current setup, cloning a
Path
is cheap enough, as it's backed by aString
. This means usingref ToPath
would probably be sufficient, and allow us to get rid ofIntoPath
. However, if we change the internal implementation (see #314), then cloning aPath
may end up becoming more expensive, so I'm unsure as to how future-proof this approach is.Option 1 incurs the least amount of "type soup", as you just say "I need a
Path
" and leave the conversion up to the user. This might not be that bad given you can just dosome_string.to_path
, but it can get repetitive.Related work
No response
The text was updated successfully, but these errors were encountered: