Skip to content

Commit

Permalink
remove lockless borrowed
Browse files Browse the repository at this point in the history
  • Loading branch information
suikammd committed Feb 27, 2023
1 parent 5877aa6 commit 1c9b82c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 117 deletions.
1 change: 0 additions & 1 deletion tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ signal = [
sync = []
test-util = ["rt", "sync", "time"]
time = []
gat = []

# Technically, removing this is a breaking change even though it only ever did
# anything with the unstable flag on. It is probably safe to get rid of it after
Expand Down
103 changes: 10 additions & 93 deletions tokio/src/io/lockless_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cfg_net_or_io_util! {
unsafe impl<T: Send + Sync + Shutdown> Send for LocklessOwnedWriteHalf<T> {}

/// Inner split trait
pub trait LocklessSplitableOwned {
pub trait LocklessSplitable {
/// Owned Read Split
type OwnedReadHalf;
/// Owned Write Split
Expand All @@ -36,32 +36,6 @@ cfg_net_or_io_util! {
fn into_split(self) -> (Self::OwnedReadHalf, Self::OwnedWriteHalf);
}

impl<T> LocklessSplitableOwned for T
where
T: LocklessSplit + Shutdown,
{
type OwnedReadHalf = LocklessOwnedReadHalf<T>;

type OwnedWriteHalf = LocklessOwnedWriteHalf<T>;

fn into_split(self) -> (Self::OwnedReadHalf, Self::OwnedWriteHalf) {
let shared = Arc::new(UnsafeCell::new(self));
(
LocklessOwnedReadHalf(shared.clone()),
LocklessOwnedWriteHalf(shared),
)
}
}
}

cfg_net_or_io_util_or_gat! {
/// Borrowed Write Half Part
#[derive(Debug)]
pub struct LocklessWriteHalf<'cx, T>(pub &'cx T);
/// Borrowed Read Half Part
#[derive(Debug)]
pub struct LocklessReadHalf<'cx, T>(pub &'cx T);

/// The object with has this `LocklessSplit` trait can be safely split
/// to read/write object in both form of `Owned` or `Borrowed`.
///
Expand All @@ -78,80 +52,23 @@ cfg_net_or_io_util_or_gat! {
fn shutdown(&mut self) {}
}

/// Inner split trait
pub trait LocklessSplitableBorrowed {
/// Borrowed Read Split
type Read<'a>
where
Self: 'a;
/// Borrowed Write Split
type Write<'a>
where
Self: 'a;

/// Split into borrowed parts
fn split(&mut self) -> (Self::Read<'_>, Self::Write<'_>);
}

impl<T> LocklessSplitableBorrowed for T
impl<T> LocklessSplitable for T
where
T: LocklessSplit + Shutdown,
{
type Read<'a> = LocklessReadHalf<'a, T> where Self: 'a;

type Write<'a> = LocklessWriteHalf<'a, T> where Self: 'a;

fn split(&mut self) -> (Self::Read<'_>, Self::Write<'_>) {
(LocklessReadHalf(&*self), LocklessWriteHalf(&*self))
}
}

#[allow(clippy::cast_ref_to_mut)]
impl<'a, T> AsyncRead for LocklessReadHalf<'a, T>
where
T: AsyncRead,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let stream = unsafe { &mut *(self.0 as *const T as *mut T) };
let stream = unsafe { Pin::new_unchecked(stream) };
stream.poll_read(cx, buf)
}
}

#[allow(clippy::cast_ref_to_mut)]
impl<'a, T> AsyncWrite for LocklessWriteHalf<'a, T>
where
T: AsyncWrite,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
let stream = unsafe { &mut *(self.0 as *const T as *mut T) };
let stream = unsafe { Pin::new_unchecked(stream) };
stream.poll_write(cx, buf)
}
type OwnedReadHalf = LocklessOwnedReadHalf<T>;

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
let stream = unsafe { &mut *(self.0 as *const T as *mut T) };
let stream = unsafe { Pin::new_unchecked(stream) };
stream.poll_flush(cx)
}
type OwnedWriteHalf = LocklessOwnedWriteHalf<T>;

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
let stream = unsafe { &mut *(self.0 as *const T as *mut T) };
let stream = unsafe { Pin::new_unchecked(stream) };
stream.poll_shutdown(cx)
fn into_split(self) -> (Self::OwnedReadHalf, Self::OwnedWriteHalf) {
let shared = Arc::new(UnsafeCell::new(self));
(
LocklessOwnedReadHalf(shared.clone()),
LocklessOwnedWriteHalf(shared),
)
}
}
}

cfg_net_or_io_util! {
impl<T> AsyncRead for LocklessOwnedReadHalf<T>
where
T: AsyncRead,
Expand Down
8 changes: 2 additions & 6 deletions tokio/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,9 @@ cfg_io_std! {
pub use stdout::{stdout, Stdout};
}

cfg_net_or_io_util_or_gat! {
mod lockless_split;
pub use lockless_split::{LocklessSplitableBorrowed, LocklessReadHalf, LocklessWriteHalf};
}

cfg_net_or_io_util! {
pub use lockless_split::{Shutdown, LocklessSplitableOwned, LocklessSplit, LocklessOwnedReadHalf, LocklessOwnedWriteHalf};
mod lockless_split;
pub use lockless_split::{Shutdown, LocklessSplitable, LocklessSplit, LocklessOwnedReadHalf, LocklessOwnedWriteHalf};
}

cfg_io_util! {
Expand Down
10 changes: 0 additions & 10 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,6 @@ macro_rules! cfg_net_or_io_util {
}
}

macro_rules! cfg_net_or_io_util_or_gat {
($($item:item)*) => {
$(
#[cfg(any(feature = "net", feature = "io-util", feature = "gat"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "net", feature = "io-util", feature = "gat"))))]
$item
)*
}
}

macro_rules! cfg_net {
($($item:item)*) => {
$(
Expand Down
8 changes: 1 addition & 7 deletions tokio/tests/io_lockless_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

use tokio::io::{
AsyncRead, AsyncWrite, LocklessOwnedReadHalf, LocklessOwnedWriteHalf, LocklessSplit,
LocklessSplitableOwned, ReadBuf, Shutdown,
LocklessSplitable, ReadBuf, Shutdown,
};
#[cfg(gat)]
use tokio::io::{LocklessReadHalf, LocklessWriteHalf};

use std::io;
use std::pin::Pin;
Expand Down Expand Up @@ -52,10 +50,6 @@ impl Shutdown for RW {}
fn is_send_and_sync() {
fn assert_bound<T: Send + Sync>() {}

#[cfg(gat)]
assert_bound::<LocklessReadHalf<'_, RW>>();
#[cfg(gat)]
assert_bound::<LocklessWriteHalf<'_, RW>>();
assert_bound::<LocklessOwnedReadHalf<RW>>();
assert_bound::<LocklessOwnedWriteHalf<RW>>();
}
Expand Down

0 comments on commit 1c9b82c

Please sign in to comment.