Remove PlainorMap enum; plain and map values now use same struct to simplify usage

This commit is contained in:
James Wilson
2025-09-30 16:08:34 +01:00
parent fc793a8b79
commit 964c474088
8 changed files with 121 additions and 401 deletions
+11 -16
View File
@@ -7,7 +7,7 @@
use alloc::borrow::Cow;
use frame_decode::storage::IntoEncodableValues;
use scale_decode::DecodeAsType;
use crate::utils::{Maybe, YesNoMaybe};
use crate::utils::{Maybe, YesMaybe};
/// A storage address. This allows access to a given storage entry, which can then
/// be iterated over or fetched from by providing the relevant set of keys, or
@@ -19,14 +19,10 @@ pub trait Address {
type KeyParts: IntoEncodableValues;
/// Type of the storage value at this location.
type Value: DecodeAsType;
/// Does the address have a default value defined for it.
/// Set to [`crate::utils::Yes`] to enable APIs which require one,
/// or [`crate::utils::Maybe`] to enable APIs which allow one
type HasDefaultValue: YesNoMaybe;
/// Does the address point to a map (as opposed to a plain value)?
/// Does the address point to a plain value (as opposed to a map)?
/// Set to [`crate::utils::Yes`] to enable APIs which require a map,
/// or [`crate::utils::Maybe`] to enable APIs which allow a map.
type IsMap;
type IsPlain: YesMaybe;
/// The pallet containing this storage entry.
fn pallet_name(&self) -> &str;
@@ -39,14 +35,14 @@ pub trait Address {
}
/// An address which is generated by the static APIs.
pub struct StaticAddress<KeyParts, Value, HasDefaultValue, IsMap> {
pub struct StaticAddress<KeyParts, Value, IsPlain> {
pallet_name: Cow<'static, str>,
entry_name: Cow<'static, str>,
validation_hash: Option<[u8; 32]>,
marker: core::marker::PhantomData<(KeyParts, Value, HasDefaultValue, IsMap)>,
marker: core::marker::PhantomData<(KeyParts, Value, IsPlain)>,
}
impl<KeyParts, Value, HasDefaultValue, IsMap> StaticAddress<KeyParts, Value, HasDefaultValue, IsMap> {
impl<KeyParts, Value, IsPlain> StaticAddress<KeyParts, Value, IsPlain> {
/// Create a new [`StaticAddress`] using static strings for the pallet and call name.
/// This is only expected to be used from codegen.
#[doc(hidden)]
@@ -83,17 +79,16 @@ impl<KeyParts, Value, HasDefaultValue, IsMap> StaticAddress<KeyParts, Value, Has
}
}
impl<KeyParts, Value, HasDefaultValue, IsMap> Address
for StaticAddress<KeyParts, Value, HasDefaultValue, IsMap>
impl<KeyParts, Value, IsPlain> Address
for StaticAddress<KeyParts, Value, IsPlain>
where
KeyParts: IntoEncodableValues,
Value: DecodeAsType,
HasDefaultValue: YesNoMaybe,
IsPlain: YesMaybe,
{
type KeyParts = KeyParts;
type Value = Value;
type HasDefaultValue = HasDefaultValue;
type IsMap = IsMap;
type IsPlain = IsPlain;
fn pallet_name(&self) -> &str {
&self.pallet_name
@@ -110,7 +105,7 @@ where
/// A dynamic address is simply a [`StaticAddress`] which asserts that the
/// entry *might* be a map and *might* have a default value.
pub type DynamicAddress<KeyParts = Vec<scale_value::Value>, Value = scale_value::Value> = StaticAddress<KeyParts, Value, Maybe, Maybe>;
pub type DynamicAddress<KeyParts = Vec<scale_value::Value>, Value = scale_value::Value> = StaticAddress<KeyParts, Value, Maybe>;
/// Construct a new dynamic storage address. You can define the type of the
/// storage keys and value yourself here, but have no guarantee that they will
+1 -1
View File
@@ -22,7 +22,7 @@ use alloc::vec::Vec;
use codec::{Compact, Decode, Encode};
use derive_where::derive_where;
pub use yesnomaybe::{Yes, No, Maybe, YesNoMaybe};
pub use yesnomaybe::{Yes, Maybe, YesMaybe};
pub use account_id::AccountId32;
pub use account_id20::AccountId20;
pub use era::Era;
+4 -11
View File
@@ -6,26 +6,19 @@
pub enum Yes {}
/// A unit marker enum.
pub enum Maybe {}
/// A unit marker enum.
pub enum No {}
/// This is implemented for [`Yes`], [`No`] and [`Maybe`] and
/// This is implemented for [`Yes`] and [`Maybe`] and
/// allows us to check at runtime which of these types is present.
pub trait YesNoMaybe {
pub trait YesMaybe {
/// [`Yes`]
fn is_yes() -> bool { false }
/// [`No`]
fn is_no() -> bool { false }
/// [`Maybe`]
fn is_maybe() -> bool { false }
}
impl YesNoMaybe for Yes {
impl YesMaybe for Yes {
fn is_yes() -> bool { true }
}
impl YesNoMaybe for No {
fn is_no() -> bool { true }
}
impl YesNoMaybe for Maybe {
impl YesMaybe for Maybe {
fn is_maybe() -> bool { true }
}