@@ -34,27 +34,30 @@ mod test_write_then_drop;
3434#[ cfg( target_os = "fuchsia" ) ]
3535mod test_fuchsia_handles;
3636
37- use bytes:: { Buf , MutBuf } ;
37+ use bytes:: { Buf , BufMut , BytesMut } ;
3838use corcovado:: event:: Event ;
3939use corcovado:: { Events , Poll } ;
4040use std:: io:: { self , Read , Write } ;
4141use std:: time:: Duration ;
4242
4343pub trait TryRead {
44- fn try_read_buf < B : MutBuf > ( & mut self , buf : & mut B ) -> io:: Result < Option < usize > >
44+ fn try_read_buf ( & mut self , buf : & mut BytesMut ) -> io:: Result < Option < usize > >
4545 where
4646 Self : Sized ,
4747 {
48- // Reads the length of the slice supplied by buf.mut_bytes into the buffer
49- // This is not guaranteed to consume an entire datagram or segment.
50- // If your protocol is msg based (instead of continuous stream) you should
51- // ensure that your buffer is large enough to hold an entire segment (1532 bytes if not jumbo
52- // frames)
53- let res = self . try_read ( unsafe { buf. mut_bytes ( ) } ) ;
48+ let remaining = buf. capacity ( ) - buf. len ( ) ;
49+ if remaining == 0 {
50+ return Ok ( Some ( 0 ) ) ;
51+ }
52+ buf. reserve ( remaining) ;
53+ let dst = buf. chunk_mut ( ) ;
54+ let dst_slice =
55+ unsafe { std:: slice:: from_raw_parts_mut ( dst. as_mut_ptr ( ) , dst. len ( ) ) } ;
56+ let res = self . try_read ( dst_slice) ;
5457
5558 if let Ok ( Some ( cnt) ) = res {
5659 unsafe {
57- buf. advance ( cnt) ;
60+ buf. advance_mut ( cnt) ;
5861 }
5962 }
6063
@@ -69,7 +72,7 @@ pub trait TryWrite {
6972 where
7073 Self : Sized ,
7174 {
72- let res = self . try_write ( buf. bytes ( ) ) ;
75+ let res = self . try_write ( buf. chunk ( ) ) ;
7376
7477 if let Ok ( Some ( cnt) ) = res {
7578 buf. advance ( cnt) ;
0 commit comments