Skip to content

dev: make Memory::get pub #2039

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

Merged
merged 5 commits into from
Apr 15, 2025
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* dev: add Memory::get_maybe_relocatable [#2039](https://github.com/lambdaclass/cairo-vm/pull/2039)

* refactor: remove duplicated get_val function [#2065](https://github.com/lambdaclass/cairo-vm/pull/2065)

* fix: Always use a normal segment in first SegmentArena segment [#1845](https://github.com/lambdaclass/cairo-vm/pull/1845)
Expand Down
36 changes: 36 additions & 0 deletions vm/src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,19 @@
}
}

/// Gets the value from memory address as a MaybeRelocatable value.
/// Returns an Error if the value at the memory address is missing or not a MaybeRelocatable.
pub fn get_maybe_relocatable(&self, key: Relocatable) -> Result<MaybeRelocatable, MemoryError> {
match self
.get(&key)
.ok_or_else(|| MemoryError::UnknownMemoryCell(Box::new(key)))?
{
// Note: the `Borrowed` variant will never occur.
Cow::Borrowed(maybe_rel) => Ok(maybe_rel.clone()),

Check warning on line 459 in vm/src/vm/vm_memory/memory.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/vm_memory/memory.rs#L459

Added line #L459 was not covered by tests
Cow::Owned(maybe_rel) => Ok(maybe_rel),
}
}

/// Inserts a value into memory
/// Returns an error if the memory cell asignment is invalid
pub fn insert_value<T: Into<MaybeRelocatable>>(
Expand Down Expand Up @@ -1194,6 +1207,29 @@
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_maybe_relocatable_valid_relocatable() {
let memory = memory![((0, 0), (1, 0))];
assert_eq!(
memory
.get_maybe_relocatable(Relocatable::from((0, 0)))
.unwrap(),
Relocatable::from((1, 0)).into()
);
}

#[test]
fn get_maybe_relocatable_valid_integer() {
let memory = memory![((0, 0), 10)];
assert_eq!(
memory
.get_maybe_relocatable(Relocatable::from((0, 0)))
.unwrap(),
10.into()
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn default_memory() {
Expand Down
Loading