Skip to content

Commit 67a7225

Browse files
authored
Merge pull request #30 from Dr-Emann/misc
Cargo Fmt, more Crossbeam channel, and Less Mutex
2 parents 3ff543f + b5d8a1a commit 67a7225

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/background.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::panic::{catch_unwind, RefUnwindSafe};
1+
use crossbeam_channel::{Receiver, RecvTimeoutError, TryRecvError};
2+
use std::panic::{catch_unwind, UnwindSafe};
23
use std::sync::atomic::{AtomicBool, Ordering};
3-
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, TryRecvError};
44
use std::sync::Arc;
55
use std::sync::Mutex;
66
use std::thread;
@@ -98,16 +98,16 @@ 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
{
105-
let (tx, rx) = mpsc::channel();
105+
let (tx, rx) = crossbeam_channel::unbounded();
106106
let control = ControlToken::new();
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/compact.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ pub fn compress_file<P: AsRef<Path>>(path: P, compression: Compression) -> std::
274274
compress_file_handle(&file, compression)
275275
}
276276

277-
pub fn compress_file_handle(file: &std::fs::File, compression: Compression) -> std::io::Result<bool> {
277+
pub fn compress_file_handle(
278+
file: &std::fs::File,
279+
compression: Compression,
280+
) -> std::io::Result<bool> {
278281
const LEN: usize = std::mem::size_of::<_WOF_EXTERNAL_INFO>()
279282
+ std::mem::size_of::<_FILE_PROVIDER_EXTERNAL_INFO_V1>();
280283

src/compression.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::io;
2-
use std::path::PathBuf;
32
use std::os::windows::fs::OpenOptionsExt;
3+
use std::path::PathBuf;
44

55
use compresstimator::Compresstimator;
66
use crossbeam_channel::{Receiver, Sender};
77
use filetime::FileTime;
8-
use winapi::um::winnt::{FILE_WRITE_ATTRIBUTES, FILE_READ_DATA};
8+
use winapi::um::winnt::{FILE_READ_DATA, FILE_WRITE_ATTRIBUTES};
99

1010
use crate::background::Background;
1111
use crate::background::ControlToken;
@@ -51,7 +51,7 @@ fn handle_file(file: &PathBuf, compression: Option<Compression>) -> io::Result<b
5151
let _ = filetime::set_file_handle_times(
5252
&handle,
5353
Some(FileTime::from_last_access_time(&meta)),
54-
Some(FileTime::from_last_modification_time(&meta))
54+
Some(FileTime::from_last_modification_time(&meta)),
5555
);
5656

5757
ret
@@ -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)