diff --git a/ic10emu/build.rs b/ic10emu/build.rs index cbc70ee..f24e9e0 100644 --- a/ic10emu/build.rs +++ b/ic10emu/build.rs @@ -68,13 +68,13 @@ fn write_repr_enum( } else { "".to_string() }; - write!( + writeln!( writer, - " #[strum({serialize_str}{props_str})] {variant_name},\n" + " #[strum({serialize_str}{props_str})] {variant_name}," ) .unwrap(); } - write!(writer, "}}\n").unwrap(); + writeln!(writer, "}}").unwrap(); } fn write_logictypes() { @@ -192,9 +192,8 @@ fn write_enums() { let e_contents = fs::read_to_string(e_infile).unwrap(); for line in e_contents.lines().filter(|l| !l.trim().is_empty()) { - let mut it = line.splitn(2, ' '); - let name = it.next().unwrap(); - let val_str = it.next().unwrap(); + let (name, val_str) = line.split_once(' ').unwrap(); + let val: Option = val_str.parse().ok(); if !check_set.contains(name) { @@ -206,9 +205,9 @@ fn write_enums() { } } - write!( + writeln!( &mut writer, - "pub(crate) const ENUM_LOOKUP: phf::Map<&'static str, u8> = {};\n", + "pub(crate) const ENUM_LOOKUP: phf::Map<&'static str, u8> = {};", enums_lookup_map_builder.build() ) .unwrap(); @@ -327,9 +326,9 @@ fn write_constants() { constants_lookup_map_builder.entry(name, constant); } - write!( + writeln!( &mut writer, - "pub(crate) const CONSTANTS_LOOKUP: phf::Map<&'static str, f64> = {};\n", + "pub(crate) const CONSTANTS_LOOKUP: phf::Map<&'static str, f64> = {};", constants_lookup_map_builder.build() ) .unwrap(); @@ -361,12 +360,12 @@ fn write_instructions_enum() { ) .unwrap(); - write!(&mut writer, " Nop,\n").unwrap(); + writeln!(&mut writer, " Nop,").unwrap(); for typ in &instructions { - write!(&mut writer, " {},\n", typ.to_case(Case::Pascal)).unwrap(); + writeln!(&mut writer, " {},", typ.to_case(Case::Pascal)).unwrap(); } - write!(&mut writer, "}}\n").unwrap(); + writeln!(&mut writer, "}}").unwrap(); write!( &mut writer, @@ -380,7 +379,7 @@ fn write_instructions_enum() { for typ in &instructions { let name = typ.to_case(Case::Pascal); - write!(&mut writer, " \"{typ}\" => Ok(Self::{name}),\n").unwrap(); + writeln!(&mut writer, " \"{typ}\" => Ok(Self::{name}),").unwrap(); } write!( &mut writer, diff --git a/ic10emu/src/grammar.rs b/ic10emu/src/grammar.rs index f22d5c1..cca7dac 100644 --- a/ic10emu/src/grammar.rs +++ b/ic10emu/src/grammar.rs @@ -9,6 +9,7 @@ use strum::EnumProperty; pub mod generated { use super::ParseError; use crate::interpreter::ICError; + use serde::{Deserialize, Serialize}; use std::str::FromStr; use strum::AsRefStr; use strum::Display; @@ -16,7 +17,6 @@ pub mod generated { use strum::EnumProperty; use strum::EnumString; use strum::IntoEnumIterator; - use serde::{Deserialize, Serialize}; include!(concat!(env!("OUT_DIR"), "/instructions.rs")); include!(concat!(env!("OUT_DIR"), "/logictypes.rs")); @@ -156,10 +156,10 @@ pub struct Line { impl FromStr for Line { type Err = ParseError; fn from_str(s: &str) -> Result { - let mut parts = s.splitn(2, "#"); + let mut parts = s.splitn(2, '#'); let code = parts .next() - .map(|s| { + .and_then(|s| { let s = s.trim_end(); if s.is_empty() { None @@ -167,7 +167,6 @@ impl FromStr for Line { Some(s.parse::()) } }) - .flatten() .transpose()?; let comment = parts.next().map(|s| s.parse()).transpose()?; Ok(Line { code, comment }) @@ -231,11 +230,11 @@ impl FromStr for Instruction { line: 0, start: 0, end: 0, - msg: format!("Missing instruction"), + msg: "Missing instruction".to_string(), }) } }?; - + let operands = get_operand_tokens(s, tokens_iter) .iter() .map(|(index, token)| { @@ -251,7 +250,10 @@ impl FromStr for Instruction { } } -fn get_operand_tokens<'a>(s: &'a str, tokens_iter: SplitConsecutiveWithIndices<'a>) -> Vec<(usize, &'a str)> { +fn get_operand_tokens<'a>( + s: &'a str, + tokens_iter: SplitConsecutiveWithIndices<'a>, +) -> Vec<(usize, &'a str)> { let mut operand_tokens = Vec::with_capacity(8); let mut string_start = None; for (index, token) in tokens_iter { @@ -298,29 +300,29 @@ pub enum Operand { impl Operand { pub fn get_value(&self, ic: &interpreter::IC) -> Result { match &self { - &Operand::RegisterSpec { + Operand::RegisterSpec { indirection, target, } => ic.get_register(*indirection, *target), - &Operand::Number(num) => Ok(num.value()), - &Operand::LogicType(lt) => lt + Operand::Number(num) => Ok(num.value()), + Operand::LogicType(lt) => lt .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::SlotLogicType(slt) => slt + Operand::SlotLogicType(slt) => slt .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::BatchMode(bm) => bm + Operand::BatchMode(bm) => bm .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::ReagentMode(rm) => rm + Operand::ReagentMode(rm) => rm .get_str("value") .map(|val| val.parse::().unwrap() as f64) .ok_or(interpreter::ICError::TypeValueNotKnown), - &Operand::Identifier(ident) => ic.get_ident_value(&ident.name), - &Operand::DeviceSpec { .. } => Err(interpreter::ICError::DeviceNotValue), + Operand::Identifier(ident) => ic.get_ident_value(&ident.name), + Operand::DeviceSpec { .. } => Err(interpreter::ICError::DeviceNotValue), } } @@ -330,9 +332,9 @@ impl Operand { signed: bool, ) -> Result { let val = self.get_value(ic)?; - if val < -9.223372036854776E+18 { + if val < -9.223_372_036_854_776E18 { Err(interpreter::ICError::ShiftUnderflowI64) - } else if val <= 9.223372036854776E+18 { + } else if val <= 9.223_372_036_854_776E18 { Ok(interpreter::f64_to_i64(val, signed)) } else { Err(interpreter::ICError::ShiftOverflowI64) @@ -355,7 +357,7 @@ impl Operand { ic: &interpreter::IC, ) -> Result<(Option, Option), interpreter::ICError> { match &self { - &Operand::DeviceSpec { device, connection } => match device { + Operand::DeviceSpec { device, connection } => match device { Device::Db => Ok((Some(ic.device), *connection)), Device::Numbered(p) => { let dp = ic @@ -378,7 +380,7 @@ impl Operand { Ok((dp, *connection)) } }, - &Operand::Identifier(id) => ic.get_ident_device_id(&id.name), + Operand::Identifier(id) => ic.get_ident_device_id(&id.name), _ => Err(interpreter::ICError::ValueNotDevice), } } @@ -402,7 +404,7 @@ impl FromStr for Operand { let mut rest_iter = rest.iter(); let indirection = rest_iter.take_while_ref(|c| *c == &'r').count(); let target = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::() .parse::() .ok(); @@ -419,7 +421,7 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid register specifier"), + msg: "Invalid register specifier".to_string(), }) } ['d', rest @ ..] => match rest { @@ -428,7 +430,7 @@ impl FromStr for Operand { connection: None, }), ['b', ':', chan @ ..] => { - if chan.into_iter().all(|c| c.is_digit(10)) { + if chan.iter().all(|c| c.is_ascii_digit()) { Ok(Operand::DeviceSpec { device: Device::Db, connection: Some(String::from_iter(chan).parse().unwrap()), @@ -438,15 +440,15 @@ impl FromStr for Operand { line: 0, start: 3, end: 3, - msg: format!("Invalid device connection specifier"), + msg: "Invalid device connection specifier".to_string(), }) } } ['r', rest @ ..] => { - let mut rest_iter = rest.into_iter().peekable(); + let mut rest_iter = rest.iter().peekable(); let indirection = rest_iter.take_while_ref(|c| *c == &'r').count(); let target_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let target = target_str.parse::().ok(); let connection = { @@ -454,11 +456,11 @@ impl FromStr for Operand { // take off ':' rest_iter.next(); let connection_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let connection = connection_str.parse::().unwrap(); let trailing = rest_iter.clone().collect::>(); - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Some(connection)) } else { let start = @@ -467,7 +469,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid device connection specifier"), + msg: "Invalid device connection specifier".to_string(), }) } } else { @@ -476,30 +478,30 @@ impl FromStr for Operand { }?; let trailing = rest_iter.collect::>(); if let Some(target) = target { - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Operand::DeviceSpec { device: Device::Indirect { indirection: indirection as u32, target, }, - connection: connection, + connection, }) } else { Err(ParseError { line: 0, start: 0, end: 0, - msg: format!("Invalid register specifier"), + msg: "Invalid register specifier".to_string(), }) } } else { Ok(Operand::Identifier(s.parse::()?)) } } - [rest @ ..] => { - let mut rest_iter = rest.into_iter().peekable(); + rest => { + let mut rest_iter = rest.iter().peekable(); let target_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let target = target_str.parse::().ok(); let connection = { @@ -507,11 +509,11 @@ impl FromStr for Operand { // take off ':' rest_iter.next(); let connection_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); let connection = connection_str.parse::().unwrap(); let trailing = rest_iter.clone().collect::>(); - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Some(connection)) } else { let start = 1 + target_str.len() + 1 + connection_str.len(); @@ -519,7 +521,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid device connection specifier"), + msg: "Invalid device connection specifier".to_string(), }) } } else { @@ -528,17 +530,17 @@ impl FromStr for Operand { }?; let trailing = rest_iter.collect::>(); if let Some(target) = target { - if trailing.len() == 0 { + if trailing.is_empty() { Ok(Operand::DeviceSpec { device: Device::Numbered(target), - connection: connection, + connection, }) } else { Err(ParseError { line: 0, start: 0, end: 0, - msg: format!("Invalid device specifier"), + msg: "Invalid device specifier".to_string(), }) } } else { @@ -554,14 +556,14 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid hash string: Can not contain '\"'"), + msg: "Invalid hash string: Can not contain '\"'".to_string(), }) } } ['$', rest @ ..] => { - let mut rest_iter = rest.into_iter(); + let mut rest_iter = rest.iter(); let num_str = rest_iter - .take_while_ref(|c| c.is_digit(16)) + .take_while_ref(|c| c.is_ascii_hexdigit()) .collect::(); let num = i64::from_str_radix(&num_str, 16).unwrap() as f64; let trailing = rest_iter.count(); @@ -572,12 +574,12 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid Hexadecimal Number"), + msg: "Invalid Hexadecimal Number".to_string(), }) } } ['%', rest @ ..] => { - let mut rest_iter = rest.into_iter(); + let mut rest_iter = rest.iter(); let num_str = rest_iter .take_while_ref(|c| c.is_digit(2)) .collect::(); @@ -590,24 +592,24 @@ impl FromStr for Operand { line: 0, start: 0, end: 0, - msg: format!("Invalid Binary Number"), + msg: "Invalid Binary Number".to_string(), }) } } - [rest @ ..] => { - let mut rest_iter = rest.into_iter().peekable(); + rest => { + let mut rest_iter = rest.iter().peekable(); let float_str = if rest_iter.peek() == Some(&&'-') { format!("{}", rest_iter.next().unwrap()) } else { "".to_string() } + &rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); if !float_str.is_empty() { if rest_iter.peek() == Some(&&'.') { rest_iter.next(); let decimal_str = rest_iter - .take_while_ref(|c| c.is_digit(10)) + .take_while_ref(|c| c.is_ascii_digit()) .collect::(); if !decimal_str.is_empty() { let float_str = float_str + "." + &decimal_str; @@ -619,7 +621,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid Decimal Number"), + msg: "Invalid Decimal Number".to_string(), }) } } else { @@ -633,7 +635,7 @@ impl FromStr for Operand { line: 0, start, end: start, - msg: format!("Invalid Integer Number"), + msg: "Invalid Integer Number".to_string(), }) } } @@ -703,10 +705,7 @@ impl FromStr for Identifier { fn from_str(s: &str) -> Result { let mut iter = s.chars(); if let Some(c) = iter.next() { - if match c { - 'a'..='z' | 'A'..='Z' | '_' | '.' => true, - _ => false, - } { + if matches!(c, 'a'..='z' | 'A'..='Z' | '_' | '.') { for (index, cc) in iter.enumerate() { match cc { 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' => continue, @@ -734,7 +733,7 @@ impl FromStr for Identifier { line: 0, start: 0, end: 0, - msg: format!("Empty Identifier"), + msg: "Empty Identifier".to_string(), }) } } diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index 1b2ca0c..8f07c8e 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -127,7 +127,7 @@ impl Display for ICState { ICState::Yield => "Ic has yielded, Resume on next tick".to_string(), ICState::Sleep(then, sleep_for) => { let format = format_description::parse("[hour]:[minute]:[second]").unwrap(); - let resume = then.clone() + time::Duration::new(*sleep_for as i64, 0); + let resume = *then + time::Duration::new(*sleep_for as i64, 0); format!( "Sleeping for {sleep_for} seconds, will resume at {}", resume.format(&format).unwrap() @@ -169,7 +169,6 @@ impl Default for Program { } impl Program { - pub fn new() -> Self { Program { instructions: Vec::new(), @@ -178,7 +177,7 @@ impl Program { } pub fn try_from_code(code: &str) -> Result { - let parse_tree = grammar::parse(&code)?; + let parse_tree = grammar::parse(code)?; let mut labels_set = HashSet::new(); let mut labels = HashMap::new(); let instructions = parse_tree @@ -220,7 +219,6 @@ impl Program { } impl IC { - pub fn new(id: u16, device: u16) -> Self { IC { device, @@ -352,7 +350,7 @@ impl IC { pub fn poke(&mut self, address: f64, val: f64) -> Result { let sp = address as i32; - if sp < 0 || sp >= 512 { + if !(0..512).contains(&sp) { Err(ICError::StackIndexOutOfRange(address)) } else { let last = self.stack[sp as usize]; @@ -426,7 +424,7 @@ impl IC { }), }, // TODO Yield => { - if operands.len() != 0 { + if !operands.is_empty() { Err(TooManyOperands { provided: operands.len() as u32, desired: 0, @@ -479,14 +477,14 @@ impl IC { }); }; let alias = match &device_reg { - &Operand::RegisterSpec { + Operand::RegisterSpec { indirection, target, } => Operand::RegisterSpec { indirection: *indirection, target: *target, }, - &Operand::DeviceSpec { device, connection } => Operand::DeviceSpec { + Operand::DeviceSpec { device, connection } => Operand::DeviceSpec { device: *device, connection: *connection, }, @@ -3110,7 +3108,7 @@ impl IC { Some(device) => match device.borrow().ic.as_ref() { Some(ic_id) => { let addr = addr.get_value(self)?; - let ic = vm.ics.get(&ic_id).unwrap().borrow(); + let ic = vm.ics.get(ic_id).unwrap().borrow(); let val = ic.peek_addr(addr)?; self.set_register(indirection, target, val)?; Ok(()) @@ -3149,7 +3147,7 @@ impl IC { Some(device) => match device.borrow().ic.as_ref() { Some(ic_id) => { let addr = addr.get_value(self)?; - let ic = vm.ics.get(&ic_id).unwrap().borrow(); + let ic = vm.ics.get(ic_id).unwrap().borrow(); let val = ic.peek_addr(addr)?; self.set_register(indirection, target, val)?; Ok(()) @@ -3173,13 +3171,13 @@ impl IC { let (Some(device_id), _connection) = dev_id.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => match device.borrow().ic.as_ref() { Some(ic_id) => { let addr = addr.get_value(self)?; let val = val.get_value(self)?; - let mut ic = vm.ics.get(&ic_id).unwrap().borrow_mut(); + let mut ic = vm.ics.get(ic_id).unwrap().borrow_mut(); ic.poke(addr, val)?; Ok(()) } @@ -3209,7 +3207,7 @@ impl IC { Some(ic_id) => { let addr = addr.get_value(self)?; let val = val.get_value(self)?; - let mut ic = vm.ics.get(&ic_id).unwrap().borrow_mut(); + let mut ic = vm.ics.get(ic_id).unwrap().borrow_mut(); ic.poke(addr, val)?; Ok(()) } @@ -3240,14 +3238,14 @@ impl IC { break 'inst Err(MissingConnecitonSpecifier); }; let network_id = vm - .get_device_same_network(self.device, device_id as u16) + .get_device_same_network(self.device, device_id) .map(|device| device.borrow().get_network_id(connection as usize)) .unwrap_or(Err(UnknownDeviceID(device_id as f64)))?; let val = val.get_value(self)?; vm.set_network_channel(network_id as usize, channel, val)?; return Ok(()); } - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let val = val.get_value(self)?; @@ -3280,7 +3278,7 @@ impl IC { device.borrow_mut().set_field(lt, val)?; Ok(()) } - None => Err(UnknownDeviceID(device_id as f64)), + None => Err(UnknownDeviceID(device_id)), } } oprs => Err(TooManyOperands { @@ -3297,7 +3295,7 @@ impl IC { let (Some(device_id), _connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let index = index.get_value(self)?; @@ -3394,14 +3392,14 @@ impl IC { break 'inst Err(MissingConnecitonSpecifier); }; let network_id = vm - .get_device_same_network(self.device, device_id as u16) + .get_device_same_network(self.device, device_id) .map(|device| device.borrow().get_network_id(connection as usize)) .unwrap_or(Err(UnknownDeviceID(device_id as f64)))?; let val = vm.get_network_channel(network_id as usize, channel)?; self.set_register(indirection, target, val)?; return Ok(()); } - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let val = device.borrow().get_field(lt)?; @@ -3444,7 +3442,7 @@ impl IC { self.set_register(indirection, target, val)?; Ok(()) } - None => Err(UnknownDeviceID(device_id as f64)), + None => Err(UnknownDeviceID(device_id)), } } oprs => Err(TooManyOperands { @@ -3471,7 +3469,7 @@ impl IC { let (Some(device_id), _connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let index = index.get_value(self)?; @@ -3507,7 +3505,7 @@ impl IC { let (Some(device_id), _connection) = dev.get_device_id(self)? else { break 'inst Err(DeviceNotSet); }; - let device = vm.get_device_same_network(self.device, device_id as u16); + let device = vm.get_device_same_network(self.device, device_id); match device { Some(device) => { let rm = ReagentMode::try_from(rm.get_value(self)?)?; diff --git a/ic10emu/src/lib.rs b/ic10emu/src/lib.rs index 45389e3..b82e089 100644 --- a/ic10emu/src/lib.rs +++ b/ic10emu/src/lib.rs @@ -76,17 +76,11 @@ pub struct Network { pub channels: [f64; 8], } -#[derive(Debug)] +#[derive(Debug, Default)] struct IdSequenceGenerator { next: u16, } -impl Default for IdSequenceGenerator { - fn default() -> Self { - IdSequenceGenerator { next: 0 } - } -} - impl IdSequenceGenerator { pub fn next(&mut self) -> u16 { let val = self.next; @@ -201,16 +195,14 @@ impl Device { pub fn get_network_id(&self, connection: usize) -> Result { if connection >= 8 { Err(ICError::ConnecitonIndexOutOFRange(connection as u32)) - } else { - if let Connection::CableNetwork(network_id) = self.connections[connection] { - if let Some(network_id) = network_id { - Ok(network_id) - } else { - Err(ICError::NetworkNotConnected(connection as u32)) - } + } else if let Connection::CableNetwork(network_id) = self.connections[connection] { + if let Some(network_id) = network_id { + Ok(network_id) } else { - Err(ICError::NotDataConnection(connection as u32)) + Err(ICError::NetworkNotConnected(connection as u32)) } + } else { + Err(ICError::NotDataConnection(connection as u32)) } } @@ -291,6 +283,12 @@ impl Device { } } +impl Default for VM { + fn default() -> Self { + Self::new() + } +} + impl VM { pub fn new() -> Self { let id_gen = IdSequenceGenerator::default(); @@ -656,17 +654,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device.borrow_mut().get_field(typ).map(|val| Some(val)) + device.borrow_mut().get_field(typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), @@ -706,17 +701,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device.borrow().get_field(typ).map(|val| Some(val)) + device.borrow().get_field(typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), @@ -757,20 +749,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device - .borrow() - .get_slot_field(index, typ) - .map(|val| Some(val)) + device.borrow().get_slot_field(index, typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), @@ -809,20 +795,14 @@ impl VM { .iter() .any(|(_net_id, net)| net.borrow().contains(&[source, *id])) { - device - .borrow() - .get_slot_field(index, typ) - .map(|val| Some(val)) + device.borrow().get_slot_field(index, typ).map(Some) } else { Ok(None) } }) .collect::, ICError>>()? .into_iter() - .filter_map(|val| { - val.map(|val| if val.is_nan() { None } else { Some(val) }) - .flatten() - }) + .filter_map(|val| val.and_then(|val| if val.is_nan() { None } else { Some(val) })) .collect_vec(); match mode { BatchMode::Sum => Ok(samples.iter().sum()), diff --git a/ic10emu/src/rand_mscorlib.rs b/ic10emu/src/rand_mscorlib.rs index e449969..ac887aa 100644 --- a/ic10emu/src/rand_mscorlib.rs +++ b/ic10emu/src/rand_mscorlib.rs @@ -73,8 +73,7 @@ impl Random { inextp = 1; } - let mut retval = - self.seed_array[inext as usize].wrapping_sub(self.seed_array[inextp as usize]); + let mut retval = self.seed_array[inext].wrapping_sub(self.seed_array[inextp]); if retval == i32::MAX { retval -= 1; @@ -82,7 +81,7 @@ impl Random { if retval < 0 { retval = retval.wrapping_add(i32::MAX); } - self.seed_array[inext as usize] = retval; + self.seed_array[inext] = retval; self.inext = inext; self.inextp = inextp; diff --git a/ic10emu/src/tokens.rs b/ic10emu/src/tokens.rs index b33cb22..7db62b1 100644 --- a/ic10emu/src/tokens.rs +++ b/ic10emu/src/tokens.rs @@ -21,7 +21,7 @@ impl<'a> Iterator for SplitConsecutiveWithIndices<'a> { + start + 'find_end: { let mut last = start; - for (index, c) in (&tail[start..]).chars().enumerate() { + for (index, c) in tail[start..].chars().enumerate() { if !self.chars.contains(&c) { break 'find_end index; } @@ -71,7 +71,7 @@ pub trait SplitConsecutiveIndicesExt: ) -> SplitConsecutiveWithIndices<'p> { SplitConsecutiveWithIndices { haystack: &self[..], - chars: chars, + chars, start: 0, } }