-
Notifications
You must be signed in to change notification settings - Fork 28
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
New API: borrow TrashItem
s
#65
Conversation
* take `IntoIterator<Item = &TrashItem>` as parameter * `RestoreCollision { path: PathBuf, restored: usize }` * `RestoreTwins(PathBuf)`
* use `thiserror` * remove `FileSystem`, add `Io`
Thank you, the linux implementation looks like a reasonable improvement, I think as discussed prior in #54. Adding If @ArturKovacs thinks it makes sense as well maybe we can see this through. |
One more feature I forgot to explain:
|
I decide to contains the path where the I/O error occurred in |
TrashItem
s and nicer ErrorTrashItem
s
It's been a while I wrote code with this crate, and I haven't run into the problem discussed here, but if I understand correctly, then this PR isn't the best solution for the issue. I don't think there's a need for the change introduced in this PR, I think you can just use the current API to solve this problem. For example this code will re-try the restoration, but during the second try, it skips the trash item that couldn't be restored. (Note that I haven't tested this code and it will attempt to restore all your trashed files.) let items = trash::list().unwrap();
if let Err(RestoreCollision {path, remaining_items}) = trash::os_limited::restore_all(items) {
// keep all except the one that couldn't be restored.
remaining_items.retain(|e| e.original_path() != path);
trash::os_limited::restore_all(remaining_items)
} |
Borrow references of let items = trash::list().unwrap();
if let Err(RestoreCollision { path, restored }) = trash::os_limited::restore_all(&items) {
// keep all except the one that couldn't be restored.
let items = items
.iter()
// you can skip restored + 1 in this show snippet, that's OK
.skip(restored)
.filter(|e| e.original_path() != path);
trash::os_limited::restore_all();
} Now the problem is, there are no contracts about it, in other words, there is a contract tells users shouldn't depend on the order:
I wonder why. |
Thanks for chiming in @ArturKovacs and providing an example. @TD-Sky Performance isn't ever an issue until it is measured, so I don't think borrowing for that reason carries a lot of value. what do you think about making this PR mergeable only by adding a non-running documentation example instead, based on @ArturKovacs example? It would show how to retry without breaking the API. |
Are there any other ways to implement #54? |
Seeing how resolving this has come along, I think it's worth it to consider cutting losses and rejecting the premise. Even though reference could be used, they aren't, and cloning the keep them isn't known to be a performance issue. There might be error cases where the input items are lost, and one might consider to 'return' the owned items in the error similar to how it was already demonstrated. This seems to be in the interest of the original author and wouldn't break the API for most, and I see value in that. |
So you thought it better to pass in references and return owned items when error occurs? I have to apologize for my potential misunderstanding, because I'm not a native English speaker. |
The original author indicated that references and changing the API there isn't needed to solve the problem. Now that we have direction, I hope this PR can get the work it needs to be merged. |
I wanna close this PR and open a new one because the correct direction disagrees the topic of this PR. |
Wait, can we make |
From what I can tell, the whole premise of "References are needed" is not only put into question but rejected. It would be a breaking change and despite being more idiomatic to use references if no consumption happens internally, changing it now half-way wouldn't change much effectively except breaking the consistency of the crate API, which overall is consuming in nature. It seems to be in the interest of the @ArturKovacs to leave it as is, and I suggest to look at specific problems with the API and add documentation and examples to show how these can be solved. |
But |
Great idea - if |
This PR brings the following new features:
restore_all
andpurge_all
take&TrashItem
as parameters instead of taking ownership. It helps us avoid cloningTrashItem
s when we want to keep the ownership of them after restoring or purging.Correspondingly, the
Error::RestoreCollision
now contains fieldrestored: usize
which means how many items are restored before collision. Anyone who wants to continue restoring can skiprestored + 1
items on the passed iterator.thiserror
now. The reason for usingthiserror
is there are too many I/O Result in the codes.#[error(transparent)] Io(#[from] io::Error)
makes the error handling more convenient and reasonable.Sadly, this PR only works on Linux. I can't setup a virtual machine of other platforms temporarily. If this PR is verified to be sensible, I wish someone could complete the work of other platforms.