Skip to content

Commit b5d8a1a

Browse files
committed
Bakground::run can take self by value
1 parent 9b3c399 commit b5d8a1a

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/background.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crossbeam_channel::{Receiver, RecvTimeoutError, TryRecvError};
2-
use std::panic::{catch_unwind, RefUnwindSafe};
2+
use std::panic::{catch_unwind, UnwindSafe};
33
use std::sync::atomic::{AtomicBool, Ordering};
44
use std::sync::Arc;
55
use std::sync::Mutex;
@@ -98,7 +98,7 @@ pub struct BackgroundHandle<T, S> {
9898
impl<T, S> BackgroundHandle<T, S> {
9999
pub fn spawn<K>(task: K) -> BackgroundHandle<T, S>
100100
where
101-
K: Background<Output = T, Status = S> + RefUnwindSafe + Send + Sync + 'static,
101+
K: Background<Output = T, Status = S> + UnwindSafe + Send + Sync + 'static,
102102
T: Send + Sync + 'static,
103103
S: Send + Sync + Clone + 'static,
104104
{
@@ -107,7 +107,7 @@ impl<T, S> BackgroundHandle<T, S> {
107107
let inner_control = control.clone();
108108

109109
let handle = thread::spawn(move || {
110-
let response = catch_unwind(|| task.run(&inner_control));
110+
let response = catch_unwind(move || task.run(&inner_control));
111111
let _ = tx.send(response);
112112
});
113113

@@ -180,7 +180,7 @@ pub trait Background: Send + Sync {
180180
type Output: Send + Sync;
181181
type Status: Send + Sync;
182182

183-
fn run(&self, control: &ControlToken<Self::Status>) -> Self::Output;
183+
fn run(self, control: &ControlToken<Self::Status>) -> Self::Output;
184184
}
185185

186186
#[cfg(test)]
@@ -195,7 +195,7 @@ mod tests {
195195
type Output = Result<u32, u32>;
196196
type Status = u32;
197197

198-
fn run(&self, control: &ControlToken<Self::Status>) -> Self::Output {
198+
fn run(self, control: &ControlToken<Self::Status>) -> Self::Output {
199199
let mut ticks = 0;
200200

201201
while ticks < 100 && !control.is_cancelled_with_pause() {

src/compression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Background for BackgroundCompactor {
6161
type Output = ();
6262
type Status = ();
6363

64-
fn run(&self, control: &ControlToken<Self::Status>) -> Self::Output {
64+
fn run(self, control: &ControlToken<Self::Status>) -> Self::Output {
6565
for file in &self.files_in {
6666
if control.is_cancelled_with_pause() {
6767
break;

src/folder.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::VecDeque;
22
use std::os::windows::fs::MetadataExt;
33
use std::path::{Path, PathBuf};
4-
use std::sync::Mutex;
54
use std::time::{Duration, Instant};
65

76
use filesize::file_real_size;
@@ -154,14 +153,14 @@ impl GroupInfo {
154153
#[derive(Debug)]
155154
pub struct FolderScan {
156155
path: PathBuf,
157-
excludes: Mutex<GlobSet>,
156+
excludes: GlobSet,
158157
}
159158

160159
impl FolderScan {
161160
pub fn new<P: AsRef<Path>>(path: P, excludes: GlobSet) -> Self {
162161
Self {
163162
path: path.as_ref().to_path_buf(),
164-
excludes: Mutex::new(excludes),
163+
excludes,
165164
}
166165
}
167166
}
@@ -170,9 +169,9 @@ impl Background for FolderScan {
170169
type Output = Result<FolderInfo, FolderInfo>;
171170
type Status = (PathBuf, FolderSummary);
172171

173-
fn run(&self, control: &ControlToken<Self::Status>) -> Self::Output {
174-
let mut ds = FolderInfo::new(&self.path);
175-
let excludes = self.excludes.lock().expect("exclude lock");
172+
fn run(self, control: &ControlToken<Self::Status>) -> Self::Output {
173+
let FolderScan { path, excludes } = self;
174+
let mut ds = FolderInfo::new(&path);
176175
let incompressible = pathdb();
177176
let mut incompressible = incompressible.write().unwrap();
178177
let _ = incompressible.load();
@@ -186,7 +185,7 @@ impl Background for FolderScan {
186185
// 4. Grab metadata - should be infallible on Windows, it comes with the
187186
// DirEntry.
188187
// 5. GetCompressedFileSizeW() or skip.
189-
let walker = WalkDir::new(&self.path)
188+
let walker = WalkDir::new(&path)
190189
.into_iter()
191190
.filter_entry(|e| e.file_type().is_file() || !excludes.is_match(e.path()))
192191
.filter_map(|e| e.map_err(|e| eprintln!("Error: {:?}", e)).ok())
@@ -198,7 +197,7 @@ impl Background for FolderScan {
198197
for (count, (entry, metadata, physical)) in walker {
199198
let shortname = entry
200199
.path()
201-
.strip_prefix(&self.path)
200+
.strip_prefix(&path)
202201
.unwrap_or_else(|_e| entry.path())
203202
.to_path_buf();
204203

0 commit comments

Comments
 (0)