feat: impl Encode, Decode and Type for Cow<T> where T: Encode + Decode + Type #3102
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This allows to use Cow for every T that implements
Encode
,Decode
, andType
.The motivation for this is that I can use the same struct to decode and encode without having unnecessary copies of data. This is especially helpful in situations where you a have struct for all db tables, since then you would need clone the participating keys of each relation into those structs.
This allows to write this
, instead of this.
The implementation for
Encode
andType
will just delegate to the implementation of T.The
Decode
trait is implemented by creating an owned Cow.The owned variant is used here, because
query_as
function has the boundfor<'r> FromRow<'r, DB::Row>
.This can only be implemented if the lifetime of the type is arbitrary/static, which necessitate the owned variant.
query_as!
won't work with an object with the same lifetime as the row, which also leads to the owned variant.I believe that the current
impl<'r, DB> Decode<'r, DB> for Cow<'r, str>
is broken/ useless, but maybe i've miss the use case for the borrowed variant while decoding.This implementation currently does not work with the derive macro for FromRow, because the macro implements
impl<'r, R> FromRow<'r, R> for TableA<'r>
instead ofimpl<'r,'a, R> FromRow<'r, R> for TableA<'a>
.But manually implementing
FromRow
works.fixed #3100