diff --git a/Cargo.lock b/Cargo.lock index 3fbeef30..9e08e8aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -534,7 +534,7 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfd1e3f8955a5d7de9fab72fc8373fade9fb8a703968cb200ae3dc6cf08e185a" dependencies = [ - "bytes 1.11.1", + "bytes", "cfg_aliases", ] @@ -588,12 +588,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" -[[package]] -name = "bytes" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c129aff112dcc562970abb69e2508b40850dd24c274761bb50fb8a0067ba6c27" - [[package]] name = "bytes" version = "1.11.1" @@ -818,7 +812,7 @@ version = "4.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" dependencies = [ - "bytes 1.11.1", + "bytes", "memchr", ] @@ -887,7 +881,7 @@ dependencies = [ name = "corcovado" version = "0.3.0" dependencies = [ - "bytes 0.3.0", + "bytes", "cfg-if 0.1.10", "criterion", "env_logger", diff --git a/corcovado/Cargo.toml b/corcovado/Cargo.toml index 18b219ff..9d3bf26d 100644 --- a/corcovado/Cargo.toml +++ b/corcovado/Cargo.toml @@ -47,7 +47,7 @@ windows = { version = "0.42.0", features = [ [dev-dependencies] env_logger = { version = "0.4.0", default-features = false } tempdir = "0.3.7" -bytes = "0.3.0" +bytes = "1.11.1" criterion = { workspace = true } [[test]] diff --git a/corcovado/test/mod.rs b/corcovado/test/mod.rs index dd2947eb..963a24b9 100644 --- a/corcovado/test/mod.rs +++ b/corcovado/test/mod.rs @@ -34,27 +34,30 @@ mod test_write_then_drop; #[cfg(target_os = "fuchsia")] mod test_fuchsia_handles; -use bytes::{Buf, MutBuf}; +use bytes::{Buf, BufMut, BytesMut}; use corcovado::event::Event; use corcovado::{Events, Poll}; use std::io::{self, Read, Write}; use std::time::Duration; pub trait TryRead { - fn try_read_buf(&mut self, buf: &mut B) -> io::Result> + fn try_read_buf(&mut self, buf: &mut BytesMut) -> io::Result> where Self: Sized, { - // Reads the length of the slice supplied by buf.mut_bytes into the buffer - // This is not guaranteed to consume an entire datagram or segment. - // If your protocol is msg based (instead of continuous stream) you should - // ensure that your buffer is large enough to hold an entire segment (1532 bytes if not jumbo - // frames) - let res = self.try_read(unsafe { buf.mut_bytes() }); + let remaining = buf.capacity() - buf.len(); + if remaining == 0 { + return Ok(Some(0)); + } + buf.reserve(remaining); + let dst = buf.chunk_mut(); + let dst_slice = + unsafe { std::slice::from_raw_parts_mut(dst.as_mut_ptr(), dst.len()) }; + let res = self.try_read(dst_slice); if let Ok(Some(cnt)) = res { unsafe { - buf.advance(cnt); + buf.advance_mut(cnt); } } @@ -69,7 +72,7 @@ pub trait TryWrite { where Self: Sized, { - let res = self.try_write(buf.bytes()); + let res = self.try_write(buf.chunk()); if let Ok(Some(cnt)) = res { buf.advance(cnt);