diff --git a/Cargo.toml b/Cargo.toml index 73b6357..fad78e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,9 @@ name = "r9" version = "0.1.0" edition = "2021" +#[lib] +#proc-macro = true + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -10,3 +13,5 @@ protobuf = "2.9.0" serde = "*" serde_derive = "*" serde_json = "*" +syn = "1.0" +quote = "1.0" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 3309fd5..1d94eff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,3 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} - mod timer; mod listhead; diff --git a/src/timer.rs b/src/timer.rs index b0c51eb..0ff2ce2 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -3,6 +3,32 @@ use std::cell::RefCell; use std::any::Any; use std::cmp; +#[macro_export] +macro_rules! my_vec { + // 没带任何参数的 my_vec,我们创建一个空的 vec + () => { + std::vec::Vec::new() + }; + + // 处理 my_vec![1, 2, 3, 4] + ($($el:expr),*) => ({ + let mut v = std::vec::Vec::new(); + $(v.push($el);)* + v + }); + + // 处理 my_vec![0; 10] + ($el:expr; $n:expr) => { + std::vec::from_elem($el, $n) + } +} + +/* +use proc_macro::TokenStream; +use quote::quote; +use syn; + */ + const TVN_BITS: usize = 6; const TVR_BITS: usize = 8; const TVN_SIZE: usize = 1 << TVN_BITS; @@ -41,8 +67,36 @@ impl Default for TimerType { } } +/* +pub trait EnableSharedFromSelf { + fn shared_from_self(); +} + +#[proc_macro_derive(crate::EnableSharedFromSelf)] +pub fn hello_macro_derive(input: TokenStream) -> TokenStream { + // Construct a representation of Rust code as a syntax tree + // that we can manipulate + let ast = syn::parse(input).unwrap(); + + // Build the trait implementation + impl_hello_macro(&ast) +} + +fn impl_hello_macro(ast: &syn::DeriveInput) -> TokenStream { + let name = &ast.ident; + let gen = quote! { + impl HelloMacro for #name { + fn hello_macro() { + println!("Hello, Macro! My name is {}!", stringify!(#name)); + } + } + }; + gen.into() +} + */ #[derive(Default)] +//#[derive(crate::EnableSharedFromSelf)] pub struct TimerList { holder: TimerListRp, wp: Rc::,