-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
176 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use frame_support::{traits::Get}; | ||
pub use paste::paste; | ||
use sp_std::borrow::{Borrow, BorrowMut}; | ||
pub use frame_support::parameter_types; | ||
|
||
|
||
#[macro_export] | ||
macro_rules! clearable_parameter_type { | ||
($vis:vis static $name:ident: $type:ty) => { | ||
paste::paste! { | ||
std::thread_local! { $vis static [<$name:snake:upper>]: std::cell::RefCell<Option<$type>> = std::cell::RefCell::new(None); } | ||
struct $name; | ||
impl $name { | ||
/// Returns the value of this parameter type. | ||
pub fn get() -> Option<$type> { | ||
[<$name:snake:upper>].with(|v| v.borrow().clone()) | ||
} | ||
|
||
/// Clear the internal value. | ||
pub fn clear() { | ||
[<$name:snake:upper>].with(|v| *v.borrow_mut() = None); | ||
} | ||
|
||
/// Set the internal value. | ||
pub fn set(t: $type) { | ||
[<$name:snake:upper>].with(|v| *v.borrow_mut() = Some(t)); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
|
||
clearable_parameter_type!(pub static TestValue: u32); | ||
|
||
#[test] | ||
fn test() { | ||
assert_eq!(TestValue::get(), None); | ||
TestValue::set(121); | ||
assert_eq!(TestValue::get(), Some(121)); | ||
TestValue::clear(); | ||
assert_eq!(TestValue::get(), None); | ||
} |
Oops, something went wrong.