Skip to content

Commit

Permalink
Add convenient constructors for status lists. (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Jul 3, 2024
1 parent 10deab4 commit 0567fda
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
44 changes: 44 additions & 0 deletions crates/claims/crates/status/src/impl/bitstream_status_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,50 @@ impl BitString {
}
}

/// Creates a new bit-string of the given length, using `f` to initialize
/// every status.
///
/// The `f` function is called with the index of the initialized status.
pub fn new_with(
status_size: StatusSize,
len: usize,
mut f: impl FnMut(usize) -> u8,
) -> Result<Self, Overflow> {
let mut result = Self::with_capacity(status_size, len);

for i in 0..len {
result.push(f(i))?;
}

Ok(result)
}

/// Creates a new bit-string of the given length, setting every status
/// to the same value.
pub fn new_with_value(
status_size: StatusSize,
len: usize,
value: u8,
) -> Result<Self, Overflow> {
Self::new_with(status_size, len, |_| value)
}

/// Creates a new bit-string of the given length, setting every status
/// to 0.
pub fn new_zeroed(status_size: StatusSize, len: usize) -> Self {
Self::new_with_value(status_size, len, 0).unwrap() // 0 cannot overflow.
}

/// Creates a new bit-string with the given status size and capacity
/// (in number of statuses).
pub fn with_capacity(status_size: StatusSize, capacity: usize) -> Self {
Self {
status_size,
bytes: Vec::with_capacity((capacity * status_size.0 as usize).div_ceil(8)),
len: 0,
}
}

/// Creates a bit-string from a byte array and status size.
pub fn from_bytes(status_size: StatusSize, bytes: Vec<u8>) -> Self {
let len = bytes.len() * 8usize / status_size.0 as usize;
Expand Down
48 changes: 48 additions & 0 deletions crates/claims/crates/status/src/impl/token_status_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,54 @@ impl BitString {
}
}

/// Creates a new status list of the given length, using `f` to initialize
/// every status.
///
/// The `f` function is called with the index of the initialized status.
pub fn new_with(
status_size: StatusSize,
len: usize,
mut f: impl FnMut(usize) -> u8,
) -> Result<Self, Overflow> {
let mut result = Self::with_capacity(status_size, len);

for i in 0..len {
result.push(f(i))?;
}

Ok(result)
}

/// Creates a new status list of the given length, setting every status
/// to the same value.
pub fn new_with_value(
status_size: StatusSize,
len: usize,
value: u8,
) -> Result<Self, Overflow> {
Self::new_with(status_size, len, |_| value)
}

/// Creates a new status list of the given length, setting every status
/// to 0.
///
/// This is an alias for [`Self::new_valid`].
pub fn new_zeroed(status_size: StatusSize, len: usize) -> Self {
Self::new_valid(status_size, len)
}

/// Creates a new status list of the given length, setting every status
/// to [`VALID`].
pub fn new_valid(status_size: StatusSize, len: usize) -> Self {
Self::new_with_value(status_size, len, VALID).unwrap() // `VALID` cannot overflow.
}

/// Creates a new status list of the given length, setting every status
/// to [`INVALID`].
pub fn new_invalid(status_size: StatusSize, len: usize) -> Self {
Self::new_with_value(status_size, len, INVALID).unwrap() // `INVALID` cannot overflow.
}

/// Creates a new status list with the given status size and capacity
/// (in number of statuses).
pub fn with_capacity(status_size: StatusSize, capacity: usize) -> Self {
Expand Down

0 comments on commit 0567fda

Please sign in to comment.