Преглед изворни кода

Finished task2. Implemented BufRead and Write for R and W.

themultiplexer пре 8 година
родитељ
комит
ab81ffc485
2 измењених фајлова са 38 додато и 4 уклоњено
  1. 4
    1
      hw6/task2/src/main.rs
  2. 34
    3
      hw6/task2/src/shell.rs

+ 4
- 1
hw6/task2/src/main.rs Прегледај датотеку

@@ -1,5 +1,6 @@
1 1
 use std::process;
2 2
 use shell::Shell;
3
+use shell::{R, W};
3 4
 use std::io;
4 5
 
5 6
 mod command;
@@ -8,10 +9,12 @@ mod shell;
8 9
 
9 10
 fn main() {
10 11
     let stdin = io::stdin();
12
+    let input = R(stdin.lock());
11 13
 
12 14
     let stdout = io::stdout();
15
+    let output = W(stdout.lock());
13 16
 
14
-    let mut s = Shell::new(stdin.lock(), stdout.lock(), "schell".to_string());
17
+    let mut s = Shell::new(input, output, "schell".to_string());
15 18
     match s.start() {
16 19
         Ok(_) => process::exit(0),
17 20
         Err(_) => process::exit(1),

+ 34
- 3
hw6/task2/src/shell.rs Прегледај датотеку

@@ -1,10 +1,14 @@
1
-use std::io::BufRead;
2
-use std::io::Write;
1
+use std::io::{BufRead,Write,Read, StdinLock, StdoutLock};
2
+use std::io;
3
+
3 4
 use command::*;
4 5
 use std::str::FromStr;
5 6
 use std::env;
6 7
 
7
-pub struct Shell<R:BufRead, W:Write> {
8
+pub struct R<'a>(pub StdinLock<'a>);
9
+pub struct W<'a>(pub StdoutLock<'a>);
10
+
11
+pub struct Shell<R, W> {
8 12
     pub reader: R,
9 13
     pub writer: W,
10 14
     pub should_exit: bool,
@@ -74,3 +78,30 @@ impl <R:BufRead, W:Write> Shell<R, W> {
74 78
 
75 79
 }
76 80
 
81
+impl <'a>Read for R<'a> {
82
+    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
83
+        self.0.read(buf)
84
+    }
85
+}
86
+
87
+impl <'a>BufRead for R<'a> {
88
+
89
+    fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
90
+        self.0.fill_buf()
91
+    }
92
+
93
+    fn consume(&mut self, amt: usize) {
94
+        self.0.consume(amt)
95
+    }
96
+}
97
+
98
+impl <'a>Write for W<'a> {
99
+    fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
100
+        self.0.write(buf)
101
+    }
102
+    
103
+    fn flush(&mut self) -> Result<(),  io::Error> {
104
+        self.0.flush()
105
+    }
106
+}
107
+

Loading…
Откажи
Сачувај