-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add
Raft::external_state_machine_request()
to run a functi…
…on inside state machine Add `Raft::external_state_machine_request()` in a fire-and-forget manner, and `Raft::with_state_machine()` blocks waiting on the response. If the input StateMachine is a different type from the one in `RaftCore`, `with_state_machine()` an error, while `external_state_machine_request()` silently ignores it. Other changes: move OptionalSerde, OptionalSync, OptionalSync from `openraft::` to `openraft::base::`
- Loading branch information
1 parent
590d943
commit 6c7527f
Showing
16 changed files
with
391 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//! Basic types used in the Raft implementation. | ||
pub use serde_able::OptionalSerde; | ||
pub use threaded::BoxAny; | ||
pub use threaded::BoxAsyncOnceMut; | ||
pub use threaded::BoxFuture; | ||
pub use threaded::BoxOnce; | ||
pub use threaded::OptionalSend; | ||
pub use threaded::OptionalSync; | ||
|
||
#[cfg(not(feature = "singlethreaded"))] | ||
mod threaded { | ||
use std::any::Any; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
|
||
pub trait OptionalSend: Send {} | ||
impl<T: Send + ?Sized> OptionalSend for T {} | ||
|
||
pub trait OptionalSync: Sync {} | ||
impl<T: Sync + ?Sized> OptionalSync for T {} | ||
|
||
pub type BoxFuture<'a, T = ()> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; | ||
pub type BoxAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> BoxFuture<T> + Send + 'a>; | ||
pub type BoxOnce<'a, A, T = ()> = Box<dyn FnOnce(&A) -> T + Send + 'a>; | ||
pub type BoxAny = Box<dyn Any + Send>; | ||
} | ||
|
||
#[cfg(feature = "singlethreaded")] | ||
mod threaded { | ||
use std::any::Any; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
|
||
pub trait OptionalSend {} | ||
impl<T: ?Sized> OptionalSend for T {} | ||
|
||
pub trait OptionalSync {} | ||
impl<T: ?Sized> OptionalSync for T {} | ||
|
||
pub type BoxFuture<'a, T = ()> = Pin<Box<dyn Future<Output = T> + 'a>>; | ||
pub type BoxAsyncOnceMut<'a, A, T = ()> = Box<dyn FnOnce(&mut A) -> BoxFuture<T> + 'a>; | ||
pub type BoxOnce<'a, A, T = ()> = Box<dyn FnOnce(&A) -> T + 'a>; | ||
pub type BoxAny = Box<dyn Any>; | ||
} | ||
|
||
#[cfg(not(feature = "serde"))] | ||
mod serde_able { | ||
#[doc(hidden)] | ||
pub trait OptionalSerde {} | ||
impl<T> OptionalSerde for T {} | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
mod serde_able { | ||
#[doc(hidden)] | ||
pub trait OptionalSerde: serde::Serialize + for<'a> serde::Deserialize<'a> {} | ||
impl<T> OptionalSerde for T where T: serde::Serialize + for<'a> serde::Deserialize<'a> {} | ||
} |
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,30 @@ | ||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] | ||
#[error( | ||
"User-defined function on the state machine failed to run; \ | ||
It may have used a different type \ | ||
of state machine from the one in RaftCore (`{actual_type}`)" | ||
)] | ||
|
||
pub struct InvalidStateMachineType { | ||
pub actual_type: &'static str, | ||
} | ||
|
||
impl InvalidStateMachineType { | ||
pub(crate) fn new<SM>() -> Self { | ||
Self { | ||
actual_type: std::any::type_name::<SM>(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_invalid_state_machine_type_to_string() { | ||
let err = super::InvalidStateMachineType::new::<u32>(); | ||
assert_eq!( | ||
err.to_string(), | ||
"User-defined function on the state machine failed to run; It may have used a different type of state machine from the one in RaftCore (`u32`)" | ||
); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.