From d623b34f7fd52af1245ce92b1328494a0c15df22 Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 28 Dec 2020 00:19:50 +0000 Subject: [PATCH] work r/w loop okey --- src/main.rs | 2 ++ src/work.rs | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2f363e6..2061037 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ mod work; mod consts; +pub const BUFSIZE: usize = 4096; // 4kb + fn main() { println!("Hello, world!"); } diff --git a/src/work.rs b/src/work.rs index aae3cb0..cb1cd5f 100644 --- a/src/work.rs +++ b/src/work.rs @@ -2,12 +2,19 @@ use super::*; use job::Job; use std::io::Read; -pub fn work_on(job: &mut Job) -> Result<(), Box> +pub fn work_on(job: &mut Job) -> Result> { let output = job.output_slice(); - let mut buf = [0u8; 4096]; - while let Ok(read) = job.read(&mut buf) { - //TODO: copy from `job`'s file into `output`. + let output = unsafe { + // Required because the compiler thinks we're mutably borrowing the same data later on, even though they are unrelated fields of the struct + std::slice::from_raw_parts_mut(output.as_mut_ptr(), output.len()) + }; + let mut read=0; + loop { + match job.read(&mut output[read..])? { + 0 => break, + current => read+=current, + } } - todo!() + Ok(read) }