binaryninja/
progress.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use std::ffi::c_void;

pub trait ProgressCallback: Sized {
    type SplitProgressType: SplitProgressBuilder;
    fn progress(&mut self, progress: usize, total: usize) -> bool;

    unsafe extern "C" fn cb_progress_callback(
        ctxt: *mut c_void,
        progress: usize,
        total: usize,
    ) -> bool {
        let ctxt: &mut Self = &mut *(ctxt as *mut Self);
        ctxt.progress(progress, total)
    }

    #[allow(clippy::wrong_self_convention)]
    unsafe fn into_raw(&mut self) -> *mut c_void {
        self as *mut Self as *mut c_void
    }

    /// Split a single progress function into proportionally sized subparts.
    /// This function takes the original progress function and returns a new function whose signature
    /// is the same but whose output is shortened to correspond to the specified subparts.
    ///
    /// The length of a subpart is proportional to the sum of all the weights.
    /// E.g. with `subpart_weights = &[ 25, 50, 25 ]`, this will return a function that calls
    /// progress_func and maps its progress to the ranges `[0..=25, 25..=75, 75..=100]`
    ///
    /// Weights of subparts, described above
    ///
    /// * `progress_func` - Original progress function (usually updates a UI)
    /// * `subpart_weights` - Weights of subparts, described above
    fn split(self, subpart_weights: &'static [usize]) -> Self::SplitProgressType;
}

pub trait SplitProgressBuilder {
    type Progress<'a>: ProgressCallback
    where
        Self: 'a;
    fn next_subpart(&mut self) -> Option<Self::Progress<'_>>;
}

impl<F> ProgressCallback for F
where
    F: FnMut(usize, usize) -> bool,
{
    type SplitProgressType = SplitProgress<F>;

    fn progress(&mut self, progress: usize, total: usize) -> bool {
        self(progress, total)
    }

    fn split(self, subpart_weights: &'static [usize]) -> Self::SplitProgressType {
        SplitProgress::new(self, subpart_weights)
    }
}

pub struct NoProgressCallback;

impl ProgressCallback for NoProgressCallback {
    type SplitProgressType = SplitProgressNop;

    fn progress(&mut self, _progress: usize, _total: usize) -> bool {
        unreachable!()
    }

    unsafe extern "C" fn cb_progress_callback(
        _ctxt: *mut c_void,
        _progress: usize,
        _total: usize,
    ) -> bool {
        true
    }

    fn split(self, subpart_weights: &'static [usize]) -> Self::SplitProgressType {
        SplitProgressNop(subpart_weights.len())
    }
}

pub struct SplitProgressNop(usize);

impl SplitProgressBuilder for SplitProgressNop {
    type Progress<'a> = NoProgressCallback;

    fn next_subpart(&mut self) -> Option<Self::Progress<'_>> {
        if self.0 == 0 {
            return None;
        }
        self.0 -= 1;
        Some(NoProgressCallback)
    }
}

pub struct SplitProgress<P> {
    callback: P,
    subpart_weights: &'static [usize],
    total: usize,
    progress: usize,
}

impl<P: ProgressCallback> SplitProgress<P> {
    pub fn new(callback: P, subpart_weights: &'static [usize]) -> Self {
        let total = subpart_weights.iter().sum();
        Self {
            callback,
            subpart_weights,
            total,
            progress: 0,
        }
    }

    pub fn next_subpart(&mut self) -> Option<SplitProgressInstance<'_, P>> {
        if self.subpart_weights.is_empty() {
            return None;
        }
        Some(SplitProgressInstance { progress: self })
    }
}

impl<P: ProgressCallback> SplitProgressBuilder for SplitProgress<P> {
    type Progress<'a>
        = SplitProgressInstance<'a, P>
    where
        Self: 'a;
    fn next_subpart(&mut self) -> Option<Self::Progress<'_>> {
        self.next_subpart()
    }
}

pub struct SplitProgressInstance<'a, P: ProgressCallback> {
    progress: &'a mut SplitProgress<P>,
}

impl<P: ProgressCallback> Drop for SplitProgressInstance<'_, P> {
    fn drop(&mut self) {
        self.progress.progress += self.progress.subpart_weights[0];
        self.progress.subpart_weights = &self.progress.subpart_weights[1..];
    }
}

