2 // Various uses of `T::Item` syntax where the bound that supplies
3 // `Item` originates in a where-clause, not the declaration of
6 use std::marker::{PhantomData};
7 use std::sync::atomic::{AtomicUsize};
8 use std::sync::atomic::Ordering::SeqCst;
10 static COUNTER: AtomicUsize = AtomicUsize::new(0);
13 trait Trait { type Item; }
15 impl Trait for Struct {
19 // Where-clause attached on the method which declares `T`.
22 fn foo<T>(_x: T::Item) where T: Trait {
23 COUNTER.fetch_add(1, SeqCst);
27 // Where-clause attached on the method to a parameter from the struct.
28 struct B<T>(PhantomData<T>);
30 fn foo(_x: T::Item) where T: Trait {
31 COUNTER.fetch_add(10, SeqCst);
35 // Where-clause attached to free fn.
36 fn c<T>(_: T::Item) where T : Trait {
37 COUNTER.fetch_add(100, SeqCst);
40 // Where-clause attached to defaulted and non-defaulted trait method.
42 fn method<T>(&self, _: T::Item) where T: Trait;
43 fn default_method<T>(&self, _: T::Item) where T: Trait {
44 COUNTER.fetch_add(1000, SeqCst);
48 impl AnotherTrait for D {
49 fn method<T>(&self, _: T::Item) where T: Trait {
50 COUNTER.fetch_add(10000, SeqCst);
54 // Where-clause attached to trait and impl containing the method.
55 trait YetAnotherTrait<T>
58 fn method(&self, _: T::Item);
59 fn default_method(&self, _: T::Item) {
60 COUNTER.fetch_add(100000, SeqCst);
63 struct E<T>(PhantomData<T>);
64 impl<T> YetAnotherTrait<T> for E<T>
67 fn method(&self, _: T::Item) {
68 COUNTER.fetch_add(1000000, SeqCst);
72 // Where-clause attached to inherent impl containing the method.
73 struct F<T>(PhantomData<T>);
74 impl<T> F<T> where T : Trait {
75 fn method(&self, _: T::Item) {
76 COUNTER.fetch_add(10000000, SeqCst);
80 // Where-clause attached to struct.
82 struct G<T> where T : Trait {
84 phantom: PhantomData<T>,
91 D.method::<Struct>(22);
92 D.default_method::<Struct>(22);
93 E(PhantomData::<Struct>).method(22);
94 E(PhantomData::<Struct>).default_method(22);
95 F(PhantomData::<Struct>).method(22);
96 G::<Struct> { data: 22, phantom: PhantomData };
97 assert_eq!(COUNTER.load(SeqCst), 11111111);