1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 23:40:05 +08:00

user refcell

This commit is contained in:
2019-08-03 17:22:26 +08:00
parent a5f07d2b14
commit a67337bb5d

View File

@@ -1,6 +1,7 @@
extern crate term;
use std::{
cell::RefCell,
env,
fs,
io::{self, Write, Error, ErrorKind},
@@ -240,12 +241,11 @@ pub fn extract_package_and_wait(dir: &str, file_name: &str) -> io::Result<()> {
pub fn copy_io<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W, total: i64) -> io::Result<u64>
where R: io::Read, W: io::Write {
// let mut written = 0u64;
// let written_cell = RefCell::new(written);
let written_cell = RefCell::new(0u64);
let start = SystemTime::now();
let written = copy_io_callback(reader, writer, total, &|total, written, _len| {
// let mut written_borrowed = written_cell.borrow_mut();
//written_cell.replace_with(|&mut old_written| old_written + len as u64);
let written = copy_io_callback(reader, writer, total, &|total, _written, len| {
written_cell.replace_with(|&mut w| w + len as u64);
let written = *written_cell.borrow();
let cost = SystemTime::now().duration_since(start.clone()).unwrap().as_secs();
let mut download_speed = "-".to_string();
if cost > 0 {