impl<P: ProgressCallback> ProgressCallback for SplitProgressInstance<'_, P> {
    type SplitProgressType = SplitProgress<Self>;

    fn progress(&mut self, progress: usize, total: usize) -> bool {
        let subpart_progress = (self.progress.subpart_weights[0] * progress) / total;
        let progress = self.progress.progress + subpart_progress;
        self.progress
            .callback
            .progress(progress, self.progress.total)
    }

    fn split(self, subpart_weights: &'static [usize]) -> Self::SplitProgressType {
        SplitProgress::new(self, subpart_weights)
    }
}

#[cfg(test)]
mod test {
    use std::cell::Cell;

    use super::*;

    #[test]
    fn progress_simple() {
        let progress = Cell::new(0);
        let mut callback = |p, _| {
            progress.set(p);
            true
        };
        callback.progress(0, 100);
        assert_eq!(progress.get(), 0);
        callback.progress(1, 100);
        assert_eq!(progress.get(), 1);
        callback.progress(50, 100);
        assert_eq!(progress.get(), 50);
        callback.progress(99, 100);
        assert_eq!(progress.get(), 99);
        callback.progress(100, 100);
        assert_eq!(progress.get(), 100);
    }

    #[test]
    fn progress_simple_split() {
        let progress = Cell::new(0);
        let callback = |p, _| {
            progress.set(p);
            true
        };
        let mut split = callback.split(&[25, 50, 25]);
        // 0..=25
        let mut split_instance = split.next_subpart().unwrap();
        split_instance.progress(0, 100);
        assert_eq!(progress.get(), 0);
        split_instance.progress(100, 100);
        assert_eq!(progress.get(), 25);
        drop(split_instance);

        // 25..=75
        let mut split_instance = split.next_subpart().unwrap();
        split_instance.progress(0, 100);
        assert_eq!(progress.get(), 25);
        split_instance.progress(25, 100);
        // there is no way to check for exact values, it depends on how the calculation is done,
        // at the time or writing of this test is always round down, but we just check a range because this
        // could change
        assert!((36..=37).contains(&progress.get()));
        split_instance.progress(50, 100);
        assert_eq!(progress.get(), 50);
        split_instance.progress(100, 100);
        assert_eq!(progress.get(), 75);
        drop(split_instance);

        // 75..=100
        let mut split_instance = split.next_subpart().unwrap();
        split_instance.progress(0, 100);
        assert_eq!(progress.get(), 75);
        split_instance.progress(100, 100);
        assert_eq!(progress.get(), 100);
        drop(split_instance);

        assert!(split.next_subpart().is_none());
    }

    #[test]
    fn progress_recursive_split() {
        let progress = Cell::new(0);
        let callback = |p, _| {
            progress.set(p);
            true
        };
        let mut split = callback.split(&[25, 50, 25]);
        // 0..=25
        let mut split_instance = split.next_subpart().unwrap();
        split_instance.progress(0, 100);
        assert_eq!(progress.get(), 0);
        split_instance.progress(100, 100);
        assert_eq!(progress.get(), 25);
        drop(split_instance);

        // 25..=75, will get split into two parts: 25..=50 and 50..=75
        {
            let split_instance = split.next_subpart().unwrap();
            let mut sub_split = split_instance.split(&[50, 50]);
            // 25..=50
            let mut sub_split_instance = sub_split.next_subpart().unwrap();
            sub_split_instance.progress(0, 100);
            assert_eq!(progress.get(), 25);
            sub_split_instance.progress(100, 100);
            assert_eq!(progress.get(), 50);
            drop(sub_split_instance);

            // 50..=75
            let mut sub_split_instance = sub_split.next_subpart().unwrap();
            sub_split_instance.progress(0, 100);
            assert_eq!(progress.get(), 50);
            sub_split_instance.progress(100, 100);
            assert_eq!(progress.get(), 75);
            drop(sub_split_instance);
        }

        // 75..=100
        let mut split_instance = split.next_subpart().unwrap();
        split_instance.progress(0, 100);
        assert_eq!(progress.get(), 75);
        split_instance.progress(100, 100);
        assert_eq!(progress.get(), 100);
        drop(split_instance);

        assert!(split.next_subpart().is_none());
    }
}