Commit 79643b8b authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Add PartialOrd and Ord to ByteUnits

parent cc072f2e
...@@ -4,6 +4,7 @@ version = "0.1.0" ...@@ -4,6 +4,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"threadpool 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
...@@ -17,6 +18,19 @@ name = "lazy_static" ...@@ -17,6 +18,19 @@ name = "lazy_static"
version = "0.2.4" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rand"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "threadpool" name = "threadpool"
version = "1.3.2" version = "1.3.2"
...@@ -25,4 +39,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" ...@@ -25,4 +39,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6" "checksum ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "23ac7c30002a5accbf7e8987d0632fa6de155b7c3d39d0067317a391e00a2ef6"
"checksum lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7291b1dd97d331f752620b02dfdbc231df7fc01bf282a00769e1cdb963c460dc" "checksum lazy_static 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7291b1dd97d331f752620b02dfdbc231df7fc01bf282a00769e1cdb963c460dc"
"checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum threadpool 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "59f6d3eff89920113dac9db44dde461d71d01e88a5b57b258a0466c32b5d7fe1" "checksum threadpool 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "59f6d3eff89920113dac9db44dde461d71d01e88a5b57b258a0466c32b5d7fe1"
...@@ -7,3 +7,4 @@ authors = ["stefschi"] ...@@ -7,3 +7,4 @@ authors = ["stefschi"]
ansi_term = "0.9" ansi_term = "0.9"
lazy_static = "0.2" lazy_static = "0.2"
threadpool = "1.0" threadpool = "1.0"
rand = "0.3"
extern crate ansi_term; extern crate ansi_term;
#[macro_use] extern crate lazy_static; #[macro_use] extern crate lazy_static;
extern crate threadpool; extern crate threadpool;
extern crate rand;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
...@@ -18,6 +19,7 @@ lazy_static! { ...@@ -18,6 +19,7 @@ lazy_static! {
static ref HEADING: Style = Yellow.bold(); static ref HEADING: Style = Yellow.bold();
} }
const WRITE_BUFFER_SIZE: usize = 4096;
fn main() { fn main() {
let model = get_file_contents("/sys/devices/virtual/dmi/id/product_version"); let model = get_file_contents("/sys/devices/virtual/dmi/id/product_version");
...@@ -119,13 +121,22 @@ struct DiskInformation { ...@@ -119,13 +121,22 @@ struct DiskInformation {
state: Mutex<DiskState>, state: Mutex<DiskState>,
} }
#[derive(Debug,Eq)] #[derive(Debug,Eq,Ord)]
enum ByteUnit { enum ByteUnit {
Byte(u64), Byte(u64),
KiloByte(u64), KiloByte(u64),
} }
use ByteUnit::*; use ByteUnit::*;
impl ByteUnit {
pub fn to_byte(&self) -> ByteUnit {
match self {
&Byte(v) => Byte(v),
&KiloByte(v) => Byte(1024 * v),
}
}
}
impl std::cmp::PartialEq<Self> for ByteUnit { impl std::cmp::PartialEq<Self> for ByteUnit {
fn eq(&self, rhs: &Self) -> bool { fn eq(&self, rhs: &Self) -> bool {
match self { match self {
...@@ -140,6 +151,23 @@ impl std::cmp::PartialEq<Self> for ByteUnit { ...@@ -140,6 +151,23 @@ impl std::cmp::PartialEq<Self> for ByteUnit {
} }
} }
} }
impl std::cmp::PartialOrd<Self> for ByteUnit {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(match self {
&Byte(l) => match other {
&Byte(r) => l.cmp(&r),
ref something_else => {
if let Byte(r) = something_else.to_byte() {
l.cmp(&r)
} else {
panic!("fn partial_cmp<ByteUnit>: this must never happen!");
}
},
},
ref something_else => something_else.to_byte().cmp(other),
})
}
}
#[derive(Debug,PartialEq,Eq)] #[derive(Debug,PartialEq,Eq)]
...@@ -235,10 +263,17 @@ fn update_jobs(disks: &Vec<Arc<DiskInformation>>, pool: &threadpool::ThreadPool) ...@@ -235,10 +263,17 @@ fn update_jobs(disks: &Vec<Arc<DiskInformation>>, pool: &threadpool::ThreadPool)
println!("Queued {} for whipe", BOLD.paint(&*disk.name)); println!("Queued {} for whipe", BOLD.paint(&*disk.name));
let disk = disk.clone(); let disk = disk.clone();
pool.execute(move || { pool.execute(move || {
let mut rng = rand::thread_rng();
*disk.state.lock().unwrap() = Whiping{ progress: Byte(0), per_second: Byte(0) }; *disk.state.lock().unwrap() = Whiping{ progress: Byte(0), per_second: Byte(0) };
let buf = [0; 4096]; let mut buf = [0; WRITE_BUFFER_SIZE];
let mut position = Byte(0);
/*
while position < disk.size {
buf = rng.get();
position += WRITE_BUFFER_SIZE as u64;
}*/
}) })
} }
} }
...@@ -278,4 +313,33 @@ mod test { ...@@ -278,4 +313,33 @@ mod test {
fn format_terrabyte() { fn format_terrabyte() {
assert_eq!(" 42.00 TB", format!("{}", KiloByte(42 * 1024 * 1024 * 1024))); assert_eq!(" 42.00 TB", format!("{}", KiloByte(42 * 1024 * 1024 * 1024)));
} }
#[test]
fn ord_eqals() {
let one_k_bytes = Byte(1024);
let one_kilobyte = KiloByte(1);
assert!(one_k_bytes <= one_kilobyte);
assert!(one_kilobyte <= one_k_bytes);
assert!(one_k_bytes >= one_kilobyte);
assert!(one_kilobyte >= one_k_bytes);
}
#[test]
fn ord_less() {
let two_k_bytes = Byte(2048);
let one_kilobyte = KiloByte(1);
assert!(two_k_bytes > one_kilobyte);
assert!(one_kilobyte < two_k_bytes);
}
#[test]
fn ord_more() {
let one_k_bytes = Byte(1024);
let two_kilobytes = KiloByte(2);
assert!(two_kilobytes > one_k_bytes);
assert!(one_k_bytes < two_kilobytes);
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment