Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Improvements #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ impl Ec for EcFile {
}

fn version(&mut self) -> String {
let mut version = unsafe { self.get_str(b"VER:") };
while version.chars().next() == Some(' ') {
version.remove(0);
}
version
let version = unsafe { self.get_str(b"VER:") };
String::from(version.trim_start_matches(' '))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allocates twice, compared to the original code which only allocates a string once, then shifts the bytes around as necessary.

}
}
28 changes: 12 additions & 16 deletions src/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ impl EcFlash {
timeout -= 1;
}

if timeout == 0 {
Err(())
} else {
Ok(())
match timeout {
0 => Err(()),
_ => Ok(()),
}
}

Expand All @@ -41,10 +40,9 @@ impl EcFlash {
timeout -= 1;
}

if timeout == 0 {
Err(())
} else {
Ok(())
match timeout {
0 => Err(()),
_ => Ok(()),
}
}

Expand All @@ -55,10 +53,9 @@ impl EcFlash {
i -= 1;
}

if i == 0 {
Err(())
} else {
Ok(())
match i {
0 => Err(()),
_ => Ok(()),
}
}

Expand Down Expand Up @@ -138,10 +135,9 @@ impl EcFlash {
return Err(format!("Unknown EC ID: 0x{:>04X}", id));
}

let (data_port, cmd_port) = if primary {
(0x62, 0x66)
} else {
(0x68, 0x6c)
let (data_port, cmd_port) = match primary {
true => (0x62, 0x66),
false => (0x68, 0x6c),
};

let ec = Self {
Expand Down
32 changes: 16 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate ecflash;
use std::{env, process};
use std::fmt::Display;
use std::fs::File;
use std::io::{stdout, stderr, BufWriter, Error, Read, Write};
use std::io::{stdout, BufWriter, Error, Read, Write};

use ecflash::{Ec, EcFile, EcFlash};

Expand All @@ -14,7 +14,7 @@ fn validate<T: PartialEq + Display, F: FnMut() -> T>(mut f: F, attempts: usize)
if a == b {
return Ok(a);
} else {
let _ = writeln!(stderr(), "Attempt {}: {} != {}", _attempt_i, a, b);
eprintln!("Attempt {}: {} != {}", _attempt_i, a, b);
}
}
Err(())
Expand All @@ -28,12 +28,12 @@ fn main() {
// Get I/O Permission
unsafe {
if iopl(3) < 0 {
let _ = writeln!(stderr(), "Failed to get I/O permission: {}", Error::last_os_error());
eprintln!("Failed to get I/O permission: {}", Error::last_os_error());
process::exit(1);
}
}

let mut ecs: Vec<(String, Box<Ec>)> = Vec::new();
let mut ecs: Vec<(String, Box<dyn Ec>)> = Vec::new();

for arg in env::args().skip(1) {
match arg.as_str() {
Expand All @@ -42,7 +42,7 @@ fn main() {
ecs.push((String::new(), Box::new(ec_flash)));
},
Err(err) => {
let _ = writeln!(stderr(), "Failed to open EC flash 1: {}", err);
eprintln!("Failed to open EC flash 1: {}", err);
process::exit(1);
}
},
Expand All @@ -51,7 +51,7 @@ fn main() {
ecs.push((String::new(), Box::new(ec_flash)));
},
Err(err) => {
let _ = writeln!(stderr(), "Failed to open EC flash 2: {}", err);
eprintln!("Failed to open EC flash 2: {}", err);
process::exit(1);
}
},
Expand All @@ -61,13 +61,13 @@ fn main() {
match ec_file.read_to_end(&mut data) {
Ok(_) => ecs.push((arg, Box::new(EcFile::new(data)))),
Err(err) => {
let _ = writeln!(stderr(), "Failed to read EC file '{}': {}", arg, err);
eprintln!("Failed to read EC file '{}': {}", arg, err);
process::exit(1);
}
}
},
Err(err) => {
let _ = writeln!(stderr(), "Failed to open EC file '{}': {}", arg, err);
eprintln!("Failed to open EC file '{}': {}", arg, err);
process::exit(1);
}
}
Expand All @@ -78,37 +78,37 @@ fn main() {

for (name, mut ec) in ecs {
if name.is_empty() {
let _ = writeln!(stdout, "EC Flash");
println!("EC Flash");
} else {
let _ = writeln!(stdout, "EC File {}:", name);
println!("EC File {}:", name);
}

match validate(|| ec.project(), 8) {
Ok(project) => {
let _ = writeln!(stdout, " Project: {}", project);
println!(" Project: {}", project);
},
Err(()) => {
let _ = writeln!(stderr(), "Failed to read EC project");
eprintln!("Failed to read EC project");
process::exit(1);
}
}

match validate(|| ec.version(), 8) {
Ok(version) => {
let _ = writeln!(stdout, " Version: {}", version);
println!(" Version: {}", version);
},
Err(()) => {
let _ = writeln!(stderr(), "Failed to read EC version");
eprintln!("Failed to read EC version");
process::exit(1);
}
}

match validate(|| ec.size(), 8) {
Ok(size) => {
let _ = writeln!(stdout, " Size: {} KB", size/1024);
println!(" Size: {} KB", size/1024);
},
Err(()) => {
let _ = writeln!(stderr(), "Failed to read EC size");
eprintln!("Failed to read EC size");
process::exit(1);
}
}
Expand Down