Skip to content

Commit

Permalink
Add AccountId::{zero, next} convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Dec 6, 2023
1 parent a4c4c72 commit 32976af
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
Expand Down Expand Up @@ -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([
Expand Down

0 comments on commit 32976af

Please sign in to comment.