Skip to content

Commit

Permalink
Merge pull request #644 from drmingdrmer/30-valid-2
Browse files Browse the repository at this point in the history
Refactor: improve mod validate
  • Loading branch information
drmingdrmer authored Jan 10, 2023
2 parents 0c56bcc + c6fc0ca commit 7b67f11
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 15 deletions.
2 changes: 1 addition & 1 deletion openraft/src/engine/engine_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::raft_state::LogStateReader;
use crate::raft_state::RaftState;
use crate::raft_types::RaftLogId;
use crate::summary::MessageSummary;
use crate::valid::Valid;
use crate::validate::Valid;
use crate::LogId;
use crate::LogIdOptionExt;
use crate::Membership;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod runtime;
pub mod storage;
pub mod testing;
pub mod timer;
pub(crate) mod valid;
pub(crate) mod validate;
pub mod versioned;

#[cfg(test)] mod raft_state_test;
Expand Down
2 changes: 1 addition & 1 deletion openraft/src/raft_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::equal;
use crate::less_equal;
use crate::node::Node;
use crate::raft_types::RaftLogId;
use crate::valid::Validate;
use crate::validate::Validate;
use crate::LogId;
use crate::LogIdOptionExt;
use crate::MembershipState;
Expand Down
8 changes: 0 additions & 8 deletions openraft/src/valid/mod.rs

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use test::Bencher;
use crate::less_equal;
use crate::quorum::AsJoint;
use crate::quorum::QuorumSet;
use crate::valid::Valid;
use crate::valid::Validate;
use crate::validate::Valid;
use crate::validate::Validate;

struct Foo {
a: u64,
Expand Down
9 changes: 9 additions & 0 deletions openraft/src/validate/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[cfg(feature = "bench")]
#[cfg(test)]
mod bench;

#[allow(clippy::module_inception)] mod validate;
mod validate_impl;

pub(crate) use validate::Valid;
pub(crate) use validate::Validate;
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::ops::Deref;
use std::ops::DerefMut;

#[macro_export]
macro_rules! less {
($a: expr, $b: expr) => {{
let a = $a;
let b = $b;
if (a < b) {
// Ok
} else {
Err(::anyerror::AnyError::error(format!(
"expect: {}({:?}) {} {}({:?})",
stringify!($a),
a,
"<",
stringify!($b),
b,
)))?;
}
}};
}

#[macro_export]
macro_rules! less_equal {
($a: expr, $b: expr) => {{
Expand Down Expand Up @@ -171,13 +192,24 @@ where T: Validate
}
}

impl<T: Display> Display for Valid<T>
where T: Validate
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
}
}

#[cfg(test)]
mod tests {
use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;

use crate::valid::Valid;
use crate::valid::Validate;
use crate::validate::Valid;
use crate::validate::Validate;

#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct Foo {
a: u64,
}
Expand All @@ -189,6 +221,25 @@ mod tests {
}
}

impl Display for Foo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Foo:{}", self.a)
}
}

#[test]
fn test_trait() {
// Display
println!("Display: {}", Valid::new(Foo { a: 3 }));

// Default
assert_eq!(Valid::new(Foo { a: 0 }), Valid::<Foo>::default(), "impl Default");

// Clone
#[allow(clippy::redundant_clone)]
let _clone = Valid::new(Foo { a: 3 }).clone();
}

#[test]
fn test_validate() {
// panic when reading an invalid state
Expand Down
28 changes: 28 additions & 0 deletions openraft/src/validate/validate_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::error::Error;

use crate::validate::Validate;

/// Dummy impl Validate for primitive types
macro_rules! impl_validate {
($typ: ty) => {
impl Validate for $typ {
fn validate(&self) -> Result<(), Box<dyn Error>> {
Ok(())
}
}
};
}

impl_validate!(bool);
impl_validate!(usize);
impl_validate!(isize);
impl_validate!(u8);
impl_validate!(u16);
impl_validate!(u32);
impl_validate!(u64);
impl_validate!(i8);
impl_validate!(i16);
impl_validate!(i32);
impl_validate!(i64);
impl_validate!(&str);
impl_validate!(String);

0 comments on commit 7b67f11

Please sign in to comment.