ソースを参照

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
 use std::process;
1
 use std::process;
2
 use shell::Shell;
2
 use shell::Shell;
3
+use shell::{R, W};
3
 use std::io;
4
 use std::io;
4
 
5
 
5
 mod command;
6
 mod command;
8
 
9
 
9
 fn main() {
10
 fn main() {
10
     let stdin = io::stdin();
11
     let stdin = io::stdin();
12
+    let input = R(stdin.lock());
11
 
13
 
12
     let stdout = io::stdout();
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
     match s.start() {
18
     match s.start() {
16
         Ok(_) => process::exit(0),
19
         Ok(_) => process::exit(0),
17
         Err(_) => process::exit(1),
20
         Err(_) => process::exit(1),

+ 34
- 3
hw6/task2/src/shell.rs ファイルの表示

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
 use command::*;
4
 use command::*;
4
 use std::str::FromStr;
5
 use std::str::FromStr;
5
 use std::env;
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
     pub reader: R,
12
     pub reader: R,
9
     pub writer: W,
13
     pub writer: W,
10
     pub should_exit: bool,
14
     pub should_exit: bool,
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
+

読み込み中…
キャンセル
保存