From bec85f5bebd640d69c6999540e6bd2fe40fbc5b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Mon, 11 Mar 2024 21:58:55 +0800 Subject: [PATCH] Refactor: make `macros` a public crate for publishing `macros` is renamed to `openraft-macros` and will be published to crates.io so that Openraft-0.9 that relies on it can be published. --- macros/Cargo.toml | 2 +- macros/src/lib.rs | 14 +++++++++++-- macros/src/lib_readme.md | 20 +++++++++++++++++++ memstore/README.md | 2 +- openraft/Cargo.toml | 5 +++-- openraft/src/lib.rs | 2 +- openraft/src/network/factory.rs | 2 +- openraft/src/network/network.rs | 2 +- openraft/src/network/snapshot_transport.rs | 2 +- openraft/src/runtime/mod.rs | 2 +- openraft/src/storage/log_store_ext.rs | 2 +- openraft/src/storage/mod.rs | 2 +- openraft/src/storage/v2.rs | 2 +- .../src/storage/v2/raft_log_storage_ext.rs | 2 +- openraft/src/testing/store_builder.rs | 2 +- 15 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 macros/src/lib_readme.md diff --git a/macros/Cargo.toml b/macros/Cargo.toml index b63d54c38..179a73beb 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "macros" +name = "openraft-macros" version = { workspace = true } edition = { workspace = true } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index a984c8cd7..a64bc54f7 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,3 +1,5 @@ +#![doc = include_str!("lib_readme.md")] + use proc_macro::TokenStream; use quote::quote; use syn::parse2; @@ -11,7 +13,7 @@ use syn::Type; /// This proc macro attribute optionally adds `Send` bounds to a trait. /// -/// By default, `Send` bounds will be added to the trait and to the return bounds of any async +/// By default, `Send` bounds will be added to the trait and to the return bounds of any async /// functions defined withing the trait. /// /// If the `singlethreaded` feature is enabled, the trait definition remains the same without any @@ -20,7 +22,7 @@ use syn::Type; /// # Example /// /// ``` -/// use macros::add_async_trait; +/// use openraft_macros::add_async_trait; /// /// #[add_async_trait] /// trait MyTrait { @@ -28,6 +30,14 @@ use syn::Type; /// } /// ``` /// +/// The above code will be transformed into: +/// +/// ```ignore +/// trait MyTrait { +/// fn my_method(&self) -> impl Future> + Send; +/// } +/// ``` +/// /// Note: This proc macro can only be used with traits. /// /// # Panics diff --git a/macros/src/lib_readme.md b/macros/src/lib_readme.md new file mode 100644 index 000000000..d16ae3387 --- /dev/null +++ b/macros/src/lib_readme.md @@ -0,0 +1,20 @@ +Supporting utils for [Openraft](https://crates.io/crates/openraft). + +`#[add_async_trait]` adds `Send` bounds to an async trait. + +# Example + +``` +#[openraft_macros::add_async_trait] +trait MyTrait { + async fn my_method(&self) -> Result<(), String>; +} +``` + +The above code will be transformed into: + +```ignore +trait MyTrait { + fn my_method(&self) -> impl Future> + Send; +} +``` \ No newline at end of file diff --git a/memstore/README.md b/memstore/README.md index 131c9e56c..c3d664692 100644 --- a/memstore/README.md +++ b/memstore/README.md @@ -1,5 +1,5 @@ # openraft-memstore -This is an in-memory example `RaftStorage` implementation based on [openraft-0.8](https://github.com/datafuselabs/openraft/tree/release-0.8). +This is an in-memory example `RaftLogStorage` and `RaftStateMachine` implementation based on [openraft](https://github.com/datafuselabs/openraft/). This crate is built mainly for testing or demonstrating purpose.:) diff --git a/openraft/Cargo.toml b/openraft/Cargo.toml index 8bce47a90..1dcaecb0d 100644 --- a/openraft/Cargo.toml +++ b/openraft/Cargo.toml @@ -13,6 +13,7 @@ keywords = { workspace = true } license = { workspace = true } repository = { workspace = true } + [dependencies] anyerror = { workspace = true } anyhow = { workspace = true, optional = true } @@ -20,7 +21,7 @@ byte-unit = { workspace = true } clap = { workspace = true } derive_more = { workspace = true } futures = { workspace = true } -macros = { path = "../macros" } +openraft-macros = { path = "../macros", version = "0.9.0" } maplit = { workspace = true } rand = { workspace = true } serde = { workspace = true, optional = true } @@ -80,7 +81,7 @@ compat = [] storage-v2 = [] # Disallows applications to share a raft instance with multiple threads. -singlethreaded = ["macros/singlethreaded"] +singlethreaded = ["openraft-macros/singlethreaded"] # Permit the follower's log to roll back to an earlier state without causing the diff --git a/openraft/src/lib.rs b/openraft/src/lib.rs index d50ad81b2..cc82c81b7 100644 --- a/openraft/src/lib.rs +++ b/openraft/src/lib.rs @@ -68,10 +68,10 @@ mod try_as_ref; pub use anyerror; pub use anyerror::AnyError; -pub use macros::add_async_trait; pub use network::RPCTypes; pub use network::RaftNetwork; pub use network::RaftNetworkFactory; +pub use openraft_macros::add_async_trait; pub use type_config::RaftTypeConfig; pub use crate::async_runtime::AsyncRuntime; diff --git a/openraft/src/network/factory.rs b/openraft/src/network/factory.rs index e781ecdf4..dbcdfe95e 100644 --- a/openraft/src/network/factory.rs +++ b/openraft/src/network/factory.rs @@ -1,4 +1,4 @@ -use macros::add_async_trait; +use openraft_macros::add_async_trait; use crate::network::RaftNetwork; use crate::OptionalSend; diff --git a/openraft/src/network/network.rs b/openraft/src/network/network.rs index d319e37bf..b49de3bef 100644 --- a/openraft/src/network/network.rs +++ b/openraft/src/network/network.rs @@ -1,7 +1,7 @@ use std::future::Future; use std::time::Duration; -use macros::add_async_trait; +use openraft_macros::add_async_trait; use crate::error::Fatal; use crate::error::RPCError; diff --git a/openraft/src/network/snapshot_transport.rs b/openraft/src/network/snapshot_transport.rs index 04d31b122..491d1aa05 100644 --- a/openraft/src/network/snapshot_transport.rs +++ b/openraft/src/network/snapshot_transport.rs @@ -6,7 +6,7 @@ use std::io::SeekFrom; use std::time::Duration; use futures::FutureExt; -use macros::add_async_trait; +use openraft_macros::add_async_trait; use tokio::io::AsyncReadExt; use tokio::io::AsyncSeekExt; use tokio::io::AsyncWriteExt; diff --git a/openraft/src/runtime/mod.rs b/openraft/src/runtime/mod.rs index 0396c4513..b381dac6b 100644 --- a/openraft/src/runtime/mod.rs +++ b/openraft/src/runtime/mod.rs @@ -1,4 +1,4 @@ -use macros::add_async_trait; +use openraft_macros::add_async_trait; use crate::engine::Command; use crate::RaftTypeConfig; diff --git a/openraft/src/storage/log_store_ext.rs b/openraft/src/storage/log_store_ext.rs index a7649a07e..49663fe1b 100644 --- a/openraft/src/storage/log_store_ext.rs +++ b/openraft/src/storage/log_store_ext.rs @@ -1,7 +1,7 @@ use std::fmt::Debug; use std::ops::RangeBounds; -use macros::add_async_trait; +use openraft_macros::add_async_trait; use crate::defensive::check_range_matches_entries; use crate::LogId; diff --git a/openraft/src/storage/mod.rs b/openraft/src/storage/mod.rs index 0e4e8c794..74406df73 100644 --- a/openraft/src/storage/mod.rs +++ b/openraft/src/storage/mod.rs @@ -14,7 +14,7 @@ use std::ops::RangeBounds; #[cfg(not(feature = "storage-v2"))] pub use adapter::Adaptor; pub use helper::StorageHelper; pub use log_store_ext::RaftLogReaderExt; -use macros::add_async_trait; +use openraft_macros::add_async_trait; pub use snapshot_signature::SnapshotSignature; pub use v2::RaftLogStorage; pub use v2::RaftLogStorageExt; diff --git a/openraft/src/storage/v2.rs b/openraft/src/storage/v2.rs index b2a91df7f..1e1a8af91 100644 --- a/openraft/src/storage/v2.rs +++ b/openraft/src/storage/v2.rs @@ -4,7 +4,7 @@ mod raft_log_storage_ext; -use macros::add_async_trait; +use openraft_macros::add_async_trait; pub use raft_log_storage_ext::RaftLogStorageExt; use crate::storage::callback::LogFlushed; diff --git a/openraft/src/storage/v2/raft_log_storage_ext.rs b/openraft/src/storage/v2/raft_log_storage_ext.rs index cc890b038..3d81399b9 100644 --- a/openraft/src/storage/v2/raft_log_storage_ext.rs +++ b/openraft/src/storage/v2/raft_log_storage_ext.rs @@ -1,5 +1,5 @@ use anyerror::AnyError; -use macros::add_async_trait; +use openraft_macros::add_async_trait; use crate::storage::LogFlushed; use crate::storage::RaftLogStorage; diff --git a/openraft/src/testing/store_builder.rs b/openraft/src/testing/store_builder.rs index 6e93d1a2c..7466534a4 100644 --- a/openraft/src/testing/store_builder.rs +++ b/openraft/src/testing/store_builder.rs @@ -1,6 +1,6 @@ #[cfg(not(feature = "storage-v2"))] use std::future::Future; -use macros::add_async_trait; +use openraft_macros::add_async_trait; #[cfg(not(feature = "storage-v2"))] use crate::storage::Adaptor; use crate::storage::RaftLogStorage;