forked from databendlabs/openraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Abstract the watch channel into a traits (databendlabs#1167)
* Refactor: Abstract the watch channel into a traits
- Loading branch information
1 parent
e69aea1
commit 1b4ec53
Showing
11 changed files
with
173 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
mod watch_error; | ||
|
||
use openraft_macros::add_async_trait; | ||
pub use watch_error::RecvError; | ||
pub use watch_error::SendError; | ||
|
||
use crate::OptionalSend; | ||
use crate::OptionalSync; | ||
|
||
pub trait Watch: Sized + OptionalSend { | ||
type Sender<T: OptionalSend + OptionalSync>: WatchSender<Self, T>; | ||
type Receiver<T: OptionalSend + OptionalSync>: WatchReceiver<Self, T>; | ||
|
||
type Ref<'a, T: OptionalSend + 'a>: std::ops::Deref<Target = T> + 'a; | ||
|
||
fn channel<T: OptionalSend + OptionalSync>(init: T) -> (Self::Sender<T>, Self::Receiver<T>); | ||
} | ||
|
||
pub trait WatchSender<W, T>: OptionalSend + Clone | ||
where | ||
W: Watch, | ||
T: OptionalSend + OptionalSync, | ||
{ | ||
fn send(&self, value: T) -> Result<(), SendError<T>>; | ||
fn send_if_modified<F>(&self, modify: F) -> bool | ||
where F: FnOnce(&mut T) -> bool; | ||
|
||
fn borrow_watched(&self) -> W::Ref<'_, T>; | ||
} | ||
|
||
#[add_async_trait] | ||
pub trait WatchReceiver<W, T>: OptionalSend + OptionalSync + Clone | ||
where | ||
W: Watch, | ||
T: OptionalSend + OptionalSync, | ||
{ | ||
async fn changed(&mut self) -> Result<(), RecvError>; | ||
fn borrow_watched(&self) -> W::Ref<'_, T>; | ||
} |
37 changes: 37 additions & 0 deletions
37
openraft/src/type_config/async_runtime/watch/watch_error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::fmt; | ||
|
||
/// Error returned by the `WatchSender`. | ||
#[derive(PartialEq, Eq, Clone, Copy)] | ||
pub struct SendError<T>(pub T); | ||
|
||
impl<T> fmt::Debug for SendError<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("SendError").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl<T> fmt::Display for SendError<T> { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(fmt, "watch channel closed") | ||
} | ||
} | ||
|
||
impl<T> std::error::Error for SendError<T> {} | ||
|
||
/// Error returned by the `WatchReceiver`. | ||
#[derive(PartialEq, Eq, Clone, Copy)] | ||
pub struct RecvError(pub ()); | ||
|
||
impl fmt::Debug for RecvError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("RecvError").finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl fmt::Display for RecvError { | ||
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(fmt, "watch channel closed") | ||
} | ||
} | ||
|
||
impl std::error::Error for RecvError {} |
Oops, something went wrong.