Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl Type, Encode, Decode for AsRef<str> #1347

Closed
malobre opened this issue Jul 27, 2021 · 5 comments
Closed

Impl Type, Encode, Decode for AsRef<str> #1347

malobre opened this issue Jul 27, 2021 · 5 comments

Comments

@malobre
Copy link

malobre commented Jul 27, 2021

Currently postgres' Text (and other db equivalents) is compatible with:

  • &'_ str
  • String
  • [&'_ str]
  • [String]
  • Vec<&'_ str>
  • Vec<String>

Instead, I suggest implementing Type, Encode & Decode for:

  • T: AsRef<str>
  • [T] where T: AsRef<str>
  • Vec<T> where T: AsRef<str>

This would simplify the code while allowing more types, such as Box<str> or Cow<'_, str>, to be used with Text.

I haven't taken a look at other SQL types but this suggestion could apply there too.

Related:

@jplatte
Copy link
Contributor

jplatte commented Jul 27, 2021

Instead, I suggest implementing Type, Encode & Decode for:

  • T: AsRef<str>
  • [T] where T: AsRef<str>
  • Vec<T> where T: AsRef<str>

This is not possible. The first impl would conflict with the other two: as far as the trait solver is concerned std may introduce AsRef<str> impls for [T] or Vec<T> (for some T) in the future. Likewise, the two latter impls will conflict with any impls for specific [SomeType] and Vec<SomeType> impls respectively.

@malobre
Copy link
Author

malobre commented Jul 27, 2021

I see how the impls for [T], Vec<T> will conflict but T: AsRef<str> and [T] where T: AsRef<str> seems possible ?

Playground

@jplatte
Copy link
Contributor

jplatte commented Jul 27, 2021

Right, because the first impl is really for T: Sized and [T] is not Sized. Still this won't really help, the AsRef<str> impl will conflict with almost all other impls since most of the types Encode and Decode are implemented for are Sized.

@malobre
Copy link
Author

malobre commented Jul 27, 2021

Got it, sorry for the noise. I'll create another issue for explicit Box<str> impls.

@malobre malobre closed this as completed Jul 27, 2021
@malobre
Copy link
Author

malobre commented Jul 28, 2021

I guess this could be revisited once specialization (rust-lang/rust#31844) is stabilized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants