Skip to content

Using deserialize_as on Nullable/Option values. #4425

Answered by weiznich
RedIODev asked this question in Q&A
Discussion options

You must be logged in to vote

This is expected behavior. #[diesel(deserialize_as)] expects that this TryFrom impl exists as documented here.

Rust's standard library doesn't provide any neither of these impls:

  • impl<F, T> TryFrom<Option<F>> for Option<T>
  • impl<F, T> From<Option<F>> for Option<T> (Implied via the existing impl<T, U: Into<T>> TryFrom]<U> for T)

as far as I know that's a because the coherence checker sees that the existing impl<T> From<T> for T would overlap with one of these missing impls for T == F.

So long story short: You cannot use the same type for both the non-optional and optional field, you need to use another new type wrapper for the optional variant.

Replies: 1 comment 3 replies

Comment options

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

@weiznich
Comment options

@RedIODev
Comment options

Answer selected by RedIODev
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