Skip to content

Commit 8402ec8

Browse files
committed
core: more simplifications
1 parent 5720de2 commit 8402ec8

File tree

2 files changed

+9
-23
lines changed

2 files changed

+9
-23
lines changed

crates/leanVm/src/core.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,7 @@ impl<PERM16, PERM24> VirtualMachine<PERM16, PERM24> {
275275
let ptr_shift_0_addr = (self.run_context.fp + shift_0)?;
276276

277277
// Read the pointer from memory. It must be a `MemoryAddress` type.
278-
let ptr: MemoryAddress = self
279-
.memory_manager
280-
.get(ptr_shift_0_addr)
281-
.ok_or(MemoryError::UninitializedMemory(ptr_shift_0_addr))?
282-
.try_into()?;
278+
let ptr: MemoryAddress = self.memory_manager.memory.get_as(ptr_shift_0_addr)?;
283279

284280
// Calculate the final, second-level address: `ptr + shift_1`.
285281
let ptr_shift_1_addr = (ptr + shift_1)?;

crates/leanVm/src/memory/mem.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,26 +125,18 @@ impl Memory {
125125

126126
/// Retrieves and converts the value stored at a given memory address into a desired type `V`.
127127
///
128+
/// Returns an error if the memory cell is uninitialized or if the conversion fails.
129+
///
128130
/// # Arguments
129131
/// * `address`: The `MemoryAddress` specifying the location of the cell to retrieve.
130-
pub(crate) fn get_as<V>(&self, address: MemoryAddress) -> Result<Option<V>, MemoryError>
132+
pub(crate) fn get_as<V>(&self, address: MemoryAddress) -> Result<V, MemoryError>
131133
where
132134
V: TryFrom<MemoryValue, Error = MemoryError>,
133135
{
134-
// Attempt to retrieve the raw memory value at the given address.
135-
match self.get(address) {
136-
// If the address is valid and contains a value:
137-
Some(value) => {
138-
// Attempt to convert the `MemoryValue` into the desired type `V`.
139-
//
140-
// If conversion fails, the error will be propagated.
141-
let converted = V::try_from(value)?;
142-
// Return the successfully converted value wrapped in `Some`.
143-
Ok(Some(converted))
144-
}
145-
// If the address is invalid or the cell is uninitialized, return `None`.
146-
None => Ok(None),
147-
}
136+
// Retrieve the raw value and convert it to the desired type `V`.
137+
self.get(address)
138+
.ok_or(MemoryError::UninitializedMemory(address))
139+
.and_then(V::try_from)
148140
}
149141

150142
/// Retrieves a fixed-size array of field elements starting from a given address.
@@ -201,9 +193,7 @@ impl Memory {
201193
let addr = (start_address + i)?;
202194

203195
// Attempt to retrieve the value from the memory and convert it into type `V`.
204-
let v = self
205-
.get_as(addr)?
206-
.ok_or(MemoryError::UninitializedMemory(addr))?;
196+
let v = self.get_as(addr)?;
207197

208198
// SAFETY: The memory value has been successfully retrieved and converted.
209199
*o = MaybeUninit::new(v);

0 commit comments

Comments
 (0)