@@ -100,7 +100,7 @@ impl BufferedIO {
100100
101101#[ derive( Debug ) ]
102102struct PyStringIO {
103- data : RefCell < Cursor < Vec < u8 > > > ,
103+ buffer : RefCell < BufferedIO > ,
104104}
105105
106106type PyStringIORef = PyRef < PyStringIO > ;
@@ -115,18 +115,16 @@ impl PyStringIORef {
115115 //write string to underlying vector
116116 fn write ( self , data : objstr:: PyStringRef , vm : & VirtualMachine ) -> PyResult {
117117 let bytes = & data. value . clone ( ) . into_bytes ( ) ;
118- let length = bytes. len ( ) ;
119118
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 ( ) ) ) ,
124122 }
125123 }
126124
127125 //return the entire contents of the underlying
128126 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 ( ) ) {
130128 Ok ( result) => Ok ( vm. ctx . new_str ( result) ) ,
131129 Err ( _) => Err ( vm. new_value_error ( "Error Retrieving Value" . to_string ( ) ) ) ,
132130 }
@@ -135,45 +133,25 @@ impl PyStringIORef {
135133 //skip to the jth position
136134 fn seek ( self , offset : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
137135 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 ( ) ) ) ,
144139 }
145-
146- Ok ( vm. ctx . new_int ( position) )
147140 }
148141
149142 //Read k bytes from the object and return.
150143 //If k is undefined || k == -1, then we read all bytes until the end of the file.
151144 //This also increments the stream position by the value of k
152145 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 ( ) ,
174149 } ;
175150
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+ }
177155 }
178156}
179157
@@ -188,7 +166,7 @@ fn string_io_new(
188166 } ;
189167
190168 PyStringIO {
191- data : RefCell :: new ( Cursor :: new ( raw_string. into_bytes ( ) ) ) ,
169+ buffer : RefCell :: new ( BufferedIO :: new ( Cursor :: new ( raw_string. into_bytes ( ) ) ) ) ,
192170 }
193171 . into_ref_with_type ( vm, cls)
194172}
@@ -778,11 +756,22 @@ mod tests {
778756 #[ test]
779757 fn test_buffered_seek ( ) {
780758 let data = vec ! [ 1 , 2 , 3 , 4 ] ;
781- let offset : u64 = 2 ;
759+ let count : u64 = 2 ;
782760 let mut buffered = BufferedIO {
783761 cursor : Cursor :: new ( data. clone ( ) ) ,
784762 } ;
785763
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) ;
787776 }
788777}
0 commit comments