@@ -329,38 +329,118 @@ mod function_trait_bounds {
329
329
}
330
330
331
331
mod trait_associated_type {
332
+ #[ derive( Debug ) ]
333
+ struct Wrapper < A > {
334
+ field : A ,
335
+ }
336
+
337
+ impl < A > Wrapper < A > {
338
+ fn unwrap ( self ) -> A {
339
+ self . field // $ fieldof=Wrapper
340
+ }
341
+ }
342
+
332
343
trait MyTrait {
333
344
type AssociatedType ;
334
345
346
+ // MyTrait::m1
335
347
fn m1 ( self ) -> Self :: AssociatedType ;
336
348
337
349
fn m2 ( self ) -> Self :: AssociatedType
338
350
where
339
351
Self :: AssociatedType : Default ,
340
352
Self : Sized ,
341
353
{
354
+ self . m1 ( ) ; // $ method=MyTrait::m1
342
355
Self :: AssociatedType :: default ( )
343
356
}
344
357
}
345
358
359
+ trait MyTraitAssoc2 {
360
+ type GenericAssociatedType < AssociatedParam > ;
361
+
362
+ // MyTrait::put
363
+ fn put < A > ( & self , a : A ) -> Self :: GenericAssociatedType < A > ;
364
+
365
+ fn putTwo < A > ( & self , a : A , b : A ) -> Self :: GenericAssociatedType < A > {
366
+ self . put ( a) ; // $ method=MyTrait::put
367
+ self . put ( b) // $ method=MyTrait::put
368
+ }
369
+ }
370
+
346
371
#[ derive( Debug , Default ) ]
347
372
struct S ;
348
373
374
+ #[ derive( Debug , Default ) ]
375
+ struct S2 ;
376
+
377
+ #[ derive( Debug , Default ) ]
378
+ struct AT ;
379
+
349
380
impl MyTrait for S {
350
- type AssociatedType = S ;
381
+ type AssociatedType = AT ;
351
382
352
383
// S::m1
353
384
fn m1 ( self ) -> Self :: AssociatedType {
354
- S
385
+ AT
386
+ }
387
+ }
388
+
389
+ impl MyTraitAssoc2 for S {
390
+ // Associated type with a type parameter
391
+ type GenericAssociatedType < AssociatedParam > = Wrapper < AssociatedParam > ;
392
+
393
+ // S::put
394
+ fn put < A > ( & self , a : A ) -> Wrapper < A > {
395
+ Wrapper { field : a }
396
+ }
397
+ }
398
+
399
+ impl MyTrait for S2 {
400
+ // Associated type definition with a type argument
401
+ type AssociatedType = Wrapper < S2 > ;
402
+
403
+ fn m1 ( self ) -> Self :: AssociatedType {
404
+ Wrapper { field : self }
405
+ }
406
+ }
407
+
408
+ // NOTE: This implementation is just to make it possible to call `m2` on `S2.`
409
+ impl Default for Wrapper < S2 > {
410
+ fn default ( ) -> Self {
411
+ Wrapper { field : S2 }
355
412
}
356
413
}
357
414
415
+ // Function that returns an associated type from a trait bound
416
+ fn g < T : MyTrait > ( thing : T ) -> <T as MyTrait >:: AssociatedType {
417
+ thing. m1 ( ) // $ method=MyTrait::m1
418
+ }
419
+
358
420
pub fn f ( ) {
359
- let x = S ;
360
- println ! ( "{:?}" , x. m1( ) ) ; // $ method=S::m1
421
+ let x1 = S ;
422
+ // Call to method in `impl` block
423
+ println ! ( "{:?}" , x1. m1( ) ) ; // $ method=S::m1 type=x1.m1():AT
361
424
362
- let x = S ;
363
- println ! ( "{:?}" , x. m2( ) ) ; // $ method=m2
425
+ let x2 = S ;
426
+ // Call to default method in `trait` block
427
+ let y = x2. m2 ( ) ; // $ method=m2 MISSING: type=y:AT
428
+ println ! ( "{:?}" , y) ;
429
+
430
+ let x3 = S ;
431
+ // Call to the method in `impl` block
432
+ println ! ( "{:?}" , x3. put( 1 ) . unwrap( ) ) ; // $ method=S::put method=unwrap
433
+
434
+ // Call to default implementation in `trait` block
435
+ println ! ( "{:?}" , x3. putTwo( 2 , 3 ) . unwrap( ) ) ; // $ method=putTwo MISSING: method=unwrap
436
+
437
+ let x4 = g ( S ) ; // $ MISSING: type=x4:AT
438
+ println ! ( "{:?}" , x4) ;
439
+
440
+ let x5 = S2 ;
441
+ println ! ( "{:?}" , x5. m1( ) ) ; // $ method=m1 MISSING: type=x5.m1():A.S2
442
+ let x6 = S2 ;
443
+ println ! ( "{:?}" , x6. m2( ) ) ; // $ method=m2 MISSING: type=x6.m2():A.S2
364
444
}
365
445
}
366
446
0 commit comments