Skip to content

Commit

Permalink
fix: fix clippy warning for monoio-compat
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Aug 7, 2023
1 parent 799c56d commit b2039c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
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

0 comments on commit b2039c3

Please sign in to comment.