update copyright year range in GDB files
[external/binutils.git] / gdb / testsuite / gdb.rust / methods.rs
1 // Copyright (C) 2016-2017 Free Software Foundation, Inc.
2
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 #![allow(dead_code)]
17 #![allow(unused_variables)]
18 #![allow(unused_assignments)]
19
20
21 pub trait Whatever {
22     fn whatever(&self) -> i32;
23     fn static_i32(x: i32) -> Self;
24 }
25
26 impl Whatever for i32 {
27     fn whatever(&self) -> i32 {
28         *self                   // set breakpoint 2 here
29     }
30
31     fn static_i32(x: i32) -> i32 {
32         x
33     }
34 }
35
36 pub struct HasMethods {
37     value: i32
38 }
39
40 impl HasMethods {
41     pub fn new() -> HasMethods {
42         HasMethods { value: 0 }
43     }
44
45     pub fn incr(&mut self) -> &mut HasMethods {
46         self.value += 1;
47         self
48     }
49
50     pub fn take(self) -> HasMethods {
51         self
52     }
53 }
54
55 impl Whatever for HasMethods {
56     fn whatever(&self) -> i32 {
57         self.value
58     }
59
60     fn static_i32(x: i32) -> HasMethods {
61         HasMethods{value: x}
62     }
63 }
64
65 enum SomeEnum {
66     One,
67     Two,
68     Three(i32),
69     Four{x: i32}
70 }
71
72 impl SomeEnum {
73     fn value(&self) -> i32 {
74         match *self {
75             SomeEnum::Three(x) => x,
76             SomeEnum::Four{x} => x,
77             _ => 0
78         }
79     }
80
81     fn mut_value(&mut self) -> i32 {
82         match *self {
83             SomeEnum::Three(x) => x,
84             SomeEnum::Four{x} => x,
85             _ => 0
86         }
87     }
88
89     fn take_value(self) -> (i32, SomeEnum) {
90         (match self {
91             SomeEnum::Three(x) => x,
92             SomeEnum::Four{x} => x,
93             _ => 0
94         }, self)
95     }
96 }
97
98 enum SimpleEnum {
99     One,
100     Two,
101     Three
102 }
103
104 impl SimpleEnum {
105     fn value(&self) -> i32 {
106         match *self {
107             SimpleEnum::One => 1,
108             SimpleEnum::Two => 2,
109             SimpleEnum::Three => 452,
110         }
111     }
112 }
113
114 fn main() {
115     let mut a = SomeEnum::Three(23);
116     let av = a.value();
117     let amv = (&mut a).mut_value();
118     let atv = a.take_value();
119     let b = SomeEnum::Four{x: 24};
120     let bv = b.value();
121     let c = SimpleEnum::Three;
122     let d = c.value();
123     let mut x = HasMethods::new();
124     x.incr();               // set breakpoint 1 here
125     (&mut x).incr();
126     let y = 23i32.whatever();
127     println!("{}", y);
128     let z = x.take();
129 }