Skip to content

Commit

Permalink
Add is_empty() to newtypes (#206)
Browse files Browse the repository at this point in the history
* typedefs: Add func_is_empty()! and add it to all newtypes that implement func_len()!

* pwhash: Add is_empty() to PasswordHash

* typedefs: 'a' - > 'an'
  • Loading branch information
brycx authored Jun 10, 2021
1 parent 2224d0c commit d907024
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/high_level/pwhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,17 @@ impl PasswordHash {
pub fn len(&self) -> usize {
self.password_hash.len()
}

#[inline]
/// Return `true` if the password hash is empty, `false` otherwise.
///
/// __NOTE__: This method should always return `false`, since there shouldn't be a way
/// to create an empty password hash.
pub fn is_empty(&self) -> bool {
debug_assert_eq!(self.encoded_password_hash.is_empty(), self.password_hash.is_empty(),
"Both the encoded password hash and the raw hash must be non-empty or empty at the same time.");
self.password_hash.is_empty()
}
}

impl core::fmt::Debug for PasswordHash {
Expand Down
25 changes: 25 additions & 0 deletions src/typedefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ macro_rules! func_len (() => (
}
));

/// Macro to implement an `is_empty()` function which will return `true` if `self.len() == 0`.
macro_rules! func_is_empty (() => (
#[inline]
/// Return `true` if this object does not hold any data, `false` otherwise.
///
/// __NOTE__: This method should always return `false`, since there shouldn't be a way
/// to create an empty instance of this object.
pub fn is_empty(&self) -> bool {
self.original_length == 0
}
));

/// Macro to implement a `generate()` function for objects that benefit from
/// having a CSPRNG available to generate data of a fixed length $gen_length.
macro_rules! func_generate (($name:ident, $upper_bound:expr, $gen_length:expr) => (
Expand Down Expand Up @@ -290,6 +302,9 @@ macro_rules! test_as_bytes_and_get_length (($name:ident, $lower_bound:expr, $upp
assert!(test_lower.$bytes_function().len() == test_lower.len());
assert!(test_lower.len() == $lower_bound);

assert_eq!(test_upper.is_empty(), false);
assert_eq!(test_lower.is_empty(), false);

// Test non-fixed-length definitions
if $lower_bound != $upper_bound {
let test_upper = $name::from_slice(&[0u8; $upper_bound - 1]).unwrap();
Expand All @@ -300,6 +315,9 @@ macro_rules! test_as_bytes_and_get_length (($name:ident, $lower_bound:expr, $upp

assert!(test_lower.$bytes_function().len() == test_lower.len());
assert!(test_lower.len() == $lower_bound + 1);

assert_eq!(test_upper.is_empty(), false);
assert_eq!(test_lower.is_empty(), false);
}
}
));
Expand Down Expand Up @@ -434,6 +452,7 @@ macro_rules! construct_secret_key {
func_unprotected_as_bytes!();
func_generate!($name, $upper_bound, $gen_length);
func_len!();
func_is_empty!();
}

#[cfg(test)]
Expand Down Expand Up @@ -490,6 +509,7 @@ macro_rules! construct_public {
impl $name {
func_from_slice!($name, $lower_bound, $upper_bound);
func_len!();
func_is_empty!();
}

#[cfg(test)]
Expand Down Expand Up @@ -530,6 +550,7 @@ macro_rules! construct_public {
func_from_slice!($name, $lower_bound, $upper_bound);
func_generate!($name, $upper_bound, $gen_length);
func_len!();
func_is_empty!();
}

#[cfg(test)]
Expand Down Expand Up @@ -596,6 +617,7 @@ macro_rules! construct_tag {
func_from_slice!($name, $lower_bound, $upper_bound);
func_unprotected_as_bytes!();
func_len!();
func_is_empty!();
}

#[cfg(test)]
Expand Down Expand Up @@ -676,6 +698,7 @@ macro_rules! construct_hmac_key {
func_unprotected_as_bytes!();
func_generate!($name, $size, $size);
func_len!();
func_is_empty!();
}

#[cfg(test)]
Expand Down Expand Up @@ -752,6 +775,7 @@ macro_rules! construct_secret_key_variable_size {
func_from_slice_variable_size!($name);
func_unprotected_as_bytes!();
func_len!();
func_is_empty!();
func_generate_variable_size!($name);
}

Expand Down Expand Up @@ -790,6 +814,7 @@ macro_rules! construct_salt_variable_size {
impl $name {
func_from_slice_variable_size!($name);
func_len!();
func_is_empty!();
func_generate_variable_size!($name);
}

Expand Down

0 comments on commit d907024

Please sign in to comment.