@@ -971,7 +971,7 @@ impl<'a, W: Write> Write for LineWriterShim<'a, W> {
971
971
return match bufs. iter ( ) . find ( |buf| !buf. is_empty ( ) ) {
972
972
Some ( buf) => self . write ( buf) ,
973
973
None => Ok ( 0 ) ,
974
- }
974
+ } ;
975
975
}
976
976
977
977
// Find the buffer containing the last newline
@@ -1321,7 +1321,11 @@ mod tests {
1321
1321
1322
1322
impl Read for ShortReader {
1323
1323
fn read ( & mut self , _: & mut [ u8 ] ) -> io:: Result < usize > {
1324
- if self . lengths . is_empty ( ) { Ok ( 0 ) } else { Ok ( self . lengths . remove ( 0 ) ) }
1324
+ if self . lengths . is_empty ( ) {
1325
+ Ok ( 0 )
1326
+ } else {
1327
+ Ok ( self . lengths . remove ( 0 ) )
1328
+ }
1325
1329
}
1326
1330
}
1327
1331
@@ -1742,27 +1746,48 @@ mod tests {
1742
1746
b. iter ( || BufWriter :: new ( io:: sink ( ) ) ) ;
1743
1747
}
1744
1748
1745
- struct AcceptOneThenFail {
1746
- written : bool ,
1749
+ #[ derive( Default , Clone ) ]
1750
+ struct ProgrammableSink {
1751
+ // Writes append to this slice
1752
+ buffer : Vec < u8 > ,
1753
+
1754
+ // Flushes set this flag
1747
1755
flushed : bool ,
1756
+
1757
+ // If true, writes & flushes will always be an error
1758
+ return_error : bool ,
1759
+
1760
+ // If set, only up to this number of bytes will be written in a single
1761
+ // call to `write`
1762
+ accept_prefix : Option < usize > ,
1763
+
1764
+ // If set, counts down with each write, and writes return an error
1765
+ // when it hits 0
1766
+ max_writes : Option < usize > ,
1748
1767
}
1749
1768
1750
- impl Write for AcceptOneThenFail {
1769
+ impl Write for ProgrammableSink {
1751
1770
fn write ( & mut self , data : & [ u8 ] ) -> io:: Result < usize > {
1752
- if !self . written {
1753
- assert_eq ! ( data, b"a\n b\n " ) ;
1754
- self . written = true ;
1755
- Ok ( data. len ( ) )
1771
+ if self . return_error {
1772
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , "test" ) )
1756
1773
} else {
1757
- Err ( io:: Error :: new ( io:: ErrorKind :: NotFound , "test" ) )
1774
+ let len = match self . accept_prefix {
1775
+ None => data. len ( ) ,
1776
+ Some ( prefix) => prefix. min ( prefix) ,
1777
+ } ;
1778
+ let data = & data[ ..len] ;
1779
+ self . buffer . extend_from_slice ( data) ;
1780
+ Ok ( len)
1758
1781
}
1759
1782
}
1760
1783
1761
1784
fn flush ( & mut self ) -> io:: Result < ( ) > {
1762
- assert ! ( self . written) ;
1763
- assert ! ( !self . flushed) ;
1764
- self . flushed = true ;
1765
- Err ( io:: Error :: new ( io:: ErrorKind :: Other , "test" ) )
1785
+ if self . return_error {
1786
+ Err ( io:: Error :: new ( io:: ErrorKind :: Other , "test" ) )
1787
+ } else {
1788
+ self . flushed = true ;
1789
+ Ok ( ( ) )
1790
+ }
1766
1791
}
1767
1792
}
1768
1793
@@ -1777,15 +1802,7 @@ mod tests {
1777
1802
/// Regression test for #37807
1778
1803
#[ test]
1779
1804
fn erroneous_flush_retried ( ) {
1780
- let a = AcceptOneThenFail { written : false , flushed : false } ;
1781
-
1782
- let mut l = LineWriter :: new ( a) ;
1783
- assert_eq ! ( l. write( b"a\n b\n a" ) . unwrap( ) , 4 ) ;
1784
- assert ! ( l. get_ref( ) . written) ;
1785
- assert ! ( l. get_ref( ) . flushed) ;
1786
- l. get_mut ( ) . flushed = false ;
1787
-
1788
- assert_eq ! ( l. write( b"a" ) . unwrap_err( ) . kind( ) , io:: ErrorKind :: Other )
1805
+ todo ! ( )
1789
1806
}
1790
1807
1791
1808
#[ test]
@@ -1895,4 +1912,50 @@ mod tests {
1895
1912
io:: Error :: new ( io:: ErrorKind :: Other , "x" )
1896
1913
}
1897
1914
}
1915
+
1916
+ /// Test that, given this input:
1917
+ ///
1918
+ /// Line 1\n
1919
+ /// Line 2\n
1920
+ /// Line 3\n
1921
+ /// Line 4
1922
+ ///
1923
+ /// And given a result that only writes to midway through Line 2
1924
+ ///
1925
+ /// That only up to the end of Line 3 is buffered
1926
+ ///
1927
+ /// This behavior is desirable because it prevents flushing partial lines
1928
+ #[ test]
1929
+ fn test_partial_write_buffers_line ( ) {
1930
+ todo ! ( )
1931
+ }
1932
+
1933
+ /// Test that, given this input:
1934
+ ///
1935
+ /// Line 1\n
1936
+ /// Line 2\n
1937
+ /// Line 3
1938
+ ///
1939
+ /// And given that the full write of lines 1 and 2 was successful
1940
+ /// That data up to Line 3 is buffered
1941
+ #[ test]
1942
+ fn test_partial_line_buffered_after_line_write ( ) {
1943
+ todo ! ( )
1944
+ }
1945
+
1946
+ /// Test that, given a partial line that exceeds the length of
1947
+ /// LineBuffer's buffer (that is, without a trailing newline), that that
1948
+ /// line is written to the inner writer
1949
+ #[ test]
1950
+ fn test_long_line_flushed ( ) {
1951
+ todo ! ( )
1952
+ }
1953
+
1954
+ /// Test that, given a very long partial line *after* successfully
1955
+ /// flushing a complete line, that that line is buffered unconditionally,
1956
+ /// and no additional writes take place
1957
+ #[ test]
1958
+ fn test_long_tail_not_flushed ( ) {
1959
+ todo ! ( )
1960
+ }
1898
1961
}
0 commit comments