]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-types/associated-types-enum-field-named.rs
feat(rustdoc): open sidebar menu when links inside it are focused
[rust.git] / src / test / ui / associated-types / associated-types-enum-field-named.rs
1 // run-pass
2 // Test associated types appearing in struct-like enum variants.
3
4
5 use self::VarValue::*;
6
7 pub trait UnifyKey {
8     type Value;
9     fn to_index(&self) -> usize;
10 }
11
12 pub enum VarValue<K:UnifyKey> {
13     Redirect { to: K },
14     Root { value: K::Value, rank: usize },
15 }
16
17 fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
18     match table[key.to_index()] {
19         VarValue::Redirect { to: ref k } => get(table, k),
20         VarValue::Root { value: ref v, rank: _ } => v,
21     }
22 }
23
24 impl UnifyKey for usize {
25     type Value = Option<char>;
26     fn to_index(&self) -> usize { *self }
27 }
28
29 fn main() {
30     let table = vec![/* 0 */ Redirect { to: 1 },
31                      /* 1 */ Redirect { to: 3 },
32                      /* 2 */ Root { value: Some('x'), rank: 0 },
33                      /* 3 */ Redirect { to: 2 }];
34     assert_eq!(get(&table, &0), &Some('x'));
35 }