1 use std::marker;
2 
3 use crate::raw;
4 use crate::util::Binding;
5 
6 /// Struct representing the progress by an in-flight transfer.
7 pub struct Progress<'a> {
8     pub(crate) raw: ProgressState,
9     pub(crate) _marker: marker::PhantomData<&'a raw::git_indexer_progress>,
10 }
11 
12 pub(crate) enum ProgressState {
13     Borrowed(*const raw::git_indexer_progress),
14     Owned(raw::git_indexer_progress),
15 }
16 
17 /// Callback to be invoked while indexing is in progress.
18 ///
19 /// This callback will be periodically called with updates to the progress of
20 /// the indexing so far. The return value indicates whether the indexing or
21 /// transfer should continue. A return value of `false` will cancel the
22 /// indexing or transfer.
23 ///
24 /// * `progress` - the progress being made so far.
25 pub type IndexerProgress<'a> = dyn FnMut(Progress<'_>) -> bool + 'a;
26 
27 impl<'a> Progress<'a> {
28     /// Number of objects in the packfile being downloaded
total_objects(&self) -> usize29     pub fn total_objects(&self) -> usize {
30         unsafe { (*self.raw()).total_objects as usize }
31     }
32     /// Received objects that have been hashed
indexed_objects(&self) -> usize33     pub fn indexed_objects(&self) -> usize {
34         unsafe { (*self.raw()).indexed_objects as usize }
35     }
36     /// Objects which have been downloaded
received_objects(&self) -> usize37     pub fn received_objects(&self) -> usize {
38         unsafe { (*self.raw()).received_objects as usize }
39     }
40     /// Locally-available objects that have been injected in order to fix a thin
41     /// pack.
local_objects(&self) -> usize42     pub fn local_objects(&self) -> usize {
43         unsafe { (*self.raw()).local_objects as usize }
44     }
45     /// Number of deltas in the packfile being downloaded
total_deltas(&self) -> usize46     pub fn total_deltas(&self) -> usize {
47         unsafe { (*self.raw()).total_deltas as usize }
48     }
49     /// Received deltas that have been hashed.
indexed_deltas(&self) -> usize50     pub fn indexed_deltas(&self) -> usize {
51         unsafe { (*self.raw()).indexed_deltas as usize }
52     }
53     /// Size of the packfile received up to now
received_bytes(&self) -> usize54     pub fn received_bytes(&self) -> usize {
55         unsafe { (*self.raw()).received_bytes as usize }
56     }
57 
58     /// Convert this to an owned version of `Progress`.
to_owned(&self) -> Progress<'static>59     pub fn to_owned(&self) -> Progress<'static> {
60         Progress {
61             raw: ProgressState::Owned(unsafe { *self.raw() }),
62             _marker: marker::PhantomData,
63         }
64     }
65 }
66 
67 impl<'a> Binding for Progress<'a> {
68     type Raw = *const raw::git_indexer_progress;
from_raw(raw: *const raw::git_indexer_progress) -> Progress<'a>69     unsafe fn from_raw(raw: *const raw::git_indexer_progress) -> Progress<'a> {
70         Progress {
71             raw: ProgressState::Borrowed(raw),
72             _marker: marker::PhantomData,
73         }
74     }
75 
raw(&self) -> *const raw::git_indexer_progress76     fn raw(&self) -> *const raw::git_indexer_progress {
77         match self.raw {
78             ProgressState::Borrowed(raw) => raw,
79             ProgressState::Owned(ref raw) => raw as *const _,
80         }
81     }
82 }
83 
84 /// Callback to be invoked while a transfer is in progress.
85 ///
86 /// This callback will be periodically called with updates to the progress of
87 /// the transfer so far. The return value indicates whether the transfer should
88 /// continue. A return value of `false` will cancel the transfer.
89 ///
90 /// * `progress` - the progress being made so far.
91 #[deprecated(
92     since = "0.11.0",
93     note = "renamed to `IndexerProgress` to match upstream"
94 )]
95 #[allow(dead_code)]
96 pub type TransportProgress<'a> = IndexerProgress<'a>;
97