gear_subxt/constants/
constant_address.rs1use crate::{dynamic::DecodedValueThunk, metadata::DecodeWithMetadata};
6use std::borrow::Cow;
7
8pub trait ConstantAddress {
11 type Target: DecodeWithMetadata;
13
14 fn pallet_name(&self) -> &str;
16
17 fn constant_name(&self) -> &str;
19
20 fn validation_hash(&self) -> Option<[u8; 32]> {
24 None
25 }
26}
27
28pub struct Address<ReturnTy> {
30 pallet_name: Cow<'static, str>,
31 constant_name: Cow<'static, str>,
32 constant_hash: Option<[u8; 32]>,
33 _marker: std::marker::PhantomData<ReturnTy>,
34}
35
36pub type DynamicAddress = Address<DecodedValueThunk>;
38
39impl<ReturnTy> Address<ReturnTy> {
40 pub fn new(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> Self {
42 Self {
43 pallet_name: Cow::Owned(pallet_name.into()),
44 constant_name: Cow::Owned(constant_name.into()),
45 constant_hash: None,
46 _marker: std::marker::PhantomData,
47 }
48 }
49
50 #[doc(hidden)]
53 pub fn new_static(
54 pallet_name: &'static str,
55 constant_name: &'static str,
56 hash: [u8; 32],
57 ) -> Self {
58 Self {
59 pallet_name: Cow::Borrowed(pallet_name),
60 constant_name: Cow::Borrowed(constant_name),
61 constant_hash: Some(hash),
62 _marker: std::marker::PhantomData,
63 }
64 }
65
66 pub fn unvalidated(self) -> Self {
68 Self {
69 pallet_name: self.pallet_name,
70 constant_name: self.constant_name,
71 constant_hash: None,
72 _marker: self._marker,
73 }
74 }
75}
76
77impl<ReturnTy: DecodeWithMetadata> ConstantAddress for Address<ReturnTy> {
78 type Target = ReturnTy;
79
80 fn pallet_name(&self) -> &str {
81 &self.pallet_name
82 }
83
84 fn constant_name(&self) -> &str {
85 &self.constant_name
86 }
87
88 fn validation_hash(&self) -> Option<[u8; 32]> {
89 self.constant_hash
90 }
91}
92
93pub fn dynamic(pallet_name: impl Into<String>, constant_name: impl Into<String>) -> DynamicAddress {
95 DynamicAddress::new(pallet_name, constant_name)
96}