You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have need to store an Either<L,R> to/from the database, and I noticed the sqlx traits were not implemented for this type so I am attempting to implement a type struct wrapper. However, I am hitting immediate issues implementing Decode as it seems to consume the self value. My current strategy is to simply try decoding it as an L and if that doesn't work then fallback and assume it must be an R, however, the ValueRef trait is quite limited and I can't call decode a 2nd time (assuming the first fails, mean it is not an L) since the value has now moved. I have tried converting to a Value using to_owned, but both converting it back via as_ref and try_decode are meeting lifetime issues. Any tips? Or can this not be done with the current state of Decode?
I ended up making my type EitherStr and making it work by first decoding to a &str and then putting bounds on L and R to implement the std FromStr trait. This works for me so I have sailed on, and in some ways, makes sense given that a generic Either holding say an integer as L and a string as R wouldn't work with the underlying DB anyway, so in reality, the underlying type is going to have to be the same, even if you have made it a wrapper type as I have. That said, I'm curious if there is a way to implement Decode above for completely different types essentially calling decode twice, and on error, calling it again for the other param, or if what I've done is the only way.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have need to store an
Either<L,R>to/from the database, and I noticed the sqlx traits were not implemented for this type so I am attempting to implement a type struct wrapper. However, I am hitting immediate issues implementingDecodeas it seems to consume theselfvalue. My current strategy is to simply try decoding it as anLand if that doesn't work then fallback and assume it must be anR, however, theValueReftrait is quite limited and I can't calldecodea 2nd time (assuming the first fails, mean it is not anL) since thevaluehas now moved. I have tried converting to aValueusingto_owned, but both converting it back viaas_refandtry_decodeare meeting lifetime issues. Any tips? Or can this not be done with the current state ofDecode?Beta Was this translation helpful? Give feedback.
All reactions