Skip to content

Commit

Permalink
Add a method to pretty print decl ids to the DeclEngine.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Sep 12, 2023
1 parent 3d65757 commit 60ca1a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
18 changes: 17 additions & 1 deletion sway-core/src/decl_engine/engine.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{
collections::{HashMap, HashSet, VecDeque},
fmt::Write,
sync::RwLock,
};

use sway_types::{Named, Spanned};

use crate::{
concurrent_slab::ConcurrentSlab,
concurrent_slab::{ConcurrentSlab, ListDisplay},
decl_engine::*,
engine_threading::*,
language::ty::{
Expand Down Expand Up @@ -321,4 +322,19 @@ impl DeclEngine {
{
self.get(index)
}

/// Pretty print method for printing the [DeclEngine]. This method is
/// manually implemented to avoid implementation overhead regarding using
/// [DisplayWithEngines].
pub fn pretty_print(&self, engines: &Engines) -> String {
let mut builder = String::new();
self.function_slab.with_slice(|elems| {
let list = elems
.iter()
.map(|type_info| format!("{:?}", engines.help_out(type_info)));
let list = ListDisplay { list };
write!(builder, "DeclEngine {{\n{list}\n}}").unwrap();
});
builder
}
}
7 changes: 7 additions & 0 deletions sway-core/src/language/ty/declaration/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashSet,
fmt,
hash::{Hash, Hasher},
};

Expand Down Expand Up @@ -41,6 +42,12 @@ pub struct TyFunctionDecl {
pub is_trait_method_dummy: bool,
}

impl DebugWithEngines for TyFunctionDecl {
fn fmt(&self, f: &mut fmt::Formatter<'_>, _engines: &Engines) -> fmt::Result {
write!(f, "{:?}", self.name,)
}
}

impl Named for TyFunctionDecl {
fn name(&self) -> &Ident {
&self.name
Expand Down

0 comments on commit 60ca1a6

Please sign in to comment.