Created
December 29, 2019 15:09
-
-
Save sidepelican/803eba4ca81080d1321bcf1ede6032f4 to your computer and use it in GitHub Desktop.
generics function overload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
l26の挙動から a
で T<&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
l25:
T<&[U]>::f
が呼べてほしいl26:
&[u8]
は&mut [U]
にはなれないので正しいl29: あいまいであるのは正しい
l30: 正しい