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

Not working with non static lifetime #6

Closed
bytedream opened this issue Oct 14, 2024 · 3 comments
Closed

Not working with non static lifetime #6

bytedream opened this issue Oct 14, 2024 · 3 comments

Comments

@bytedream
Copy link
Owner

It is not working when there is a lifetime

#[serde_inline_default]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
struct Person<'a> {
    #[serde_inline_default("A name")]
    name: &'a str,
}
error[E0261]: use of undeclared lifetime name `'a`
  --> src/main.rs:36:12
   |
31 | #[serde_inline_default]
   |                        - lifetime `'a` is missing in item created through this procedural macro
...
36 |     name: &'a str,
   |            ^^ undeclared lifetime

Originally posted by @rochacbruno in #1 (comment)

@bytedream
Copy link
Owner Author

bytedream commented Oct 14, 2024

@rochacbruno creating a default for a reference is generally not supported in serde afaik. You'd have to use Cow<'a, str> instead of &'a str, support for lifetimes in generic arguments got added in v0.2.2.

@rochacbruno
Copy link

Hi @bytedream, this works

fn default_name() -> &'static str {
    "My Site"
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
struct Site<'a> {
    #[serde(default = "default_name")]
    name: &'a str,
}

@bytedream
Copy link
Owner Author

Okay that's a bit weird, yesterday evening I had a similar example and it didn't compile haha, but yours does so all good.
v0.2.2 also has support for lifetimes references, so using at least this version should fix your problem.

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