瀏覽代碼

Ran rustfmt on various files

Lorenz Bung 7 年之前
父節點
當前提交
c88a64567d
共有 3 個檔案被更改,包括 62 行新增54 行删除
  1. 4
    1
      hw7/task1/src/hash256.rs
  2. 25
    17
      hw7/task1/src/main.rs
  3. 33
    36
      hw7/task2/src/lib.rs

+ 4
- 1
hw7/task1/src/hash256.rs 查看文件

@@ -24,7 +24,10 @@ pub fn verify_product(base: usize, number: usize, difficulty: &String) -> Option
24 24
     let hash = Sha256::hash(bytes).hex();
25 25
 
26 26
     if hash.ends_with(difficulty) {
27
-        return Some(Solution{ number: number, hash: hash,});
27
+        return Some(Solution {
28
+            number: number,
29
+            hash: hash,
30
+        });
28 31
     }
29 32
 
30 33
     None

+ 25
- 17
hw7/task1/src/main.rs 查看文件

@@ -14,7 +14,7 @@ pub fn main() {
14 14
     let diff = matches.value_of("difficulty").unwrap_or("1");
15 15
     let mut time_measurement = false;
16 16
 
17
-    if diff.chars().any( |c| !c.is_digit(16)) {
17
+    if diff.chars().any(|c| !c.is_digit(16)) {
18 18
         println!("Difficulty is not hexadecimal.");
19 19
         process::exit(1)
20 20
     }
@@ -37,7 +37,7 @@ pub fn main() {
37 37
                 if let Some(x) = hash256::verify_product(b, n, &diff.to_string()) {
38 38
                     let end = get_time();
39 39
                     println!("Number: {} --> hash: {}", x.number, x.hash);
40
-                    if time_measurement{
40
+                    if time_measurement {
41 41
                         let diff = end - start;
42 42
                         let s = diff.num_seconds();
43 43
                         let ms = diff.num_milliseconds();
@@ -60,19 +60,27 @@ fn create_app<'a, 'b>() -> App<'a, 'b> {
60 60
     App::new("Hash256")
61 61
         .version("1.0")
62 62
         .author("Lorenz Bung & Joshua Rutschmann")
63
-        .about("Calculates the Hashvalue of the given base, number and difficulty.")
64
-        .arg(Arg::with_name("base")
65
-            .value_name("base")
66
-            .help("The base of the hash to be calculated on.")
67
-            .takes_value(true)
68
-            .required(true))
69
-        .arg(Arg::with_name("difficulty")
70
-            .value_name("difficulty")
71
-            .help("The difficulty of the calculated hash.")
72
-            .takes_value(true)
73
-            .required(true))
74
-        .subcommand(SubCommand::with_name("timings")
75
-            .about("controls timing features")
76
-            .version("1.0")
77
-            .author("Lorenz Bung & Joshua Rutschmann"))
63
+        .about(
64
+            "Calculates the Hashvalue of the given base, number and difficulty.",
65
+        )
66
+        .arg(
67
+            Arg::with_name("base")
68
+                .value_name("base")
69
+                .help("The base of the hash to be calculated on.")
70
+                .takes_value(true)
71
+                .required(true),
72
+        )
73
+        .arg(
74
+            Arg::with_name("difficulty")
75
+                .value_name("difficulty")
76
+                .help("The difficulty of the calculated hash.")
77
+                .takes_value(true)
78
+                .required(true),
79
+        )
80
+        .subcommand(
81
+            SubCommand::with_name("timings")
82
+                .about("controls timing features")
83
+                .version("1.0")
84
+                .author("Lorenz Bung & Joshua Rutschmann"),
85
+        )
78 86
 }

+ 33
- 36
hw7/task2/src/lib.rs 查看文件

@@ -29,9 +29,7 @@ impl FromStr for Command {
29 29
         match parts.next() {
30 30
             Some("exit") => Ok(Command::Exit),
31 31
             Some("cd") => Ok(Command::parse_cd(parts.next())),
32
-            Some(_) => {
33
-                Ok(Command::Program(s.to_string()))
34
-            }
32
+            Some(_) => Ok(Command::Program(s.to_string())),
35 33
             None => Ok(Command::Empty),
36 34
         }
37 35
     }
@@ -132,7 +130,7 @@ impl<R: BufRead, W: Write> Shell<R, W> {
132 130
     fn run(&mut self, command: Command) -> Result<(), ErrorType> {
133 131
 
134 132
         match command {
135
-            Command::Empty => {},
133
+            Command::Empty => {}
136 134
             Command::Program(cmd) => {
137 135
 
138 136
                 let commands = cmd.split('|');
@@ -141,44 +139,44 @@ impl<R: BufRead, W: Write> Shell<R, W> {
141 139
                 let needs_pipe = commands_vec.len() == 2;
142 140
 
143 141
 
144
-                    let fi = fork();
145
-                    match fi {
146
-                        Ok(Parent { child: _child }) => {
147
-                            let _ = wait();
142
+                let fi = fork();
143
+                match fi {
144
+                    Ok(Parent { child: _child }) => {
145
+                        let _ = wait();
148 146
 
149
-                        }
150
-                        Ok(Child) => {
151
-                            if let Ok((reader, writer)) = pipe() {
152
-                                let sec = fork();
153
-                                match sec {
154
-                                    Ok(Parent { child: _child }) => {
155
-                                        let _ = wait();
156
-                                        if let Some(second) = commands_vec.get(1) {
157
-                                            match close(writer) {
158
-                                                Ok(_) => {},
159
-                                                Err(_) => return Err(ErrorType::BrokenPipeError),
160
-                                            }
161
-                                            dup2(reader, 0).unwrap();
162
-                                            self.execute(second);
147
+                    }
148
+                    Ok(Child) => {
149
+                        if let Ok((reader, writer)) = pipe() {
150
+                            let sec = fork();
151
+                            match sec {
152
+                                Ok(Parent { child: _child }) => {
153
+                                    let _ = wait();
154
+                                    if let Some(second) = commands_vec.get(1) {
155
+                                        match close(writer) {
156
+                                            Ok(_) => {}
157
+                                            Err(_) => return Err(ErrorType::BrokenPipeError),
163 158
                                         }
159
+                                        dup2(reader, 0).unwrap();
160
+                                        self.execute(second);
164 161
                                     }
165
-                                    Ok(Child) => {
166
-                                        if let Some(first) = commands_vec.get(0) {
167
-                                            if needs_pipe {
168
-                                                close(reader).unwrap();
169
-                                                dup2(writer, 1).unwrap();
170
-                                            }
171
-                                            self.execute(first);
162
+                                }
163
+                                Ok(Child) => {
164
+                                    if let Some(first) = commands_vec.get(0) {
165
+                                        if needs_pipe {
166
+                                            close(reader).unwrap();
167
+                                            dup2(writer, 1).unwrap();
172 168
                                         }
169
+                                        self.execute(first);
173 170
                                     }
174
-                                    Err(_) => return Err(ErrorType::ForkError),
175 171
                                 }
172
+                                Err(_) => return Err(ErrorType::ForkError),
176 173
                             }
177 174
                         }
178
-                        Err(_) => return Err(ErrorType::ForkError),
179 175
                     }
176
+                    Err(_) => return Err(ErrorType::ForkError),
177
+                }
180 178
 
181
-            },
179
+            }
182 180
             Command::Exit => self.should_exit = true,
183 181
             Command::Cd(_) => {
184 182
                 command.exec_cd();
@@ -187,12 +185,11 @@ impl<R: BufRead, W: Write> Shell<R, W> {
187 185
         Ok(())
188 186
     }
189 187
 
190
-    fn execute(&self, cmd: &str){
191
-        let parts:Vec<&str> = cmd.split_whitespace().collect();
192
-        let args:Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
188
+    fn execute(&self, cmd: &str) {
189
+        let parts: Vec<&str> = cmd.split_whitespace().collect();
190
+        let args: Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
193 191
         let t = args.into_boxed_slice();
194 192
 
195 193
         execvp(&t[0], &t).unwrap();
196 194
     }
197 195
 }
198
-

Loading…
取消
儲存