mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-06 04:00:02 +00:00
Refactor: Extract new crate simple-git
(#1304)
`binstalk-downloader` contains stuff about http(s) before the git code is moved into it and now it becomes http and git. While git indeed uses http stuff, which is why I decided to put it into binstalk-downloader, it is more than just downloading since it is stateful (can be cached locally and updated) where as http is stateless. Also `binstalk-downloader`'s codegen time now increases dramatically and it also creates extra dependencies for binstalk-fetchers, delaying its execution. The git code also don't use anything from `binstalk-downloader` at all, it makes sense to be an independent crate. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
20a57a9a9b
commit
dc77a1ab93
17 changed files with 258 additions and 24 deletions
44
crates/simple-git/src/cancellation_token.rs
Normal file
44
crates/simple-git/src/cancellation_token.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering::Relaxed},
|
||||
Arc,
|
||||
};
|
||||
|
||||
use derive_destructure2::destructure;
|
||||
|
||||
/// Token that can be used to cancel git operation.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct GitCancellationToken(Arc<AtomicBool>);
|
||||
|
||||
impl GitCancellationToken {
|
||||
/// Create a guard that cancel the git operation on drop.
|
||||
#[must_use = "You must assign the guard to a variable, \
|
||||
otherwise it is equivalent to `GitCancellationToken::cancel()`"]
|
||||
pub fn cancel_on_drop(self) -> GitCancelOnDrop {
|
||||
GitCancelOnDrop(self)
|
||||
}
|
||||
|
||||
/// Cancel the git operation.
|
||||
pub fn cancel(&self) {
|
||||
self.0.store(true, Relaxed)
|
||||
}
|
||||
|
||||
pub(super) fn get_atomic(&self) -> &AtomicBool {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Guard used to cancel git operation on drop
|
||||
#[derive(Debug, destructure)]
|
||||
pub struct GitCancelOnDrop(GitCancellationToken);
|
||||
|
||||
impl Drop for GitCancelOnDrop {
|
||||
fn drop(&mut self) {
|
||||
self.0.cancel()
|
||||
}
|
||||
}
|
||||
impl GitCancelOnDrop {
|
||||
/// Disarm the guard, return the token.
|
||||
pub fn disarm(self) -> GitCancellationToken {
|
||||
self.destructure().0
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue