subxt_signer/utils.rs
1// Copyright 2019-2025 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5#![allow(unused_macros)]
6
7/// Use like:
8///
9/// ```rust,ignore
10/// once_static_cloned!{
11/// /// Some documentation.
12/// fn foo() -> Vec<u8> {
13/// vec![1,2,3,4]
14/// }
15/// }
16/// ```
17///
18/// Clones the item out of static storage. Useful if it
19/// takes a while to create the item but cloning it is fairly cheap.
20macro_rules! once_static_cloned {
21 ($($(#[$attr:meta])* $vis:vis fn $name:ident() -> $ty:ty { $expr:expr } )+) => {
22 $(
23 $(#[$attr])*
24 #[allow(missing_docs)]
25 $vis fn $name() -> $ty {
26 cfg_if::cfg_if! {
27 if #[cfg(feature = "std")] {
28 static VAR: std::sync::OnceLock<$ty> = std::sync::OnceLock::new();
29 VAR.get_or_init(|| { $expr }).clone()
30 } else {
31 { $expr }
32 }
33 }
34 }
35 )+
36 };
37}