Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Rust #31

Closed
Nugine opened this issue Feb 26, 2020 · 4 comments
Closed

Add support for Rust #31

Nugine opened this issue Feb 26, 2020 · 4 comments

Comments

@Nugine
Copy link

Nugine commented Feb 26, 2020

Can this awesome plugin support Rust?

Rust has std::fmt::Debug which may be helpful to the implementation.

@hediet
Copy link
Owner

hediet commented Feb 26, 2020

Which debugger do you use for rust?
I remember that calling trait methods from gdb was not possible.
If you visualize a suitable json string, it should work out of the box.

@Nugine
Copy link
Author

Nugine commented Feb 26, 2020

The debugger is lldb.
I manually generate a visualization string at each break point and get the visualizer worked.

Here is a proof of concept.

screenshot.png

use serde::Serialize;

#[derive(Debug, Serialize)]
struct Label {
    label: Option<String>,
}
#[derive(Debug, Serialize)]
struct Row {
    label: Option<String>,
    columns: Vec<Column>,
}
#[derive(Debug, Serialize)]
struct Column {
    content: Option<String>,
    tag: Option<String>,
    color: Option<String>,
}
#[derive(Debug, Serialize)]
struct Marker {
    id: String,
    row: u64,
    column: u64,
    rows: Option<u64>,
    columns: Option<u64>,
    label: Option<String>,
    color: Option<String>,
}

#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_false(b: &bool) -> bool {
    !*b
}

#[derive(Debug, Serialize)]
struct Kind {
    #[serde(skip_serializing_if = "is_false")]
    array: bool,
}

#[derive(Debug, Serialize)]
pub struct Grid {
    kind: Kind,
    #[serde(rename = "columnLabels")]
    column_labels: Option<Vec<Label>>,
    rows: Vec<Row>,
    markers: Option<Vec<Marker>>,
}

fn show_arr(a: &[i32]) -> String {
    let n = a.len();
    let labels: Vec<Label> = (0..n)
        .map(|i| Label {
            label: Some(i.to_string()),
        })
        .collect();
    let columns: Vec<Column> = a
        .iter()
        .map(|x| Column {
            content: Some(format!("{:?}", x)),
            tag: Some(x.to_string()),
            color: None,
        })
        .collect();
    let row = Row {
        label: None,
        columns,
    };
    let kind = Kind { array: true };

    let grid = Grid {
        kind,
        column_labels: Some(labels),
        rows: vec![row],
        markers: None,
    };

    serde_json::to_string(&grid).unwrap()
}

fn main() {
    let mut arr = vec![1, 2, 3];
    let mut _s = show_arr(&arr);
    for _ in 0..5 {
        arr.swap(0, 2); // break point
        _s = show_arr(&arr);
    }
    dbg!(arr); // break point
    println!("Hello, world!");
}

It is more difficult for no-vm languages to debug with visualization. I think it's impossible to visualize without intrusive code.

@hediet
Copy link
Owner

hediet commented Feb 26, 2020

I think it's impossible to visualize without intrusive code [for no-vm languages].

You might be right. Injecting code into runtimes of scripting languages is very easy (you can just eval' a class declaration), into languages interpreting bytecode harder (you need to invoke the class loader) and compiled languages very hard.
The vscode-lldb extension already supports debug visualizers. They are written in python and output html using lldb's debugging API and are very specific to Rust/C++. My extension is almost generic and builds a lot of infrastructure around the visualizations. Maybe, by implementing a special case for lldb, my extension use the same approach.

A year ago, I worked on a MiniJava compiler in Rust.
Back then, I hacked together a prototype of this extension that could visualize the lattice states during constant folding and load store optimization. It was incredibly useful, without it I don't think I could've tackled that task in the given time.

So I still think that this extension might be very useful for compile time languages in some specific cases. Beginners shouldn't use rust to learn algorithms anyway.

Thanks for that demo btw.! Maybe a crate would be awesome!

@Nugine
Copy link
Author

Nugine commented Feb 26, 2020

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants