|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +//! Datatype for easy mutation of the extra "sidecar" data. |
| 19 | +
|
| 20 | +use super::*; |
| 21 | + |
| 22 | +/// A mutator type allowing inspection and possible modification of the extra "sidecar" data. |
| 23 | +/// |
| 24 | +/// This may be used as a `Deref` for the pallet's extra data. If mutated (using `DerefMut`), then |
| 25 | +/// any uncommitted changes (see `commit` function) will be automatically committed to storage when |
| 26 | +/// dropped. Changes, even after committed, may be reverted to their original values with the |
| 27 | +/// `revert` function. |
| 28 | +pub struct ExtraMutator<T: Config> { |
| 29 | + id: T::AssetId, |
| 30 | + who: T::AccountId, |
| 31 | + original: T::Extra, |
| 32 | + pending: Option<T::Extra>, |
| 33 | +} |
| 34 | + |
| 35 | +impl<T: Config> Drop for ExtraMutator<T> { |
| 36 | + fn drop(&mut self) { |
| 37 | + debug_assert!(self.commit().is_ok(), "attempt to write to non-existent asset account"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl<T: Config> sp_std::ops::Deref for ExtraMutator<T> { |
| 42 | + type Target = T::Extra; |
| 43 | + fn deref(&self) -> &T::Extra { |
| 44 | + match self.pending { |
| 45 | + Some(ref value) => value, |
| 46 | + None => &self.original, |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<T: Config> sp_std::ops::DerefMut for ExtraMutator<T> { |
| 52 | + fn deref_mut(&mut self) -> &mut T::Extra { |
| 53 | + if self.pending.is_none() { |
| 54 | + self.pending = Some(self.original.clone()); |
| 55 | + } |
| 56 | + self.pending.as_mut().unwrap() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<T: Config> ExtraMutator<T> { |
| 61 | + pub(super) fn maybe_new(id: T::AssetId, who: impl sp_std::borrow::Borrow<T::AccountId>) |
| 62 | + -> Option<ExtraMutator<T>> |
| 63 | + { |
| 64 | + if Account::<T>::contains_key(id, who.borrow()) { |
| 65 | + Some(ExtraMutator::<T> { |
| 66 | + id, |
| 67 | + who: who.borrow().clone(), |
| 68 | + original: Account::<T>::get(id, who.borrow()).extra, |
| 69 | + pending: None, |
| 70 | + }) |
| 71 | + } else { |
| 72 | + None |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /// Commit any changes to storage. |
| 78 | + pub fn commit(&mut self) -> Result<(), ()> { |
| 79 | + if let Some(extra) = self.pending.take() { |
| 80 | + Account::<T>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| |
| 81 | + if let Some(ref mut account) = maybe_account { |
| 82 | + account.extra = extra; |
| 83 | + Ok(()) |
| 84 | + } else { |
| 85 | + Err(()) |
| 86 | + } |
| 87 | + ) |
| 88 | + } else { |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// Revert any changes, even those already committed by `self` and drop self. |
| 94 | + pub fn revert(mut self) -> Result<(), ()> { |
| 95 | + self.pending = None; |
| 96 | + Account::<T>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| |
| 97 | + if let Some(ref mut account) = maybe_account { |
| 98 | + account.extra = self.original.clone(); |
| 99 | + Ok(()) |
| 100 | + } else { |
| 101 | + Err(()) |
| 102 | + } |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
0 commit comments