]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/associated-types-enum-field-named.rs
a499aa6733aae0783532874e7292b1c91db1263d
[rustc.git] / src / test / run-pass / associated-types-enum-field-named.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test associated types appearing in struct-like enum variants.
12
13 use self::VarValue::*;
14
15 pub trait UnifyKey {
16 type Value;
17 fn to_index(&self) -> usize;
18 }
19
20 pub enum VarValue<K:UnifyKey> {
21 Redirect { to: K },
22 Root { value: K::Value, rank: usize },
23 }
24
25 fn get<'a,K:UnifyKey<Value=Option<V>>,V>(table: &'a Vec<VarValue<K>>, key: &K) -> &'a Option<V> {
26 match table[key.to_index()] {
27 VarValue::Redirect { to: ref k } => get(table, k),
28 VarValue::Root { value: ref v, rank: _ } => v,
29 }
30 }
31
32 impl UnifyKey for usize {
33 type Value = Option<char>;
34 fn to_index(&self) -> usize { *self }
35 }
36
37 fn main() {
38 let table = vec![/* 0 */ Redirect { to: 1 },
39 /* 1 */ Redirect { to: 3 },
40 /* 2 */ Root { value: Some('x'), rank: 0 },
41 /* 3 */ Redirect { to: 2 }];
42 assert_eq!(get(&table, &0), &Some('x'));
43 }