@@ -212,6 +212,34 @@ fn build_matrix(data: &[(i32, i32, f32)], all_zones: &[i32]) -> Vec<f32> {
212212 matrix
213213}
214214
215+ enum WriterType {
216+ Plain ( BufWriter < File > ) ,
217+ Gzip ( BufWriter < GzEncoder < File > > ) ,
218+ }
219+
220+ impl Write for WriterType {
221+ fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
222+ match self {
223+ WriterType :: Plain ( writer) => writer. write ( buf) ,
224+ WriterType :: Gzip ( writer) => writer. write ( buf) ,
225+ }
226+ }
227+
228+ fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
229+ match self {
230+ WriterType :: Plain ( writer) => writer. flush ( ) ,
231+ WriterType :: Gzip ( writer) => writer. flush ( ) ,
232+ }
233+ }
234+
235+ fn write_all ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < ( ) > {
236+ match self {
237+ WriterType :: Plain ( writer) => writer. write_all ( buf) ,
238+ WriterType :: Gzip ( writer) => writer. write_all ( buf) ,
239+ }
240+ }
241+ }
242+
215243/// Writes the MTX file in the specified format. If the output file name ends with `.gz`,
216244/// the file is written as a gzip-compressed file.
217245///
@@ -224,10 +252,10 @@ fn build_matrix(data: &[(i32, i32, f32)], all_zones: &[i32]) -> Vec<f32> {
224252/// This function will panic if it fails to create or write to the output file.
225253fn write_mtx_file ( output_file_name : & str , all_zones : & [ i32 ] , matrix : & [ f32 ] ) -> std:: io:: Result < ( ) > {
226254 let output_file = File :: create ( output_file_name) ?;
227- let mut writer: Box < dyn Write > = if output_file_name. ends_with ( ".gz" ) {
228- Box :: new ( BufWriter :: new ( GzEncoder :: new ( output_file, Compression :: default ( ) ) ) )
255+ let mut writer: WriterType = if output_file_name. ends_with ( ".gz" ) {
256+ WriterType :: Gzip ( BufWriter :: new ( GzEncoder :: new ( output_file, Compression :: default ( ) ) ) )
229257 } else {
230- Box :: new ( BufWriter :: new ( output_file) )
258+ WriterType :: Plain ( BufWriter :: new ( output_file) )
231259 } ;
232260
233261 let zone_count = all_zones. len ( ) as i32 ;
0 commit comments