fs-err icon indicating copy to clipboard operation
fs-err copied to clipboard

path type different than std::fs::File

Open dbregman opened this issue 4 years ago • 5 comments

std::fs::File uses AsRef<Path> as the type for passing paths while fs_err uses Into<PathBuf>

This makes fs_err not be a "drop in replacement": When I pass an AsRef<Path> to fs_err::File::open, it does not compile

dbregman avatar Jan 31 '21 08:01 dbregman

This is true. Maybe we should qualify this statement with an asterisk and explain how we differ?

Alternatively, maybe we should change our bounds to either P: AsRef<Path> or P: AsRef<Path> + Into<PathBuf>. The latter would let us avoid an allocation but doesn't really solve this issue.

LPGhatguy avatar May 24 '21 22:05 LPGhatguy

I also hit this, and had to rework some code. I'm guessing the error types need to own the path and that's why this is done?

kellpossible avatar Aug 30 '21 15:08 kellpossible

Is it really necessary that the error should own the path? Can't it just store the reference to the path instead?

TonalidadeHidrica avatar Nov 18 '21 14:11 TonalidadeHidrica

@TonalidadeHidrica The error must own the path, it cannot be a reference because that would make both the wrapper and the error non-'static. Requiring Into<PathBuf> rather than AsRef<Path> is basically an optimization, allowing fs_err to avoid an allocation when you pass an actual PathBuf.

This issue should probably be resolved by a PR that improves the docs to explain this, and show how to change the code that doesn't compile due to the difference. (The change is trivial, but can be perplexing to a newbie.)

hniksic avatar Jun 08 '23 08:06 hniksic

Its worth noting that all uses of Into<PathBuf> are for constructing an fs_err::File, which owns a PathBuf even in the happy path. All the other functions were changed to AsRef<Path> in #31 and will allocate only in the failure path.

I'd be happy to accept a PR to improve docs around this

andrewhickman avatar Jun 12 '23 19:06 andrewhickman