mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-25 10:23:23 +00:00
Refactor project structure
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
bd878dd1c9
commit
e3c227ae06
57
kernel/libs/comp-sys/controlled/src/lib.rs
Normal file
57
kernel/libs/comp-sys/controlled/src/lib.rs
Normal file
@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! This crate defines two attribute macros `controlled` and `uncontrolled`.
|
||||
//! This two macros are attached to functions or static variables to enable crate level access control.
|
||||
//! To use these two macros, a crate must at first registers a tool named `component_access_control`,
|
||||
//! because controlled used tool attribute internally.
|
||||
//!
|
||||
//! Below is a simple usage example.
|
||||
//! ```rust
|
||||
//! // crate-level tool registration
|
||||
//! #![feature(register_tool)]
|
||||
//! #![register_tool(component_access_control)]
|
||||
//!
|
||||
//! #[macro_use]
|
||||
//! extern crate controlled; // import this crate
|
||||
//!
|
||||
//! #[controlled]
|
||||
//! pub static FOO: usize = 0;
|
||||
//!
|
||||
//! #[uncontrolled]
|
||||
//! pub fn bar() {}
|
||||
//! ```
|
||||
use quote::quote;
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn controlled(
|
||||
attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let attr = attr.to_string();
|
||||
if attr.len() != 0 {
|
||||
panic!("controlled cannot accept inner tokens.")
|
||||
}
|
||||
let mut tokens: proc_macro::TokenStream = quote!(
|
||||
#[component_access_control::controlled]
|
||||
)
|
||||
.into();
|
||||
tokens.extend(item);
|
||||
tokens
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn uncontrolled(
|
||||
attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let attr = attr.to_string();
|
||||
if attr.len() != 0 {
|
||||
panic!("uncontrolled cannot accept inner tokens.")
|
||||
}
|
||||
let mut tokens: proc_macro::TokenStream = quote!(
|
||||
#[component_access_control::uncontrolled]
|
||||
)
|
||||
.into();
|
||||
tokens.extend(item);
|
||||
tokens
|
||||
}
|
Reference in New Issue
Block a user