@@ -100,7 +100,7 @@ impl BufferedIO {
100
100
101
101
#[ derive( Debug ) ]
102
102
struct PyStringIO {
103
- data : RefCell < Cursor < Vec < u8 > > > ,
103
+ buffer : RefCell < BufferedIO > ,
104
104
}
105
105
106
106
type PyStringIORef = PyRef < PyStringIO > ;
@@ -115,18 +115,16 @@ impl PyStringIORef {
115
115
//write string to underlying vector
116
116
fn write ( self , data : objstr:: PyStringRef , vm : & VirtualMachine ) -> PyResult {
117
117
let bytes = & data. value . clone ( ) . into_bytes ( ) ;
118
- let length = bytes. len ( ) ;
119
118
120
- let mut cursor = self . data . borrow_mut ( ) ;
121
- match cursor. write_all ( bytes) {
122
- Ok ( _) => Ok ( vm. ctx . new_int ( length) ) ,
123
- Err ( _) => Err ( vm. new_type_error ( "Error Writing String" . to_string ( ) ) ) ,
119
+ match self . buffer . borrow_mut ( ) . write ( bytes. to_vec ( ) ) {
120
+ Some ( value) => Ok ( vm. ctx . new_int ( value) ) ,
121
+ None => Err ( vm. new_type_error ( "Error Writing String" . to_string ( ) ) ) ,
124
122
}
125
123
}
126
124
127
125
//return the entire contents of the underlying
128
126
fn getvalue ( self , vm : & VirtualMachine ) -> PyResult {
129
- match String :: from_utf8 ( self . data . borrow ( ) . clone ( ) . into_inner ( ) ) {
127
+ match String :: from_utf8 ( self . buffer . borrow ( ) . getvalue ( ) ) {
130
128
Ok ( result) => Ok ( vm. ctx . new_str ( result) ) ,
131
129
Err ( _) => Err ( vm. new_value_error ( "Error Retrieving Value" . to_string ( ) ) ) ,
132
130
}
@@ -135,45 +133,25 @@ impl PyStringIORef {
135
133
//skip to the jth position
136
134
fn seek ( self , offset : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
137
135
let position = objint:: get_value ( & offset) . to_u64 ( ) . unwrap ( ) ;
138
- if let Err ( _) = self
139
- . data
140
- . borrow_mut ( )
141
- . seek ( SeekFrom :: Start ( position. clone ( ) ) )
142
- {
143
- return Err ( vm. new_value_error ( "Error Retrieving Value" . to_string ( ) ) ) ;
136
+ match self . buffer . borrow_mut ( ) . seek ( position) {
137
+ Some ( value) => Ok ( vm. ctx . new_int ( value) ) ,
138
+ None => Err ( vm. new_value_error ( "Error Performing Operation" . to_string ( ) ) ) ,
144
139
}
145
-
146
- Ok ( vm. ctx . new_int ( position) )
147
140
}
148
141
149
142
//Read k bytes from the object and return.
150
143
//If k is undefined || k == -1, then we read all bytes until the end of the file.
151
144
//This also increments the stream position by the value of k
152
145
fn read ( self , bytes : OptionalArg < Option < PyObjectRef > > , vm : & VirtualMachine ) -> PyResult {
153
- let mut buffer = String :: new ( ) ;
154
-
155
- match bytes {
156
- OptionalArg :: Present ( Some ( ref integer) ) => {
157
- let k = objint:: get_value ( integer) . to_u64 ( ) . unwrap ( ) ;
158
- let mut handle = self . data . borrow ( ) . clone ( ) . take ( k) ;
159
-
160
- //read bytes into string
161
- if let Err ( _) = handle. read_to_string ( & mut buffer) {
162
- return Err ( vm. new_value_error ( "Error Retrieving Value" . to_string ( ) ) ) ;
163
- }
164
-
165
- //the take above consumes the struct value
166
- //we add this back in with the takes into_inner method
167
- self . data . replace ( handle. into_inner ( ) ) ;
168
- }
169
- _ => {
170
- if let Err ( _) = self . data . borrow_mut ( ) . read_to_string ( & mut buffer) {
171
- return Err ( vm. new_value_error ( "Error Retrieving Value" . to_string ( ) ) ) ;
172
- }
173
- }
146
+ let data = match self . buffer . borrow_mut ( ) . read ( byte_count ( bytes) ) {
147
+ Some ( value) => value,
148
+ None => Vec :: new ( ) ,
174
149
} ;
175
150
176
- Ok ( vm. ctx . new_str ( buffer) )
151
+ match String :: from_utf8 ( data) {
152
+ Ok ( value) => Ok ( vm. ctx . new_str ( value) ) ,
153
+ Err ( _) => Err ( vm. new_value_error ( "Error Retrieving Value" . to_string ( ) ) ) ,
154
+ }
177
155
}
178
156
}
179
157
@@ -188,7 +166,7 @@ fn string_io_new(
188
166
} ;
189
167
190
168
PyStringIO {
191
- data : RefCell :: new ( Cursor :: new ( raw_string. into_bytes ( ) ) ) ,
169
+ buffer : RefCell :: new ( BufferedIO :: new ( Cursor :: new ( raw_string. into_bytes ( ) ) ) ) ,
192
170
}
193
171
. into_ref_with_type ( vm, cls)
194
172
}
@@ -778,11 +756,22 @@ mod tests {
778
756
#[ test]
779
757
fn test_buffered_seek ( ) {
780
758
let data = vec ! [ 1 , 2 , 3 , 4 ] ;
781
- let offset : u64 = 2 ;
759
+ let count : u64 = 2 ;
782
760
let mut buffered = BufferedIO {
783
761
cursor : Cursor :: new ( data. clone ( ) ) ,
784
762
} ;
785
763
786
- assert_eq ! ( buffered. seek( offset. clone( ) ) . unwrap( ) , offset) ;
764
+ assert_eq ! ( buffered. seek( count. clone( ) ) . unwrap( ) , count) ;
765
+ assert_eq ! ( buffered. read( count. clone( ) as i64 ) . unwrap( ) , vec![ 3 , 4 ] ) ;
766
+ }
767
+
768
+ #[ test]
769
+ fn test_buffered_value ( ) {
770
+ let data = vec ! [ 1 , 2 , 3 , 4 ] ;
771
+ let buffered = BufferedIO {
772
+ cursor : Cursor :: new ( data. clone ( ) ) ,
773
+ } ;
774
+
775
+ assert_eq ! ( buffered. getvalue( ) , data) ;
787
776
}
788
777
}
0 commit comments