Skip to content

Instantly share code, notes, and snippets.

@sidepelican
Created December 29, 2019 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidepelican/803eba4ca81080d1321bcf1ede6032f4 to your computer and use it in GitHub Desktop.
Save sidepelican/803eba4ca81080d1321bcf1ede6032f4 to your computer and use it in GitHub Desktop.
generics function overload
use std::marker::PhantomData;
struct T<A> {
_phantom: PhantomData<A>,
}
impl<U> T<&[U]> {
fn f(_: &[U]) {
println!("f(_: &[U])")
}
}
impl<U> T<&mut [U]> {
fn f(_: &mut [U]) {
println!("f(_: &mut [U])")
}
fn g(_: &mut [U]) {
println!("g(_: &mut [U])")
}
}
fn main() {
let a: &[u8] = &[1][..];
// T::f(a); // multiple applicable items in scope
// T::g(a); // mismatched types
let b: &mut [u8] = &mut [1][..];
// T::f(b); // multiple applicable items in scope
T::g(b);
}
@sidepelican
Copy link
Author

l25: T<&[U]>::f が呼べてほしい
l26: &[u8]&mut [U] にはなれないので正しい
l29: あいまいであるのは正しい
l30: 正しい

@sidepelican
Copy link
Author

l26の挙動から aT<&mut [U]>::f は呼べないだろうと思われ、l25は T<&[U]>::f で一意に解決できそうだけどそうはならなくて不思議

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment