rust-mysql-simple icon indicating copy to clipboard operation
rust-mysql-simple copied to clipboard

Implement ToRow for T where T: IntoValue

Open gsingh93 opened this issue 10 years ago • 3 comments

I was trying to use a single value as the argument to prep_exec, and I was surprised when I couldn't pass the value val or (val) to the function. I had to pass (val,), which makes sense in retrospect, but I don't see why ToRow couldn't just be implemented for the type T directly.

gsingh93 avatar Jul 31 '15 01:07 gsingh93

I tried, but

src/value.rs:732:1: 736:2 error: conflicting implementations for trait `value::ToRow` [E0119]
src/value.rs:732 impl<'a, T: ToRow + Clone> ToRow for &'a T {
src/value.rs:733     fn to_row(self) -> Vec<Value> {
src/value.rs:734         self.clone().to_row()
src/value.rs:735     }
src/value.rs:736 }

blackbeam avatar Jul 31 '15 04:07 blackbeam

If you remove the implementation for &'a T, will the implementation for T cover that case?

gsingh93 avatar Jul 31 '15 17:07 gsingh93

No. It starts to conflict with all non-generic impls of IntoValue trait. There a three traits involved in making execute to work:

  1. IntoValue - takes ownership and implemented for all terminal types.
  2. ToValue - this trait is used in ToRow for &[&ToValue] because ToRow for &[&IntoValue] will always dereference DST ref. It works via ToValue for &T where T: IntoValue + Clone.
  3. ToRow - this trait is implemented for tuples, &[&ToValue], Vec<Value> itself and &T where T: ToRow.

And something always broke when i trying to implement ToRow for IntoValue.

There is a way to implement ToRow for IntoValue but i must sacrifice ToRow for &T where T: ToRow and i am not sure i should do this to be able to write t instead of (t,).

Maybe you have arguments which helps me to make this decision?

blackbeam avatar Aug 01 '15 09:08 blackbeam