Skip to content

Better delegation detection in fmt derives #502

@justDeeevin

Description

@justDeeevin

#322 added a behavior that automatically detects when actual printing behavior is being delegated to the formatter. However, I'm finding that this isn't applying in more complex cases.

Here's my situation:
I've got a Value enum in my crate serde-beve which represents the self-describing values in the BEVE data format. In this enum, strings are stored as Vec<u8>s in order to more closely match how they're stored in the data format itself, and to allow me to just write the bytes directly during serialization. However, I'd like the debug implementation of Value to print the string itself instead of the bytes. Should be simple:

#[derive(derive_more::Debug)]
enum Value {
    // ...
    StringArray(
        #[debug("{:?}", _0.iter().map(|v| unsafe {std::str::from_utf8_unchecked(v)}))]
        Vec<Vec<u8>>
    ),
    // ...
}

However, as described in #321, this ends up never pretty-printing the vector

fn main() {
    let value = Value::from(vec!["hello", "world"]); // impl From<Vec<&str>> for Value is provided by my crate
    println!("{value:#?}");
}

Expected:

StringArray(
  [
    "hello",
    "world",
  ],
)

Got:

String(
  ["hello", "world"],
)

I'm not sure what the current implementation lacks that prevents it from successfully handling this case, but I feel like it should be possible.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions