Add `AccountId::{zero, next}` convenience methods

This commit is contained in:
Jack Grigg 2023-12-06 14:43:59 +00:00
parent a4c4c72174
commit 32976af62a
1 changed files with 21 additions and 1 deletions

View File

@ -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> {
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([