Skip to content

Commit 7ba282c

Browse files
authored
dev: make Memory::get pub (#2039)
* dev: make Memory::get `pub` * edit changelog * dev: expose get_maybe_relocatable instead * add comment on borrowed variant * add tests
1 parent 5ef8405 commit 7ba282c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* dev: add Memory::get_maybe_relocatable [#2039](https://github.com/lambdaclass/cairo-vm/pull/2039)
6+
57
* refactor: remove duplicated get_val function [#2065](https://github.com/lambdaclass/cairo-vm/pull/2065)
68

79
* fix: Always use a normal segment in first SegmentArena segment [#1845](https://github.com/lambdaclass/cairo-vm/pull/1845)

vm/src/vm/vm_memory/memory.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,19 @@ impl Memory {
448448
}
449449
}
450450

451+
/// Gets the value from memory address as a MaybeRelocatable value.
452+
/// Returns an Error if the value at the memory address is missing or not a MaybeRelocatable.
453+
pub fn get_maybe_relocatable(&self, key: Relocatable) -> Result<MaybeRelocatable, MemoryError> {
454+
match self
455+
.get(&key)
456+
.ok_or_else(|| MemoryError::UnknownMemoryCell(Box::new(key)))?
457+
{
458+
// Note: the `Borrowed` variant will never occur.
459+
Cow::Borrowed(maybe_rel) => Ok(maybe_rel.clone()),
460+
Cow::Owned(maybe_rel) => Ok(maybe_rel),
461+
}
462+
}
463+
451464
/// Inserts a value into memory
452465
/// Returns an error if the memory cell asignment is invalid
453466
pub fn insert_value<T: Into<MaybeRelocatable>>(
@@ -1194,6 +1207,29 @@ mod memory_tests {
11941207
);
11951208
}
11961209

1210+
#[test]
1211+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1212+
fn get_maybe_relocatable_valid_relocatable() {
1213+
let memory = memory![((0, 0), (1, 0))];
1214+
assert_eq!(
1215+
memory
1216+
.get_maybe_relocatable(Relocatable::from((0, 0)))
1217+
.unwrap(),
1218+
Relocatable::from((1, 0)).into()
1219+
);
1220+
}
1221+
1222+
#[test]
1223+
fn get_maybe_relocatable_valid_integer() {
1224+
let memory = memory![((0, 0), 10)];
1225+
assert_eq!(
1226+
memory
1227+
.get_maybe_relocatable(Relocatable::from((0, 0)))
1228+
.unwrap(),
1229+
10.into()
1230+
);
1231+
}
1232+
11971233
#[test]
11981234
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
11991235
fn default_memory() {

0 commit comments

Comments
 (0)