Skip to content
Snippets Groups Projects
Unverified Commit 007565a7 authored by clangenb's avatar clangenb Committed by GitHub
Browse files

Incrementable: return None instead of saturating (#7846)


Closes #7845

---------

Co-authored-by: default avatarcmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: default avatarGuillaume Thiolliere <guillaume.thiolliere@parity.io>
parent 6b980646
Branches
No related merge requests found
Pipeline #518391 waiting for manual action with stages
in 57 minutes and 5 seconds
title: 'Incrementable: return None instead of saturating'
doc:
- audience:
- Runtime Dev
- Runtime User
description: 'Fix implementation to follow the trait specification more closely. Closes #7845 '
crates:
- name: frame-support
bump: patch
......@@ -25,7 +25,7 @@ use scale_info::TypeInfo;
pub use sp_core::storage::TrackedStorageKey;
use sp_core::Get;
use sp_runtime::{
traits::{Convert, Member, Saturating},
traits::{Convert, Member},
DispatchError, RuntimeDebug,
};
......@@ -292,9 +292,7 @@ macro_rules! impl_incrementable {
$(
impl Incrementable for $type {
fn increment(&self) -> Option<Self> {
let mut val = self.clone();
val.saturating_inc();
Some(val)
self.checked_add(1)
}
fn initial_value() -> Option<Self> {
......@@ -332,6 +330,14 @@ mod tests {
use crate::BoundedVec;
use sp_core::{ConstU32, ConstU64};
#[test]
fn incrementable_works() {
assert_eq!(0u8.increment(), Some(1));
assert_eq!(1u8.increment(), Some(2));
assert_eq!(u8::MAX.increment(), None);
}
#[test]
fn linear_storage_price_works() {
type Linear = LinearStoragePrice<ConstU64<7>, ConstU64<3>, u64>;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment