Skip to content

Commit

Permalink
Change: rename AsyncOneshotSendExt to OneshotSender, remove `Toki…
Browse files Browse the repository at this point in the history
…oOneShotSender`

**Removal of `TokioOneShotSender`:** Previously,
`TokioOneShotSender(tokio::sync::oneshot::Sender)` was used to
implement `Debug` for a oneshot sender. Given that `Debug`
implementation is not a mandatory requirement, `TokioOneShotSender` is
no longer necessary and has been removed.

**Rename `AsyncOneshotSendExt` to `OneshotSender`:** The renaming
reflects a more streamlined and intuitive naming convention.

Upgrade tip:

- rename `AsyncOneshotSendExt` to `OneshotSender`

---

- `AsyncOneshotSendExt` and `TokioOneShotSender` are introduced in #1026
  • Loading branch information
drmingdrmer committed Jul 8, 2024
1 parent 4efdbfd commit 6ffcd2c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
20 changes: 6 additions & 14 deletions openraft/src/async_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait AsyncRuntime: Debug + Default + PartialEq + Eq + OptionalSend + Option
type ThreadLocalRng: rand::Rng;

/// Type of a `oneshot` sender.
type OneshotSender<T: OptionalSend>: AsyncOneshotSendExt<T> + OptionalSend + OptionalSync + Debug + Sized;
type OneshotSender<T: OptionalSend>: OneshotSender<T> + OptionalSend + OptionalSync + Sized;

/// Type of a `oneshot` receiver error.
type OneshotReceiverError: std::error::Error + OptionalSend;
Expand Down Expand Up @@ -104,8 +104,6 @@ pub trait AsyncRuntime: Debug + Default + PartialEq + Eq + OptionalSend + Option
#[derive(Debug, Default, PartialEq, Eq)]
pub struct TokioRuntime;

pub struct TokioOneShotSender<T: OptionalSend>(pub tokio::sync::oneshot::Sender<T>);

impl AsyncRuntime for TokioRuntime {
type JoinError = tokio::task::JoinError;
type JoinHandle<T: OptionalSend + 'static> = tokio::task::JoinHandle<T>;
Expand All @@ -114,7 +112,7 @@ impl AsyncRuntime for TokioRuntime {
type TimeoutError = tokio::time::error::Elapsed;
type Timeout<R, T: Future<Output = R> + OptionalSend> = tokio::time::Timeout<T>;
type ThreadLocalRng = rand::rngs::ThreadRng;
type OneshotSender<T: OptionalSend> = TokioOneShotSender<T>;
type OneshotSender<T: OptionalSend> = tokio::sync::oneshot::Sender<T>;
type OneshotReceiver<T: OptionalSend> = tokio::sync::oneshot::Receiver<T>;
type OneshotReceiverError = tokio::sync::oneshot::error::RecvError;

Expand Down Expand Up @@ -168,11 +166,11 @@ impl AsyncRuntime for TokioRuntime {
fn oneshot<T>() -> (Self::OneshotSender<T>, Self::OneshotReceiver<T>)
where T: OptionalSend {
let (tx, rx) = tokio::sync::oneshot::channel();
(TokioOneShotSender(tx), rx)
(tx, rx)
}
}

pub trait AsyncOneshotSendExt<T> {
pub trait OneshotSender<T> {
/// Attempts to send a value on this channel, returning it back if it could
/// not be sent.
///
Expand All @@ -184,15 +182,9 @@ pub trait AsyncOneshotSendExt<T> {
fn send(self, t: T) -> Result<(), T>;
}

impl<T: OptionalSend> AsyncOneshotSendExt<T> for TokioOneShotSender<T> {
impl<T> OneshotSender<T> for tokio::sync::oneshot::Sender<T> {
#[inline]
fn send(self, t: T) -> Result<(), T> {
self.0.send(t)
}
}

impl<T: OptionalSend> Debug for TokioOneShotSender<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("TokioSendWrapper").finish()
self.send(t)
}
}
2 changes: 1 addition & 1 deletion openraft/src/core/raft_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tracing::Instrument;
use tracing::Level;
use tracing::Span;

use crate::async_runtime::AsyncOneshotSendExt;
use crate::async_runtime::OneshotSender;
use crate::config::Config;
use crate::config::RuntimeConfig;
use crate::core::balancer::Balancer;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/core/sm/worker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tokio::sync::mpsc;

use crate::async_runtime::AsyncOneshotSendExt;
use crate::async_runtime::OneshotSender;
use crate::core::notify::Notify;
use crate::core::raft_msg::ResultSender;
use crate::core::sm::handle::Handle;
Expand Down
14 changes: 12 additions & 2 deletions openraft/src/engine/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;
use std::fmt::Debug;

use crate::async_runtime::AsyncOneshotSendExt;
use crate::async_runtime::OneshotSender;
use crate::core::sm;
use crate::engine::CommandKind;
use crate::error::Infallible;
Expand Down Expand Up @@ -244,7 +245,6 @@ where C: RaftTypeConfig
}
}

#[derive(Debug)]
pub(crate) struct ValueSender<C, T>
where
T: Debug + PartialEq + Eq + OptionalSend,
Expand All @@ -254,6 +254,16 @@ where
tx: OneshotSenderOf<C, T>,
}

impl<C, T> Debug for ValueSender<C, T>
where
T: Debug + PartialEq + Eq + OptionalSend,
C: RaftTypeConfig,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ValueSender").field("value", &self.value).finish()
}
}

impl<C, T> PartialEq for ValueSender<C, T>
where
T: Debug + PartialEq + Eq + OptionalSend,
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/raft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use tracing::trace_span;
use tracing::Instrument;
use tracing::Level;

use crate::async_runtime::AsyncOneshotSendExt;
use crate::async_runtime::OneshotSender;
use crate::config::Config;
use crate::config::RuntimeConfig;
use crate::core::command_state::CommandState;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/raft/responder/impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::async_runtime::AsyncOneshotSendExt;
use crate::async_runtime::OneshotSender;
use crate::raft::message::ClientWriteResult;
use crate::raft::responder::Responder;
use crate::type_config::alias::OneshotReceiverOf;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/storage/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io;

use tokio::sync::oneshot;

use crate::async_runtime::AsyncOneshotSendExt;
use crate::async_runtime::OneshotSender;
use crate::raft_state::io_state::log_io_id::LogIOId;
use crate::type_config::alias::OneshotSenderOf;
use crate::LogId;
Expand Down
1 change: 0 additions & 1 deletion openraft/src/timer/timeout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::time::Duration;
use tokio::time::sleep;
use tokio::time::Instant;

use crate::async_runtime::AsyncOneshotSendExt;
use crate::timer::timeout::RaftTimer;
use crate::timer::Timeout;
use crate::AsyncRuntime;
Expand Down

0 comments on commit 6ffcd2c

Please sign in to comment.