diff --git a/tokio-util/Cargo.toml b/tokio-util/Cargo.toml index b5a93dc3b..eb5f80f56 100644 --- a/tokio-util/Cargo.toml +++ b/tokio-util/Cargo.toml @@ -35,7 +35,7 @@ __docs_rs = ["futures-util"] [dependencies] tokio = { version = "1.28.0", path = "../tokio", features = ["sync"] } -bytes = "1.0.0" +bytes = "1.2.1" futures-core = "0.3.0" futures-sink = "0.3.0" futures-io = { version = "0.3.0", optional = true } diff --git a/tokio-util/src/udp/frame.rs b/tokio-util/src/udp/frame.rs index 38e790ae9..84a8096dc 100644 --- a/tokio-util/src/udp/frame.rs +++ b/tokio-util/src/udp/frame.rs @@ -5,13 +5,13 @@ use tokio::{io::ReadBuf, net::UdpSocket}; use bytes::{BufMut, BytesMut}; use futures_sink::Sink; +use std::io; use std::pin::Pin; use std::task::{ready, Context, Poll}; use std::{ borrow::Borrow, net::{Ipv4Addr, SocketAddr, SocketAddrV4}, }; -use std::{io, mem::MaybeUninit}; /// A unified [`Stream`] and [`Sink`] interface to an underlying `UdpSocket`, using /// the `Encoder` and `Decoder` traits to encode and decode frames. @@ -83,7 +83,7 @@ where let addr = { // Safety: `chunk_mut()` returns a `&mut UninitSlice`, and `UninitSlice` is a // transparent wrapper around `[MaybeUninit]`. - let buf = unsafe { &mut *(pin.rd.chunk_mut() as *mut _ as *mut [MaybeUninit]) }; + let buf = unsafe { pin.rd.chunk_mut().as_uninit_slice_mut() }; let mut read = ReadBuf::uninit(buf); let ptr = read.filled().as_ptr(); let res = ready!(pin.socket.borrow().poll_recv_from(cx, &mut read)); @@ -91,9 +91,10 @@ where assert_eq!(ptr, read.filled().as_ptr()); let addr = res?; + let filled = read.filled().len(); // Safety: This is guaranteed to be the number of initialized (and read) bytes due // to the invariants provided by `ReadBuf::filled`. - unsafe { pin.rd.advance_mut(read.filled().len()) }; + unsafe { pin.rd.advance_mut(filled) }; addr }; diff --git a/tokio-util/src/util/poll_buf.rs b/tokio-util/src/util/poll_buf.rs index 04985d492..452cc3c83 100644 --- a/tokio-util/src/util/poll_buf.rs +++ b/tokio-util/src/util/poll_buf.rs @@ -2,7 +2,6 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use bytes::{Buf, BufMut}; use std::io::{self, IoSlice}; -use std::mem::MaybeUninit; use std::pin::Pin; use std::task::{ready, Context, Poll}; @@ -59,7 +58,7 @@ pub fn poll_read_buf( // Safety: `chunk_mut()` returns a `&mut UninitSlice`, and `UninitSlice` is a // transparent wrapper around `[MaybeUninit]`. - let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit]) }; + let dst = unsafe { dst.as_uninit_slice_mut() }; let mut buf = ReadBuf::uninit(dst); let ptr = buf.filled().as_ptr(); ready!(io.poll_read(cx, &mut buf)?); diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 2b0c1127a..2179bae32 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -91,7 +91,7 @@ tokio-macros = { version = "~2.5.0", path = "../tokio-macros", optional = true } pin-project-lite = "0.2.11" # Everything else is optional... -bytes = { version = "1.1.0", optional = true } +bytes = { version = "1.2.1", optional = true } mio = { version = "1.0.1", optional = true, default-features = false } parking_lot = { version = "0.12.0", optional = true } diff --git a/tokio/src/io/util/read_buf.rs b/tokio/src/io/util/read_buf.rs index c211e0422..70701f647 100644 --- a/tokio/src/io/util/read_buf.rs +++ b/tokio/src/io/util/read_buf.rs @@ -41,7 +41,6 @@ where fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { use crate::io::ReadBuf; - use std::mem::MaybeUninit; let me = self.project(); @@ -51,7 +50,7 @@ where let n = { let dst = me.buf.chunk_mut(); - let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit]) }; + let dst = unsafe { dst.as_uninit_slice_mut() }; let mut buf = ReadBuf::uninit(dst); let ptr = buf.filled().as_ptr(); ready!(Pin::new(me.reader).poll_read(cx, &mut buf)?);