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

All macros require the output to implement Display #49

Open
KianKhadempour opened this issue Aug 29, 2024 · 0 comments
Open

All macros require the output to implement Display #49

KianKhadempour opened this issue Aug 29, 2024 · 0 comments

Comments

@KianKhadempour
Copy link

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 added a commit to KianKhadempour/rust-si that referenced this issue Aug 29, 2024
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

Successfully merging a pull request may close this issue.

1 participant