Skip to content

Trouble using DisplayFromStr on an Option<T> field. #329

Answered by jonasbb
Krapaince asked this question in Q&A
Discussion options

You must be logged in to vote

If your Documentation implements Display, then you write this. If you need Deserialize you also want to add #[serde(default)], otherwise the field will not be optional while deserializing. This issue #185 has the details.

    #[serde_as(as = "Option<DisplayFromStr>")]
    error_uri: Option<Documentation>,

Writing #[serde_as(as = "Option<DisplayFromStr>")] means there is a Option<T> where T: Display. If you write #[serde_as(as = "DisplayFromStr")] it requires your field to be Display, here Option<Documentation>: Display, but Option never implements Display, so this will not work.

In general, the idea of serde_as is that you attribute mirrors the structure of your type, but you place a modi…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Krapaince
Comment options

@jonasbb
Comment options

@Krapaince
Comment options

Answer selected by jonasbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants