From 0e6a235dc40d30639578037191627742fefe3b7f Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Wed, 21 Feb 2024 18:55:40 +0200 Subject: [PATCH] Implement `Display` for `MemorySegmentManager` (#1606) * Impl Display for MemorySegmentManager * Remove old code + Add changelog entry * Fix * fmt * Update test --------- Co-authored-by: Pedro Fontana --- CHANGELOG.md | 2 ++ vm/src/vm/vm_memory/memory.rs | 4 ++-- vm/src/vm/vm_memory/memory_segments.rs | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6150ba3635..e45de2e562 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/vm/src/vm/vm_memory/memory.rs b/vm/src/vm/vm_memory/memory.rs index 6a3db35086..f50d5c11cd 100644 --- a/vm/src/vm/vm_memory/memory.rs +++ b/vm/src/vm/vm_memory/memory.rs @@ -551,7 +551,7 @@ impl fmt::Display for Memory { } } } - writeln!(f, "}}") + Ok(()) } } @@ -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] diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index 58e65589c1..cabba6266b 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -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; @@ -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::*;