refactor(downloads): ran cargo clippy & cargo fmt

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2024-11-11 18:27:39 +11:00
parent 17244496ec
commit 5e3d26b3ca
6 changed files with 23 additions and 20 deletions

View File

@ -132,9 +132,10 @@ impl GameDownloadAgent {
return chunk.lengths.iter().sum::<usize>(); return chunk.lengths.iter().sum::<usize>();
}) })
.sum::<usize>(); .sum::<usize>();
let chunk_count = manifest_download.iter().map(|(_, chunk)| { let chunk_count = manifest_download
chunk.lengths.len() .values()
}).sum(); .map(|chunk| chunk.lengths.len())
.sum();
self.progress = ProgressObject::new(length.try_into().unwrap(), chunk_count); self.progress = ProgressObject::new(length.try_into().unwrap(), chunk_count);
if let Ok(mut manifest) = self.manifest.lock() { if let Ok(mut manifest) = self.manifest.lock() {

View File

@ -75,13 +75,13 @@ impl DropDownloadPipeline<Response, File> {
progress: Arc<AtomicUsize>, progress: Arc<AtomicUsize>,
size: usize, size: usize,
) -> Self { ) -> Self {
return Self { Self {
source, source,
destination, destination,
control_flag, control_flag,
progress, progress,
size, size,
}; }
} }
fn copy(&mut self) -> Result<bool, io::Error> { fn copy(&mut self) -> Result<bool, io::Error> {
@ -99,10 +99,8 @@ impl DropDownloadPipeline<Response, File> {
current_size += bytes_read; current_size += bytes_read;
buf_writer.write_all(&copy_buf[0..bytes_read])?; buf_writer.write_all(&copy_buf[0..bytes_read])?;
self.progress.fetch_add( self.progress
bytes_read.try_into().unwrap(), .fetch_add(bytes_read, std::sync::atomic::Ordering::Relaxed);
std::sync::atomic::Ordering::Relaxed,
);
if current_size == self.size { if current_size == self.size {
break; break;
@ -114,7 +112,7 @@ impl DropDownloadPipeline<Response, File> {
fn finish(self) -> Result<Digest, io::Error> { fn finish(self) -> Result<Digest, io::Error> {
let checksum = self.destination.finish()?; let checksum = self.destination.finish()?;
return Ok(checksum); Ok(checksum)
} }
} }
@ -187,5 +185,5 @@ pub fn download_game_chunk(
return Err(GameDownloadError::ChecksumError); return Err(GameDownloadError::ChecksumError);
} }
return Ok(true); Ok(true)
} }

View File

@ -25,7 +25,6 @@ impl From<bool> for DownloadThreadControlFlag {
} }
} }
#[derive(Clone)] #[derive(Clone)]
pub struct DownloadThreadControl { pub struct DownloadThreadControl {
inner: Arc<AtomicBool>, inner: Arc<AtomicBool>,

View File

@ -1,4 +1,7 @@
use std::sync::{atomic::{AtomicUsize, Ordering}, Arc}; use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
#[derive(Clone)] #[derive(Clone)]
pub struct ProgressObject { pub struct ProgressObject {
@ -8,16 +11,17 @@ pub struct ProgressObject {
impl ProgressObject { impl ProgressObject {
pub fn new(max: usize, length: usize) -> Self { pub fn new(max: usize, length: usize) -> Self {
let arr = (0..length).map(|_| { Arc::new(AtomicUsize::new(0)) }).collect(); let arr = (0..length).map(|_| Arc::new(AtomicUsize::new(0))).collect();
Self { Self {
max, max,
progress_instances: Arc::new(arr) progress_instances: Arc::new(arr),
} }
} }
pub fn sum(&self) -> usize { pub fn sum(&self) -> usize {
self.progress_instances.iter().map(|instance| { self.progress_instances
instance.load(Ordering::Relaxed) .iter()
}).sum() .map(|instance| instance.load(Ordering::Relaxed))
.sum()
} }
pub fn get_progress(&self) -> f64 { pub fn get_progress(&self) -> f64 {

View File

@ -0,0 +1 @@