From bc25542d98db9af1dc80a67de4853ee3ead80f36 Mon Sep 17 00:00:00 2001 From: lindon <53193460+LindonKelley@users.noreply.github.com> Date: Tue, 8 Nov 2022 09:03:20 -0600 Subject: [PATCH] changed Memory::init to utilize Vec::resize rather than manually growing the vector --- src/memory.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/memory.rs b/src/memory.rs index b9e7469..d7a4dcd 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,3 +1,5 @@ +use std::convert::TryInto; + /// Emulates main memory. pub struct Memory { /// Memory content @@ -18,11 +20,15 @@ impl Memory { /// # Arguments /// * `capacity` pub fn init(&mut self, capacity: u64) { - for _i in 0..((capacity + 7) / 8) { - self.data.push(0); - } + // casting may fail on 32-bit targets, and an error here is more useful than one in a VM + // expecting more memory than it has + self.data.resize( + ((capacity + 7) / 8).try_into() + .expect("unable to allocate, usize cannot handle the required capacity"), + 0 + ); } - + /// Reads a byte from memory. /// /// # Arguments