From 32976af62afa5b9c44393d0cb54f9e0dc02ad46f Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 6 Dec 2023 14:43:59 +0000 Subject: [PATCH] Add `AccountId::{zero, next}` convenience methods --- src/lib.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 18f8825..68aed68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,6 +59,16 @@ impl ConditionallySelectable for AccountId { } } +impl AccountId { + /// The ID for account zero (the first account). + pub const ZERO: Self = Self(0); + + /// Returns the next account ID in sequence, or `None` on overflow. + pub fn next(&self) -> Option { + Self::try_from(self.0 + 1).ok() + } +} + /// The error type returned when a checked integral type conversion fails. #[derive(Clone, Copy, Debug)] pub struct TryFromIntError(()); @@ -264,9 +274,19 @@ memuse::impl_no_dynamic_usage!(Scope); #[cfg(test)] mod tests { - use super::DiversifierIndex; + use super::{AccountId, DiversifierIndex}; + use assert_matches::assert_matches; + #[test] + fn account_id_next() { + let zero = AccountId::ZERO; + assert_eq!(zero.next(), AccountId::try_from(1).ok()); + + let max_id = AccountId::try_from((1 << 31) - 1).unwrap(); + assert_eq!(max_id.next(), None); + } + #[test] fn diversifier_index_to_u32() { let two = DiversifierIndex([