From 55373cb614dbafbcda40d76a2f4438a0056ae94e Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Sun, 25 Sep 2022 11:19:50 +0200 Subject: [PATCH] feat: add uint `IsNil` method (#13381) --- math/CHANGELOG.md | 1 + math/uint.go | 5 +++++ math/uint_test.go | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/math/CHANGELOG.md b/math/CHANGELOG.md index 4354b122f63a..3b7c8e741ff7 100644 --- a/math/CHANGELOG.md +++ b/math/CHANGELOG.md @@ -31,4 +31,5 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +* [#13381](https://github.com/cosmos/cosmos-sdk/pull/13381) Add uint `IsNil` method. * [#12634](https://github.com/cosmos/cosmos-sdk/pull/12634) Move `sdk.Dec` to math package, call it `LegacyDec`. diff --git a/math/uint.go b/math/uint.go index 8ce6105e9fb0..81da2d9ef8d5 100644 --- a/math/uint.go +++ b/math/uint.go @@ -18,6 +18,11 @@ func (u Uint) BigInt() *big.Int { return new(big.Int).Set(u.i) } +// IsNil returns true if Uint is uninitialized +func (u Uint) IsNil() bool { + return u.i == nil +} + // NewUintFromBigUint constructs Uint from big.Uint func NewUintFromBigInt(i *big.Int) Uint { u, err := checkNewUint(i) diff --git a/math/uint_test.go b/math/uint_test.go index 3077e5094181..428672cb2cea 100644 --- a/math/uint_test.go +++ b/math/uint_test.go @@ -91,6 +91,11 @@ func (s *uintTestSuite) TestUintPanics() { s.Require().True(sdkmath.ZeroUint().LTE(sdkmath.OneUint())) } +func (s *uintTestSuite) TestIsNil() { + s.Require().False(sdkmath.OneUint().IsNil()) + s.Require().True(sdkmath.Uint{}.IsNil()) +} + func (s *uintTestSuite) TestArithUint() { for d := 0; d < 1000; d++ { n1 := uint64(rand.Uint32())