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

Using pass-through formatting in Display #38

Open
nyurik opened this issue Feb 10, 2024 · 3 comments
Open

Using pass-through formatting in Display #38

nyurik opened this issue Feb 10, 2024 · 3 comments

Comments

@nyurik
Copy link
Contributor

nyurik commented Feb 10, 2024

Is there a way to generate this Display implementation? Note that this is not a write!(f, "{0}", v), but rather v.fmt(f) call.

use std::fmt;

pub enum Term {
    Int(i32),
    Float(f64),
    Str(String),
}

impl fmt::Display for Term {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Int(v) => v.fmt(f),
            Self::Float(v) => v.fmt(f),
            Self::Str(v) => v.fmt(f),
        }
    }
}
@frozenlib
Copy link
Owner

Currently, parse-display does not have the ability to generate such code.

In the future, we may add the ability to generate such code by specifying an attribute such as #[display(transparent)].

@nyurik
Copy link
Contributor Author

nyurik commented Feb 11, 2024

Thx @frozenlib for quick reply! Could you maybe point me to what changes would be needed to make it possible? I could try to hack on it in my spare time

@frozenlib
Copy link
Owner

struct DisplayArgs {
#[struct_meta(unnamed)]
format: Option<LitStr>,
with: Option<Expr>,
style: Option<LitStr>,
bound: Option<Vec<Quotable<Bound>>>,
#[struct_meta(name = "crate")]
crate_path: Option<Path>,
dump: bool,
}

I think the first step would be to add field transparent to DisplayArgs and change the code generation method to use this value.

I won't know until I actually write it, but it seems like many modifications will be necessary.

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