google / google/xls

[enhancement] Add custom formatting for structs in DSLX trace_fmt!

Open
#2,604 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

### What's hard to do? (limit 100 words)

DSLX structs can't define default `trace_fmt!` formatting logic. When tracing or debugging, all struct fields are printed using the format specified in the curly braces `{}`. This can be hard to read for larger structs, especially when different fields should be shown in decimal, hexadecimal, or binary formats. The problem is even more visible for nested structs. There’s currently no way to format only the relevant fields or present them in a cleaner, more readable way.

### Current best alternative workaround (limit 100 words)

The current approach is to manually build formatted strings using `trace_fmt!`, extracting each field individually:

```rust
trace_fmt!(tok, "Point = (x={:#x}, y={:#b})", p.x, p.y);
```

It’s also possible to wrap such `trace_fmt!` calls in helper functions to reduce duplication, but this adds complexity and still lacks a standard format per type.

### Your view of the "best case XLS enhancement" (limit 100 words)

DSLX could support a way to define custom formatting for user-defined structs, improving `trace_fmt!` readability and reducing duplication.

One possible approach, inspired by Rust, is to introduce a `Display`-like trait, allowing users to implement a formatting method:

```rust
struct Point {
x: u32,
y: u32,
}

impl Display for Point {
fn to_string(self) -> u8[N] {
format!("({:#x},{:#b})", self.x, self.y)
}
}
```

Then `trace_fmt!` could support this custom formatting directly:
```rust
trace_fmt!(tok, "Point = {}", p);
```

This Rust-like solution would make debug output much cleaner and more consistent.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.