rust-si icon indicating copy to clipboard operation
rust-si copied to clipboard

All macros require the output to implement Display

Open KianKhadempour opened this issue 5 months ago • 0 comments

Because try_scan! has the following line

format_args!($pattern, $($arg),*);

an implementation of Display is mandatory for any type that you want to convert to.

Example:

enum Foo {
    Bar,
    Baz,
}

impl FromStr for Foo {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Bar" => Ok(Foo::Bar),
            "Baz" => Ok(Foo::Baz),
            _ => Err(()),
        }
    }
}

fn main() {
    let foo: Foo = read!();
}

Something like the former would be expected to work, but because of the issue, Foo would be required to implement Display as well. This is unnecessary in most cases, so removing that line would make this library easier to use.

Here is the error the compiler gives;

error[E0277]: `Foo` doesn't implement `std::fmt::Display`
   --> src\main.rs:106:20
    |
106 |     let foo: Foo = read!();
    |                    ^^^^^^^ `Foo` cannot be formatted with the default formatter
    |
    = help: the trait `std::fmt::Display` is not implemented for `Foo`
    = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
    = note: this error originates in the macro `format_args` which comes from the expansion of the macro `read` (in Nightly builds, run with -Z macro-backtrace for more info)

KianKhadempour avatar Aug 29 '24 04:08 KianKhadempour