]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/associated-types-enum-field-numbered.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / associated-types-enum-field-numbered.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 tuple-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(K),
22     Root(K::Value, 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(ref k) => get(table, k),
28         VarValue::Root(ref v, _) => 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(1),
39                      /* 1 */ Redirect(3),
40                      /* 2 */ Root(Some('x'), 0),
41                      /* 3 */ Redirect(2)];
42     assert_eq!(get(&table, &0), &Some('x'));
43 }