-
I use deserialize_as to deserialize a blob from sqlite as an Uuid from the uuid library. The code for the Uuid wrapper allowing deserialization can be found in this issue. It works great with non nullable Uuids. But in case of nullable blobs in the sql database deserialize_as doesn't work for some reason. I tried multiple possible syntax that could be used and concluded on the following one causing the smallest error. #[derive(Debug, Queryable, Selectable)]
#[diesel(table_name = xyz]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct Example {
#[diesel(deserialize_as = UUID)
id: Uuid, //works as Intended
#[diesel(deserialize_as = Option<UUID>)
opt_id: Option<Uuid>, // doesn't work:
// the trait bound `std::option::Option<Uuid>: TryFrom<std::option::Option<UUID>>` is not satisfied
#[diesel(deserialize_as = Option<UUID>)
opt_id2: Option<UUID> // works for some reason (probably identical to opt_id3)
opt_id3: Option<UUID> // works obviously because UUID implements FromSql etc.
} I really dislike the wrapper member in the struct. Is there a way to keep the unwrapped form in the struct? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
This is expected behavior. Rust's standard library doesn't provide any neither of these impls:
as far as I know that's a because the coherence checker sees that the existing 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. |
Beta Was this translation helpful? Give feedback.
This is expected behavior.
#[diesel(deserialize_as)]
expects that thisTryFrom
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 existingimpl<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 forT == 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.