Make clippy happy

This commit is contained in:
2019-04-01 12:51:40 +02:00
parent 8159943199
commit 4d618efe42
8 changed files with 54 additions and 41 deletions

2
Cargo.lock generated
View File

@@ -16,7 +16,7 @@ dependencies = [
[[package]] [[package]]
name = "rustijvm" name = "rustijvm"
version = "0.1.0" version = "1.0.0"
dependencies = [ dependencies = [
"serde 1.0.65 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.65 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.65 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.65 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@@ -2,12 +2,13 @@ use Result;
use block::Block; use block::Block;
use ops::{Operation,num_to_op}; use ops::{Operation,num_to_op};
#[allow(unknown_lints,len_without_is_empty)]
pub trait BinReadable { pub trait BinReadable {
fn get(&mut self) -> Result<u8>; fn get(&mut self) -> Result<u8>;
fn cur(&self) -> usize; fn cur(&self) -> usize;
fn len(&self) -> usize; fn len(&self) -> usize;
fn slice(&mut self, len: usize) -> &[u8]; fn slice(&mut self, len: usize) -> &[u8];
fn is_empty(&self) -> bool;
} }
pub trait BinRead { pub trait BinRead {

View File

@@ -78,4 +78,8 @@ impl BinReadable for Block {
self.pointer += len; self.pointer += len;
slice slice
} }
fn is_empty(&self) -> bool {
self.source.is_empty()
}
} }

View File

@@ -24,11 +24,11 @@ pub struct Instruction {
impl fmt::Display for Instruction { impl fmt::Display for Instruction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.label { if self.label {
write!(f, "\nLBL_{}:\n", self.pos)?; writeln!(f, "\nLBL_{}:", self.pos)?;
} }
if self.wide { if self.wide {
write!(f, " WIDE\n")?; writeln!(f, " WIDE")?;
} }
write!(f, " {}", self.name)?; write!(f, " {}", self.name)?;
@@ -48,7 +48,7 @@ impl fmt::Display for Instruction {
} }
} }
write!(f, "\n")?; writeln!(f)?;
Ok(()) Ok(())
} }
@@ -82,12 +82,12 @@ pub struct Method {
impl Method { impl Method {
pub fn new(i: i32, pos: usize, args: usize, vars: usize) -> Self { pub fn new(i: i32, pos: usize, args: usize, vars: usize) -> Self {
let name; let name = if pos == 0 {
if pos == 0 { "main".to_string()
name = "main".to_string();
} else { } else {
name = format!("func_{}", i); format!("func_{}", i)
} };
Self { Self {
name, name,
pos, pos,
@@ -129,10 +129,9 @@ impl Method {
impl fmt::Display for Method { impl fmt::Display for Method {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut main_var_off = 0; let main_var_off = if self.name == "main" {
if self.name == "main" { writeln!(f, ".main")?;
write!(f, ".main\n")?; 1
main_var_off = 1;
} else { } else {
write!(f, ".method {}(", self.name)?; write!(f, ".method {}(", self.name)?;
let mut comma = ""; let mut comma = "";
@@ -140,18 +139,19 @@ impl fmt::Display for Method {
write!(f, "{}var{}", comma, i)?; write!(f, "{}var{}", comma, i)?;
comma = ", "; comma = ", ";
} }
write!(f,")\n")?; writeln!(f,")")?;
} 0
};
if self.vars > 0 { if self.vars > 0 {
write!(f, "\n.var\n")?; writeln!(f, "\n.var")?;
for i in 1..=self.vars { for i in 1..=self.vars {
write!(f, " var{}\n", i + self.args - main_var_off)?; writeln!(f, " var{}", i + self.args - main_var_off)?;
} }
write!(f, ".end-var\n")?; writeln!(f, ".end-var")?;
} }
write!(f, "\n")?; writeln!(f)?;
for inst in &self.instructions { for inst in &self.instructions {
write!(f, "{}", inst)?; write!(f, "{}", inst)?;
@@ -159,9 +159,9 @@ impl fmt::Display for Method {
if self.name == "main" { if self.name == "main" {
write!(f, "\n.end-main\n\n")?; writeln!(f, "\n.end-main\n")?;
} else { } else {
write!(f, "\n.end-method\n\n")?; writeln!(f, "\n.end-method\n")?;
} }
Ok(()) Ok(())
@@ -214,25 +214,25 @@ impl Disassembler {
for arg in args.clone() { for arg in args.clone() {
let v = match arg { let v = match arg {
ops::Args::Byte => self.text.read_i8()? as i32, ops::Args::Byte => i32::from(self.text.read_i8()?),
ops::Args::Short => self.text.read_i16()? as i32, ops::Args::Short => i32::from(self.text.read_i16()?),
ops::Args::Var => { ops::Args::Var => {
if wide { if wide {
self.text.read_u16()? as i32 i32::from(self.text.read_u16()?)
} else { } else {
self.text.read_u8()? as i32 i32::from(self.text.read_u8()?)
} }
}, },
ops::Args::Label => { ops::Args::Label => {
let offset = self.text.read_i16()?; let offset = self.text.read_i16()?;
let target = pos as i64 + offset as i64; let target = pos as i64 + i64::from(offset);
if target < 0 { if target < 0 {
return Err("Invalid jump offset"); return Err("Invalid jump offset");
} }
method.labels.push(target as usize); method.labels.push(target as usize);
target as i32 target as i32
}, },
ops::Args::Constant => self.text.read_u16()? as i32, ops::Args::Constant => i32::from(self.text.read_u16()?),
}; };
params.push(v); params.push(v);
} }
@@ -372,14 +372,14 @@ impl Disassembly {
impl fmt::Display for Disassembly { impl fmt::Display for Disassembly {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.constants.len() > 0 { if self.constants.is_empty() {
write!(f, ".constant\n")?; writeln!(f, ".constant")?;
for (i, c) in self.constants.iter().enumerate() { for (i, c) in self.constants.iter().enumerate() {
if !c.method { if !c.method {
write!(f, " CONST_{:<6} {}\n", i, c.value)?; writeln!(f, " CONST_{:<6} {}", i, c.value)?;
} }
} }
write!(f, ".end-constant\n\n")?; writeln!(f, ".end-constant\n")?;
} }
for method in &self.methods { for method in &self.methods {

View File

@@ -1,4 +1,4 @@
#[derive(Debug)] #[derive(Debug,Default)]
pub struct Heap { pub struct Heap {
pub heaps: Vec<Vec<i32>> pub heaps: Vec<Vec<i32>>
} }
@@ -22,4 +22,8 @@ impl Heap {
self.heaps.push(vec![0; size]); self.heaps.push(vec![0; size]);
self.len() - 1 self.len() - 1
} }
pub fn is_empty(&self) -> bool {
self.heaps.is_empty()
}
} }

View File

@@ -52,4 +52,8 @@ impl BinReadable for IJVMReader {
self.pointer += len; self.pointer += len;
slice slice
} }
fn is_empty(&self) -> bool {
self.source.is_empty()
}
} }

View File

@@ -155,17 +155,17 @@ impl Machine {
for arg in &args { for arg in &args {
let v = match arg { let v = match arg {
Args::Byte => self.block.read_i8()? as i32, Args::Byte => i32::from(self.block.read_i8()?),
Args::Short => self.block.read_i16()? as i32, Args::Short => i32::from(self.block.read_i16()?),
Args::Var => { Args::Var => {
if wide { if wide {
self.block.read_u16()? as i32 i32::from(self.block.read_u16()?)
} else { } else {
self.block.read_u8()? as i32 i32::from(self.block.read_u8()?)
} }
}, },
Args::Label => self.block.read_i16()? as i32, Args::Label => i32::from(self.block.read_i16()?),
Args::Constant => self.block.read_u16()? as i32, Args::Constant => i32::from(self.block.read_u16()?),
}; };
params.push(v); params.push(v);
} }

View File

@@ -4,7 +4,7 @@ use Result;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::net::TcpListener; use std::net::TcpListener;
#[derive(Debug)] #[derive(Debug, Default)]
pub struct NetStack { pub struct NetStack {
pub stream: Option<TcpStream> pub stream: Option<TcpStream>
} }
@@ -28,7 +28,7 @@ impl NetStack {
Ok(()) Ok(())
} }
Err(_) => Err("Could not accept connection") Err(_) => Err("Could not accept connection")
} }
} }
pub fn connect(&mut self, host: u32, port: u16) -> Result<()> { pub fn connect(&mut self, host: u32, port: u16) -> Result<()> {