@@ -39,6 +39,61 @@ pin_project! {
3939 }
4040}
4141
42+ impl < W > BufWriter < W > {
43+ delegate_access_inner ! ( inner, W , ( ) ) ;
44+
45+ /// Returns a reference to the internally buffered data.
46+ pub fn buffer ( & self ) -> & [ u8 ] {
47+ & self . buf
48+ }
49+
50+ /// Capacity of `buf`. how many chars can be held in buffer
51+ pub ( super ) fn capacity ( & self ) -> usize {
52+ self . buf . capacity ( )
53+ }
54+
55+ /// Remaining number of bytes to reach `buf` 's capacity
56+ #[ inline]
57+ pub ( super ) fn spare_capacity ( & self ) -> usize {
58+ self . buf . capacity ( ) - self . buf . len ( )
59+ }
60+
61+ /// Write byte slice directly into `self.buf`
62+ ///
63+ /// Based on `std::io::BufWriter`
64+ #[ inline]
65+ unsafe fn write_to_buffer_unchecked ( self : Pin < & mut Self > , buf : & [ u8 ] ) {
66+ debug_assert ! ( buf. len( ) <= self . spare_capacity( ) ) ;
67+ let this = self . project ( ) ;
68+ let old_len = this. buf . len ( ) ;
69+ let buf_len = buf. len ( ) ;
70+ let src = buf. as_ptr ( ) ;
71+ unsafe {
72+ let dst = this. buf . as_mut_ptr ( ) . add ( old_len) ;
73+ ptr:: copy_nonoverlapping ( src, dst, buf_len) ;
74+ this. buf . set_len ( old_len + buf_len) ;
75+ }
76+ }
77+
78+ /// Write a byte slice directly into buffer
79+ ///
80+ /// Will truncate the number of bytes written to `spare_capacity()` so you want to
81+ /// calculate the size of your slice to avoid losing bytes
82+ ///
83+ /// Based on `std::io::BufWriter`
84+ pub ( super ) fn write_to_buf ( self : Pin < & mut Self > , buf : & [ u8 ] ) -> usize {
85+ let available = self . spare_capacity ( ) ;
86+ let amt_to_buffer = available. min ( buf. len ( ) ) ;
87+
88+ // SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
89+ unsafe {
90+ self . write_to_buffer_unchecked ( & buf[ ..amt_to_buffer] ) ;
91+ }
92+
93+ amt_to_buffer
94+ }
95+ }
96+
4297impl < W : AsyncWrite > BufWriter < W > {
4398 /// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
4499 /// but may change in the future.
@@ -79,59 +134,6 @@ impl<W: AsyncWrite> BufWriter<W> {
79134 Poll :: Ready ( ret)
80135 }
81136
82- delegate_access_inner ! ( inner, W , ( ) ) ;
83-
84- /// Returns a reference to the internally buffered data.
85- pub fn buffer ( & self ) -> & [ u8 ] {
86- & self . buf
87- }
88-
89- /// Capacity of `buf`. how many chars can be held in buffer
90- pub ( super ) fn capacity ( & self ) -> usize {
91- self . buf . capacity ( )
92- }
93-
94- /// Remaining number of bytes to reach `buf` 's capacity
95- #[ inline]
96- pub ( super ) fn spare_capacity ( & self ) -> usize {
97- self . buf . capacity ( ) - self . buf . len ( )
98- }
99-
100- /// Write a byte slice directly into buffer
101- ///
102- /// Will truncate the number of bytes written to `spare_capacity()` so you want to
103- /// calculate the size of your slice to avoid losing bytes
104- ///
105- /// Based on `std::io::BufWriter`
106- pub ( super ) fn write_to_buf ( self : Pin < & mut Self > , buf : & [ u8 ] ) -> usize {
107- let available = self . spare_capacity ( ) ;
108- let amt_to_buffer = available. min ( buf. len ( ) ) ;
109-
110- // SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
111- unsafe {
112- self . write_to_buffer_unchecked ( & buf[ ..amt_to_buffer] ) ;
113- }
114-
115- amt_to_buffer
116- }
117-
118- /// Write byte slice directly into `self.buf`
119- ///
120- /// Based on `std::io::BufWriter`
121- #[ inline]
122- unsafe fn write_to_buffer_unchecked ( self : Pin < & mut Self > , buf : & [ u8 ] ) {
123- debug_assert ! ( buf. len( ) <= self . spare_capacity( ) ) ;
124- let this = self . project ( ) ;
125- let old_len = this. buf . len ( ) ;
126- let buf_len = buf. len ( ) ;
127- let src = buf. as_ptr ( ) ;
128- unsafe {
129- let dst = this. buf . as_mut_ptr ( ) . add ( old_len) ;
130- ptr:: copy_nonoverlapping ( src, dst, buf_len) ;
131- this. buf . set_len ( old_len + buf_len) ;
132- }
133- }
134-
135137 /// Write directly using `inner`, bypassing buffering
136138 pub ( super ) fn inner_poll_write (
137139 self : Pin < & mut Self > ,
0 commit comments