From 68a652ed2f51d366bb8033497e6dfe545895410e Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 23 Jul 2026 08:08:33 +0800 Subject: feat: scaffold crate structure and implement core macros Add the project skeleton, LICENSE files, README, Makefile, doc examples, and the initial implementation of `#[func]`, `invoke!`, and `select!` procedural macros. --- test/Cargo.lock | 290 ++++++++++++++++++++++++++++++++++++++++++++++++ test/Cargo.toml | 3 + test/async/Cargo.toml | 17 +++ test/async/src/lib.rs | 2 + test/src/lib.rs | 3 + test/src/test_func.rs | 36 ++++++ test/src/test_invoke.rs | 50 +++++++++ test/src/test_select.rs | 92 +++++++++++++++ test/sync/Cargo.toml | 16 +++ test/sync/src/lib.rs | 2 + 10 files changed, 511 insertions(+) create mode 100644 test/Cargo.lock create mode 100644 test/Cargo.toml create mode 100644 test/async/Cargo.toml create mode 100644 test/async/src/lib.rs create mode 100644 test/src/lib.rs create mode 100644 test/src/test_func.rs create mode 100644 test/src/test_invoke.rs create mode 100644 test/src/test_select.rs create mode 100644 test/sync/Cargo.toml create mode 100644 test/sync/src/lib.rs (limited to 'test') diff --git a/test/Cargo.lock b/test/Cargo.lock new file mode 100644 index 0000000..5d4be76 --- /dev/null +++ b/test/Cargo.lock @@ -0,0 +1,290 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "futures" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a88cf1f829d945f548cf8fec32c61b1f202b6d93b45848602fc02af4b12ad218" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "262590f4fe6afeb0bc83be1daa64e52657fe185690a958af7f3ad0e92085c5ae" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cd50c473c80f6d7c3670a752354b8e569b1a7cbfdc0419ec88e5edad85e0dc7" + +[[package]] +name = "futures-executor" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6754879cc9f2c66f88c6e5c35344bb0bdb0708b0352b1201815667c7eabc7458" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4577ecaa3c4f96589d473f679a71b596316f6641bc350038b962a5daf0085d7a" + +[[package]] +name = "futures-macro" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d6d3cde68c518367be28956066ddfef33813991b77a55005a69dae04bf3b10b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "futures-sink" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e34418ac499d6305c2fb5ad0ed2f6ac998c5f8ca209b4510f7f94242c647e307" + +[[package]] +name = "futures-task" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b231ed28831efb4a61a08580c4bc233ec56bc009f4cd8f52da2c3cb97df0c109" + +[[package]] +name = "futures-util" +version = "0.3.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a77a90a256fce34da66415271e30f94ee91c57b04b8a2c042d9cf3220179deaa" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "slab", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "might_be_async" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "toml", +] + +[[package]] +name = "might_be_async_test_async" +version = "0.1.0" +dependencies = [ + "futures", + "might_be_async", +] + +[[package]] +name = "might_be_async_test_sync" +version = "0.1.0" +dependencies = [ + "might_be_async", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.3", +] + +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "toml_write", + "winnow", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" +dependencies = [ + "memchr", +] diff --git a/test/Cargo.toml b/test/Cargo.toml new file mode 100644 index 0000000..b92b54b --- /dev/null +++ b/test/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +members = ["sync", "async"] +resolver = "2" diff --git a/test/async/Cargo.toml b/test/async/Cargo.toml new file mode 100644 index 0000000..8184940 --- /dev/null +++ b/test/async/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "might_be_async_test_async" +version = "0.1.0" +edition = "2024" + +[features] +sync = [] +metadata_async = [] +custom_ft = [] +custom_invoke = [] + +[package.metadata.might_be_async] +default_feature_name = "metadata_async" + +[dependencies] +might_be_async = { path = "../.." } +futures = "0.3" diff --git a/test/async/src/lib.rs b/test/async/src/lib.rs new file mode 100644 index 0000000..a5e000a --- /dev/null +++ b/test/async/src/lib.rs @@ -0,0 +1,2 @@ +#![allow(unused)] +include!("../../src/lib.rs"); diff --git a/test/src/lib.rs b/test/src/lib.rs new file mode 100644 index 0000000..5e91425 --- /dev/null +++ b/test/src/lib.rs @@ -0,0 +1,3 @@ +include!("test_func.rs"); +include!("test_invoke.rs"); +include!("test_select.rs"); diff --git a/test/src/test_func.rs b/test/src/test_func.rs new file mode 100644 index 0000000..71fe365 --- /dev/null +++ b/test/src/test_func.rs @@ -0,0 +1,36 @@ +#[might_be_async::func] +fn identity(x: i32) -> i32 { + x +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_func_identity() { + assert_eq!(identity(42), 42); +} + +#[might_be_async::func] +fn first(a: T, _b: T) -> T +where + T: std::fmt::Debug, +{ + a.clone() +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_func_generic() { + assert_eq!(first(1, 2), 1); + assert_eq!(first("a", "b"), "a"); +} + +#[might_be_async::func("custom_ft")] +fn triple(x: i32) -> i32 { + x * 3 +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_func_custom_feature() { + assert_eq!(triple(3), 9); +} diff --git a/test/src/test_invoke.rs b/test/src/test_invoke.rs new file mode 100644 index 0000000..b8506e9 --- /dev/null +++ b/test/src/test_invoke.rs @@ -0,0 +1,50 @@ +// ─── 测试 1: 默认 feature 名 ───────────────────────────────────────────────── +// +// 不指定 feature → 用 Cargo.toml 的 default_feature_name = "metadata_async" +// +// 展开(feature "metadata_async" 关闭时): +// { #[cfg(feature = "metadata_async")] { double(5).await } +// #[cfg(not(feature = "metadata_async"))] { double(5) } } + +#[cfg(not(feature = "metadata_async"))] +fn double(x: i32) -> i32 { + x * 2 +} + +#[cfg(feature = "metadata_async")] +async fn double(x: i32) -> i32 { + x * 2 +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_invoke_default() { + assert_eq!(might_be_async::invoke!(double(5)), 10); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_invoke_default() { + let result = futures::executor::block_on(async { might_be_async::invoke!(double(5)) }); + assert_eq!(result, 10); +} + +#[might_be_async::func] +fn square(x: i32) -> i32 { + x * x +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_invoke_explicit() { + assert_eq!(might_be_async::invoke!("metadata_async" => square(6)), 36); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_invoke_explicit() { + let result = futures::executor::block_on(async { + might_be_async::invoke!("metadata_async" => square(6)) + }); + assert_eq!(result, 36); +} diff --git a/test/src/test_select.rs b/test/src/test_select.rs new file mode 100644 index 0000000..2cf3bfe --- /dev/null +++ b/test/src/test_select.rs @@ -0,0 +1,92 @@ +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_explicit() { + let r = might_be_async::select! { "metadata_async" => { 1 } else ! => { 2 } }; + assert_eq!(r, 2); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_explicit() { + let r = might_be_async::select! { "metadata_async" => { 1 } else ! => { 2 } }; + assert_eq!(r, 1); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_not() { + let r = might_be_async::select! { "metadata_async" => { 10 } else ! => { 20 } }; + assert_eq!(r, 20); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_not() { + let r = might_be_async::select! { "metadata_async" => { 10 } else ! => { 20 } }; + assert_eq!(r, 10); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_not_first() { + let r = might_be_async::select! { ! => { 30 } else "metadata_async" => { 40 } }; + assert_eq!(r, 30); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_not_first() { + let r = might_be_async::select! { ! => { 30 } else "metadata_async" => { 40 } }; + assert_eq!(r, 40); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_both_explicit() { + let r = might_be_async::select! { "metadata_async" => { 100 } else "custom_ft" => { 200 } }; + assert_eq!(r, 100); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_implicit() { + let r = might_be_async::select! { { 1 + 2 } else { 3 + 4 } }; + assert_eq!(r, 7); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_implicit() { + let r = futures::executor::block_on(async { + might_be_async::select! { { 1 + 2 } else { 3 + 4 } } + }); + assert_eq!(r, 3); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_mixed_explicit_first() { + let r = might_be_async::select! { "metadata_async" => { 100 } else { 200 } }; + assert_eq!(r, 200); +} + +#[cfg(feature = "metadata_async")] +#[test] +fn test_select_mixed_explicit_first() { + let r = might_be_async::select! { "metadata_async" => { 100 } else { 200 } }; + assert_eq!(r, 100); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_metadata_default() { + let r = might_be_async::select! { { 50 } else { 60 } }; + assert_eq!(r, 60); +} + +#[cfg(not(feature = "metadata_async"))] +#[test] +fn test_select_metadata_two_not() { + let r = might_be_async::select! { ! => { 70 } else ! => { 80 } }; + assert_eq!(r, 80); +} diff --git a/test/sync/Cargo.toml b/test/sync/Cargo.toml new file mode 100644 index 0000000..a2491da --- /dev/null +++ b/test/sync/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "might_be_async_test_sync" +version = "0.1.0" +edition = "2024" + +[features] +sync = [] +metadata_async = [] +custom_ft = [] +custom_invoke = [] + +[package.metadata.might_be_async] +default_feature_name = "metadata_async" + +[dependencies] +might_be_async = { path = "../.." } diff --git a/test/sync/src/lib.rs b/test/sync/src/lib.rs new file mode 100644 index 0000000..a5e000a --- /dev/null +++ b/test/sync/src/lib.rs @@ -0,0 +1,2 @@ +#![allow(unused)] +include!("../../src/lib.rs"); -- cgit