Skip to content

Commit

Permalink
Implement Display for MemorySegmentManager (lambdaclass#1606)
Browse files Browse the repository at this point in the history
* Impl Display for MemorySegmentManager

* Remove old code + Add changelog entry

* Fix

* fmt

* Update test

---------

Co-authored-by: Pedro Fontana <[email protected]>
  • Loading branch information
fmoletta and pefontana authored Feb 21, 2024
1 parent beede16 commit 0e6a235
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
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

* feat: Implement `Display` for `MemorySegmentManager`[#1606](https://github.com/lambdaclass/cairo-vm/pull/1606)

* fix: make Felt252DictEntryUpdate work with MaybeRelocatable instead of only Felt [#1624](https://github.com/lambdaclass/cairo-vm/pull/1624).

* chore: bump `cairo-lang-` dependencies to 2.5.3 [#1596](https://github.com/lambdaclass/cairo-vm/pull/1596)
Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ impl fmt::Display for Memory {
}
}
}
writeln!(f, "}}")
Ok(())
}
}

Expand Down Expand Up @@ -1424,7 +1424,7 @@ mod memory_tests {

assert_eq!(
format!("{}", memory),
"(-1,0) : -1:0\n(-1,1) : 8\n(-1,2) : 9\n(0,0) : 1\n(0,1) : -1:0\n(0,2) : 3\n(1,0) : -1:1\n(1,1) : 5\n(1,2) : -1:2\n}\n");
"(-1,0) : -1:0\n(-1,1) : 8\n(-1,2) : 9\n(0,0) : 1\n(0,1) : -1:0\n(0,2) : 3\n(1,0) : -1:1\n(1,1) : 5\n(1,2) : -1:2\n");
}

#[test]
Expand Down
24 changes: 24 additions & 0 deletions vm/src/vm/vm_memory/memory_segments.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt;

use crate::stdlib::prelude::*;
use crate::stdlib::{any::Any, collections::HashMap};
use crate::vm::runners::cairo_runner::CairoArg;
Expand Down Expand Up @@ -269,6 +271,28 @@ impl Default for MemorySegmentManager {
}
}

impl fmt::Display for MemorySegmentManager {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "Memory:\n{}", self.memory)?;
if let Some(used_sizes) = &self.segment_used_sizes {
writeln!(f, "Segment Info:")?;
for (index, used_size) in used_sizes.iter().enumerate() {
writeln!(
f,
"Segment Number: {}, Used Size: {}, Size {}",
index,
used_size,
self.segment_sizes
.get(&index)
.map(|n| n.to_string())
.unwrap_or(String::from("None"))
)?;
}
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 0e6a235

Please sign in to comment.