Compare smol-hyper and hyper-futures for impl Read
(Jin Qing’s Column, Nov., 2024)
Hyper uses tokio’s AsyncRead/Write trait not the futures-0.3 one.
See: https://github.com/hyperium/hyper/issues/2024
There are at least 3 ways to implement hyper Read/Write for futures_io::AsyncRead/Write:
- smol-hyper: FuturesIo implements hyper I/O traits for futures-io implementors.
- download: 45K
- hyper-futures: Compatibility layer for futures to use AsyncRead and AsyncWrite traits with hyper
- download: 1413
- tokio_util::compat + hyper_util::rt::tokio:
- first use tokio_util to implement tokio IO
- then use TokioIo to implement hyper’s IO traits
Prefer smol-hyper or hyper-futures because they are directly.
The implementation of smol-hyper and hyper-futures are very similar.
Following is the comparison of impl Read.
smol-hyper
Revision: 4d6916333055d25896ddcd52ef6ff33af717e0e2
Author: Dependency Dog <renovate@notgull.net>
Date: 24.10.19 8:15:03
Message:
chore(deps): update rust crate hyper to v1.5.0
impl<T: futures_io::AsyncRead + ?Sized> hyper::rt::Read for FuturesIo<T> {
#[inline]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
mut buf: ReadBufCursor<'_>,
) -> Poll<io::Result<()>> {
// Fill the read buffer with initialized data.
let read_slice = unsafe {
let buffer = buf.as_mut();
buffer.as_mut_ptr().write_bytes(0, buffer.len());
slice::from_raw_parts_mut(buffer.as_mut_ptr() as *mut u8, buffer.len())
};
// Read bytes from the underlying source.
let n = match self.get_pin_mut().poll_read(cx, read_slice) {
Poll::Ready(Ok(n)) => n,
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Pending => return Poll::Pending,
};
// Advance the buffer.
unsafe {
buf.advance(n);
}
Poll::Ready(Ok(()))
}
}
hyper-futures
Revision: faeab613dc6a30a4439e0e29eedc13cd20693f63
Author: AlexSherb <alex.sherbinin.andr@gmail.com>
Date: 24.1.12 2:08:13
Message:
Removed dbg! calls. Version changed to 0.1.1
impl<T: AsyncRead + AsyncWrite> hyper::rt::Read for AsyncReadWriteCompat<T> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
mut buf: hyper::rt::ReadBufCursor<'_>,
) -> Poll<Result<(), std::io::Error>> {
let buf_slice: &mut [u8] = unsafe { std::mem::transmute(buf.as_mut()) };
match self.project().inner.poll_read(cx, buf_slice) {
Poll::Ready(bytes_read) => {
let bytes_read = bytes_read?;
unsafe {
buf.advance(bytes_read);
}
Poll::Ready(Ok(()))
}
Poll::Pending => Poll::Pending,
}
}
}
217

被折叠的 条评论
为什么被折叠?



