11use itertools:: Itertools ;
2- use std:: cell:: RefCell ;
32use std:: cmp;
43use std:: io:: { self , BufRead , Write } ;
54use std:: ops:: Range ;
6- use std:: rc:: Rc ;
7- use std:: vec:: Vec ;
85
9- /// Write minimap to output
6+ /// Write minimap to the writer.
107pub fn write (
11- output : Rc < RefCell < dyn Write > > ,
12- reader : Box < dyn BufRead > ,
8+ mut writer : impl Write ,
9+ reader : impl BufRead ,
1310 hscale : f64 ,
1411 vscale : f64 ,
1512 padding : Option < usize > ,
@@ -36,12 +33,12 @@ pub fn write(
3633 }
3734 frame. iter_mut ( ) . skip ( chunk_size) . for_each ( |row| * row = 0 ..0 ) ;
3835 scale_frame ( & mut frame, hscale) ;
39- write_frame ( output . clone ( ) , & frame, padding) ?;
36+ write_frame ( & mut writer , & frame, padding) ?;
4037 }
4138 Ok ( ( ) )
4239}
4340
44- /// Print minimap to stdout
41+ /// Print minimap to the stdout.
4542///
4643/// # Examples
4744///
@@ -51,14 +48,14 @@ pub fn write(
5148/// use std::io;
5249/// use std::io::BufReader;
5350///
54- /// let reader = Box::new(BufReader::new( io::stdin()) );
55- /// code_minimap::print(reader , 1.0, 1.0, None).unwrap();
51+ /// let stdin = io::stdin();
52+ /// code_minimap::printstd(stdin.lock() , 1.0, 1.0, None).unwrap();
5653/// ```
57- pub fn print ( reader : Box < dyn BufRead > , hscale : f64 , vscale : f64 , padding : Option < usize > ) -> io:: Result < ( ) > {
58- write ( Rc :: new ( RefCell :: new ( io:: stdout ( ) ) ) , reader, hscale, vscale, padding)
54+ pub fn printstd ( reader : impl BufRead , hscale : f64 , vscale : f64 , padding : Option < usize > ) -> io:: Result < ( ) > {
55+ write ( io:: stdout ( ) , reader, hscale, vscale, padding)
5956}
6057
61- /// Write minimap to string
58+ /// Write minimap to a string.
6259///
6360/// # Examples
6461///
@@ -68,23 +65,17 @@ pub fn print(reader: Box<dyn BufRead>, hscale: f64, vscale: f64, padding: Option
6865/// use std::io;
6966/// use std::io::BufReader;
7067///
71- /// let reader = Box::new(BufReader::new( io::stdin()) );
72- /// let s = code_minimap::write_to_string(reader , 1.0, 1.0, None).unwrap();
68+ /// let stdin = io::stdin();
69+ /// let s = code_minimap::write_to_string(stdin.lock() , 1.0, 1.0, None).unwrap();
7370/// print!("{}", s);
7471/// ```
75- pub fn write_to_string (
76- reader : Box < dyn BufRead > ,
77- hscale : f64 ,
78- vscale : f64 ,
79- padding : Option < usize > ,
80- ) -> io:: Result < String > {
81- let buf = Rc :: new ( RefCell :: new ( Vec :: new ( ) ) ) ;
82- write ( buf. clone ( ) , reader, hscale, vscale, padding) ?;
83- let buf = Rc :: try_unwrap ( buf) . unwrap ( ) . into_inner ( ) ;
72+ pub fn write_to_string ( reader : impl BufRead , hscale : f64 , vscale : f64 , padding : Option < usize > ) -> io:: Result < String > {
73+ let mut buf = Vec :: new ( ) ;
74+ write ( & mut buf, reader, hscale, vscale, padding) ?;
8475 Ok ( String :: from_utf8 ( buf) . unwrap ( ) )
8576}
8677
87- fn write_frame ( output : Rc < RefCell < dyn Write > > , frame : & [ Range < usize > ] , padding : Option < usize > ) -> std:: io:: Result < ( ) > {
78+ fn write_frame ( mut output : impl Write , frame : & [ Range < usize > ] , padding : Option < usize > ) -> std:: io:: Result < ( ) > {
8879 let idx = |pos| {
8980 frame
9081 . iter ( )
@@ -97,8 +88,8 @@ fn write_frame(output: Rc<RefCell<dyn Write>>, frame: &[Range<usize>], padding:
9788 . map ( |i| BRAILLE_MATRIX [ ( idx ( i) ) + ( idx ( i + 1 ) << 4 ) ] )
9889 . collect ( ) ;
9990 match padding {
100- Some ( padding) => writeln ! ( output. borrow_mut ( ) , "{0:<1$}" , line, padding) ,
101- None => writeln ! ( output. borrow_mut ( ) , "{}" , line) ,
91+ Some ( padding) => writeln ! ( output, "{0:<1$}" , line, padding) ,
92+ None => writeln ! ( output, "{}" , line) ,
10293 }
10394}
10495
@@ -147,7 +138,7 @@ mod test {
147138 case( " a b c\n d efg \n h i\n jk" , "⢐⡛⠿⠭" )
148139 ) ]
149140 fn test_write_to_string ( input : & ' static str , expected : & str ) {
150- let actual = write_to_string ( Box :: new ( input. as_bytes ( ) ) , 1.0 , 1.0 , None ) . unwrap ( ) ;
141+ let actual = write_to_string ( input. as_bytes ( ) , 1.0 , 1.0 , None ) . unwrap ( ) ;
151142 assert_eq ! ( expected, actual. trim( ) ) ;
152143 }
153144}
0 commit comments