@@ -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