Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix clippy warning for monoio-compat #201

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion monoio-compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT/Apache-2.0"
name = "monoio-compat"
readme = "README.md"
repository = "https://github.com/bytedance/monoio"
version = "0.1.1"
version = "0.1.2"

[dependencies]
monoio = {version = "0.1.0", path = "../monoio", default-features = false}
Expand Down
20 changes: 8 additions & 12 deletions monoio-compat/src/safe_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io;
use std::{cell::UnsafeCell, io};

use monoio::{
buf::IoBufMut,
Expand All @@ -12,7 +12,7 @@ use crate::{box_future::MaybeArmedBoxFuture, buf::Buf};
/// The Wrapper will impl tokio AsyncRead and AsyncWrite.
/// Mainly used for compatible.
pub struct StreamWrapper<T> {
stream: T,
stream: UnsafeCell<T>,
read_buf: Option<Buf>,
write_buf: Option<Buf>,

Expand All @@ -27,7 +27,7 @@ unsafe impl<T: Split> Split for StreamWrapper<T> {}
impl<T> StreamWrapper<T> {
/// Consume self and get inner T.
pub fn into_inner(self) -> T {
self.stream
self.stream.into_inner()
}

/// Creates a new `TcpStreamCompat` from a monoio `TcpStream` or `UnixStream`.
Expand All @@ -36,7 +36,7 @@ impl<T> StreamWrapper<T> {
let w_buf = Buf::new(write_buffer);

Self {
stream,
stream: UnsafeCell::new(stream),
read_buf: Some(r_buf),
write_buf: Some(w_buf),
read_fut: Default::default(),
Expand Down Expand Up @@ -79,8 +79,7 @@ impl<T: AsyncReadRent + Unpin + 'static> tokio::io::AsyncRead for StreamWrapper<
// there is no data in buffer. we will construct the future
let buf = unsafe { this.read_buf.take().unwrap_unchecked() };
// we must leak the stream
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const T as *mut T) };
let stream = unsafe { &mut *this.stream.get() };
this.read_fut.arm_future(AsyncReadRent::read(stream, buf));
}

Expand Down Expand Up @@ -145,8 +144,7 @@ impl<T: AsyncWriteRent + Unpin + 'static> tokio::io::AsyncWrite for StreamWrappe
unsafe { owned_buf.set_init(len) };

// we must leak the stream
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const T as *mut T) };
let stream = unsafe { &mut *this.stream.get() };
this.write_fut
.arm_future(AsyncWriteRentExt::write_all(stream, owned_buf));
match this.write_fut.poll(cx) {
Expand Down Expand Up @@ -184,8 +182,7 @@ impl<T: AsyncWriteRent + Unpin + 'static> tokio::io::AsyncWrite for StreamWrappe
}

if !this.flush_fut.armed() {
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const T as *mut T) };
let stream = unsafe { &mut *this.stream.get() };
this.flush_fut.arm_future(stream.flush());
}
this.flush_fut.poll(cx)
Expand All @@ -211,8 +208,7 @@ impl<T: AsyncWriteRent + Unpin + 'static> tokio::io::AsyncWrite for StreamWrappe
}

if !this.shutdown_fut.armed() {
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const T as *mut T) };
let stream = unsafe { &mut *this.stream.get() };
this.shutdown_fut.arm_future(stream.shutdown());
}
this.shutdown_fut.poll(cx)
Expand Down
20 changes: 8 additions & 12 deletions monoio-compat/src/tcp_unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io;
use std::{cell::UnsafeCell, io};

use monoio::{
io::{AsyncReadRent, AsyncWriteRent},
Expand Down Expand Up @@ -33,7 +33,7 @@ impl Dst {
}

pub struct TcpStreamCompat {
stream: TcpStream,
stream: UnsafeCell<TcpStream>,
read_dst: Dst,
write_dst: Dst,

Expand All @@ -45,7 +45,7 @@ pub struct TcpStreamCompat {

impl From<TcpStreamCompat> for TcpStream {
fn from(stream: TcpStreamCompat) -> Self {
stream.stream
stream.stream.into_inner()
}
}

Expand All @@ -57,7 +57,7 @@ impl TcpStreamCompat {
/// valid and the same among different calls before Poll::Ready returns.
pub unsafe fn new(stream: TcpStream) -> Self {
Self {
stream,
stream: UnsafeCell::new(stream),
read_dst: Default::default(),
write_dst: Default::default(),
read_fut: Default::default(),
Expand All @@ -83,8 +83,7 @@ impl tokio::io::AsyncRead for TcpStreamCompat {
let raw_buf = this.read_dst.check_and_to_rawbuf(ptr, len);
if !this.read_fut.armed() {
// we must leak the stream
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const TcpStream as *mut TcpStream) };
let stream = unsafe { &mut *this.stream.get() };
this.read_fut
.arm_future(AsyncReadRent::read(stream, raw_buf));
}
Expand Down Expand Up @@ -115,8 +114,7 @@ impl tokio::io::AsyncWrite for TcpStreamCompat {
let raw_buf = this.write_dst.check_and_to_rawbuf(ptr, len);
if !this.write_fut.armed() {
// we must leak the stream
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const TcpStream as *mut TcpStream) };
let stream = unsafe { &mut *this.stream.get() };
this.write_fut
.arm_future(AsyncWriteRent::write(stream, raw_buf));
}
Expand All @@ -138,8 +136,7 @@ impl tokio::io::AsyncWrite for TcpStreamCompat {
let this = self.get_mut();

if !this.flush_fut.armed() {
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const TcpStream as *mut TcpStream) };
let stream = unsafe { &mut *this.stream.get() };
this.flush_fut.arm_future(stream.flush());
}
this.flush_fut.poll(cx)
Expand All @@ -152,8 +149,7 @@ impl tokio::io::AsyncWrite for TcpStreamCompat {
let this = self.get_mut();

if !this.shutdown_fut.armed() {
#[allow(cast_ref_to_mut)]
let stream = unsafe { &mut *(&this.stream as *const TcpStream as *mut TcpStream) };
let stream = unsafe { &mut *this.stream.get() };
this.shutdown_fut.arm_future(stream.shutdown());
}
this.shutdown_fut.poll(cx)
Expand Down
4 changes: 2 additions & 2 deletions monoio/src/io/util/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
'r: loop {
let (read_res, mut buf_read) = reader.read(buf).await;
match read_res {
Ok(n) if n == 0 => {
Ok(0) => {
// read closed
break;
}
Expand All @@ -42,7 +42,7 @@ where
'w: loop {
let (write_res, buf_) = writer.write_all(buf_read).await;
match write_res {
Ok(n) if n == 0 => {
Ok(0) => {
// write closed
return Err(io::Error::new(
io::ErrorKind::WriteZero,
Expand Down