-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
165 lines (146 loc) · 4.21 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// This example is licensed under the CC0 license.
// https://creativecommons.org/share-your-work/public-domain/cc0
//
// This means that you can use the code from this example in your projects
// without any restrictions or attribution.
#![allow(unused_variables)]
// use `cargo run --features trace`
#[macro_use]
extern crate nolog;
type Fauna = Vec<Box<dyn Creature>>;
struct Planet<'a> {
name: &'a str,
fauna: Fauna,
}
struct Fish<'a> {
name: &'a str,
repr: &'a str,
speech: &'a str,
}
struct Rabbit<'a> {
name: &'a str,
repr: &'a str,
speech: &'a str,
#[allow(dead_code)]
reincarnations: u128,
}
trait Creature {
fn new() -> Self
where
Self: Sized;
fn repr(&self) -> String;
fn speech(&self) -> String;
fn name(&self) -> String;
}
impl Creature for Fish<'_> {
fn new() -> Self {
Self {
name: "Fish",
repr: "><>",
speech: "Yohooo-ho!",
}
}
fn repr(&self) -> String {
self.repr.into()
}
fn speech(&self) -> String {
self.speech.into()
}
fn name(&self) -> String {
self.name.into()
}
}
impl Creature for Rabbit<'_> {
fn new() -> Self {
Self {
name: "Rabbit",
repr: ">o.o<",
speech: "Mmmm!",
reincarnations: Default::default(),
}
}
fn repr(&self) -> String {
self.repr.into()
}
fn speech(&self) -> String {
self.speech.into()
}
fn name(&self) -> String {
self.name.into()
}
}
impl Drop for Planet<'_> {
fn drop(&mut self) {
crit!("The planet {} has been destroyed.", self.name);
}
}
impl<'a> Planet<'a> {
fn new(name: &'a str) -> Self {
crit!(r#"The planet "{name}" has been created."#);
trace!("{name}" => "Am I a planet?! Oh no...");
Self {
name,
fauna: Default::default(),
}
}
fn get_answer(&self) -> u32 {
let name = self.name;
debug!("{name}" => "{}?", 2*7);
info!("{name}" => "{}?", 3*7);
warn!("{name}" => "{}.. maybe..", 5*7);
debug!(
"Planet {name} thinks...";
->[2] "Planet {name} thinks...";
);
error!("{name}" => "{}!! Oh, yeaaaah!", 2*3*7);
crit!(->[8] "{name}" => "Where is everyone?..");
2 * 3 * 7
}
fn push(&mut self, creature: impl Creature + 'static) {
let repr = creature.repr();
let speech = creature.speech();
let name = creature.name();
let planet_name = self.name;
info!(->[12] "New {name} on planet {planet_name}.");
debug!(->[12] "{repr}" => "{name} says: {speech}");
self.fauna.push(Box::new(creature));
}
}
fn main() {
let (server, ip) = ("localhost", "127.0.0.1");
let server_check_result = "Ok";
debug!(->[_,1] "The simulation server started successfully.");
debug!(
"{server}" => "{ip}";
"Status" => ->[3] "{server_check_result}";
);
{
newline!();
let universe = [0; 3];
crit!("The Universe was created with a lifetime of {} days.", universe.len());
let planet_name = "Novus";
let mut planet = Planet::new(planet_name);
info!(r#"The calculation of the "Answer to the Ultimate Question" begins."#);
let answer = planet.get_answer();
crit!(->[_,1,1] "The answer is {answer}.");
warn!("Planet {planet_name} started watching TV.");
error!("Uncontrolled evolutionary processes have begun on the planet {planet_name}.");
for day in 1..(universe.len() + 1) {
if day == universe.len() {
warn!(->[6] "Today is day {day}, the last day of the Universe.");
for _ in 0..2 {
planet.push(Rabbit::new());
}
} else {
trace!(->[6] "Today is day {day}");
planet.push(Fish::new());
}
}
let mut population = vec![];
for creature in &planet.fauna {
population.push(creature.repr());
}
info!("The population of the planet {planet_name}: {population:?}.");
}
crit!("The End of the Universe.");
}