Rust: Add examples with associated type accessed on associated type

This commit is contained in:
Simon Friis Vindum
2026-02-11 09:10:21 +01:00
parent 2b10c8aef3
commit 2fa71f0c17
2 changed files with 603 additions and 529 deletions

View File

@@ -7,7 +7,7 @@ impl<A> Wrapper<A> {
}
}
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone, Copy)]
struct S;
#[derive(Debug, Default)]
@@ -270,6 +270,41 @@ mod type_param_access_associated_type {
}
}
// Associated type accessed on another associated type
fn tp_nested_assoc_type<T: GetSet>(thing: T) -> <<T as GetSet>::Output as GetSet>::Output
where
<T as GetSet>::Output: GetSet,
{
thing.get().get() // $ target=GetSet::get target=GetSet::get
}
pub trait GetSetWrap {
type Assoc: GetSet;
// GetSetWrap::get_wrap
fn get_wrap(&self) -> Self::Assoc;
}
impl GetSetWrap for S {
type Assoc = S;
// S::get_wrap
fn get_wrap(&self) -> Self::Assoc {
S
}
}
// Nested associated type accessed on a type parameter of an impl block
impl<TI> Wrapper<TI>
where
TI: GetSetWrap,
{
fn extract2(&self) -> <<TI as GetSetWrap>::Assoc as GetSet>::Output {
self.0.get_wrap().get() // $ fieldof=Wrapper target=GetSetWrap::get_wrap $ MISSING: target=GetSet::get
}
}
pub fn test() {
let _o1 = tp_with_as(S); // $ target=tp_with_as type=_o1:S3
let _o2 = tp_without_as(S); // $ target=tp_without_as type=_o2:S3
@@ -278,8 +313,12 @@ mod type_param_access_associated_type {
_o4, // $ type=_o4:bool
) = tp_assoc_from_supertrait(S); // $ target=tp_assoc_from_supertrait
let _o5 = tp_nested_assoc_type(Wrapper(S)); // $ target=tp_nested_assoc_type MISSING: type=_o5:S3
let w = Wrapper(S);
let _extracted = w.extract(); // $ target=extract type=_extracted:S3
let _extracted2 = w.extract2(); // $ target=extract2 MISSING: type=_extracted2:S3
}
}