Skip to content

Commit

Permalink
Fixes according Cmichi's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx committed Mar 4, 2022
1 parent 236defd commit 91b36cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 0 additions & 2 deletions examples/upgradeable-contract/upgradeable-flipper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ crate-type = ["cdylib"]
default = ["std"]
std = [
"ink_primitives/std",
"ink_metadata",
"ink_metadata/std",
"ink_env/std",
"ink_storage/std",
"ink_lang/std",
"scale/std",
"scale-info",
"scale-info/std",
]
ink-as-dependency = []
2 changes: 1 addition & 1 deletion examples/upgradeable-contract/upgradeable-flipper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod flipper {
pub struct Flipper {
/// The field is `Upgradeable`, which means if the field is not initialized, it will be.
///
/// By default ink! throw an error that field is not initialized.
/// By default ink! would throw an error that the field is not initialized.
/// With that wrapper, you can initialize the field later during the method execution,
/// not in the constructor.
value: Upgradeable<bool, NotInitialized>,
Expand Down
13 changes: 13 additions & 0 deletions examples/upgradeable-contract/upgradeable-flipper/upgradeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ use scale::{
Encode,
};

/// It is a status struct for `Upgradeable`, to specify that the inner type is initialized.
#[derive(Debug)]
pub struct Initialized;
/// It is a status struct for `Upgradeable`, to specify that the inner type may be not initialized and
/// `pull_spread` should initialize it.
#[derive(Debug)]
pub struct NotInitialized;

/// The `Upgradeable` means if the field is not initialized, it will be.
///
/// By default ink! would throw an error that the field is not initialized.
/// With that wrapper, you can initialize the field later during the method execution,
/// not in the constructor. It can be done because `SpreadLayout` for `Upgradeable<T, NotInitialized>`
/// creates the object, if storage key is empty.
#[derive(Debug, Decode, Encode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct Upgradeable<T: PackedLayout, InitializationStatus = Initialized> {
Expand All @@ -35,6 +44,7 @@ impl<T: PackedLayout, State> Upgradeable<T, State> {
}
}

/// It is default implementation of `SpreadLayout` for case when we don't need to init.
impl<T: PackedLayout> SpreadLayout for Upgradeable<T, Initialized> {
const FOOTPRINT: u64 = T::FOOTPRINT;
const REQUIRES_DEEP_CLEAN_UP: bool = T::REQUIRES_DEEP_CLEAN_UP;
Expand All @@ -52,6 +62,7 @@ impl<T: PackedLayout> SpreadLayout for Upgradeable<T, Initialized> {
}
}

/// It is implementation of `SpreadLayout` that initialize the inner type if it is not initialized.
impl<T: PackedLayout + SpreadAllocate> SpreadLayout for Upgradeable<T, NotInitialized> {
const FOOTPRINT: u64 = <T as SpreadLayout>::FOOTPRINT;
const REQUIRES_DEEP_CLEAN_UP: bool = <T as SpreadLayout>::REQUIRES_DEEP_CLEAN_UP;
Expand All @@ -76,6 +87,8 @@ impl<T: PackedLayout + SpreadAllocate> SpreadLayout for Upgradeable<T, NotInitia
}
}

/// Below the boilerplate code to implement `PackedLayout`, `SpreadAllocate`, `PackedAllocate`.

impl<T: PackedLayout> PackedLayout for Upgradeable<T, Initialized> {
fn pull_packed(&mut self, at: &Key) {
<T as PackedLayout>::pull_packed(&mut self.inner, at)
Expand Down

0 comments on commit 91b36cd

Please sign in to comment.