Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lading_payload/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub(crate) mod config;
pub(crate) mod strings;
pub(crate) mod tags;
2 changes: 1 addition & 1 deletion lading_payload/src/common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ where
pub(crate) fn valid(&self) -> (bool, &'static str) {
match self {
Self::Constant(_) => (true, ""),
Self::Inclusive { min, max } => (min < max, "min must be less than max"),
Self::Inclusive { min, max } => (min <= max, "min must be less than or equal to max"),
}
}

Expand Down
23 changes: 19 additions & 4 deletions lading_payload/src/common/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub(crate) struct Pool {
}

/// Opaque 'str' handle for the pool
pub(crate) type Handle = (usize, usize);
///
// The pool will not index more than 2**32. Using a type smaller than `usize`
// allows a more compact representation.
pub(crate) type Handle = (u32, u32);

impl Pool {
/// Create a new instance of `Pool` with the default alpha-numeric character
Expand All @@ -31,6 +34,7 @@ impl Pool {
where
R: rand::Rng + ?Sized,
{
assert!(u32::try_from(bytes).is_ok());
Self::with_size_and_alphabet(rng, bytes, ALPHANUM)
}

Expand Down Expand Up @@ -97,10 +101,18 @@ impl Pool {
}

let max_lower_idx = self.inner.len() - bytes;
let lower_idx = rng.random_range(0..max_lower_idx);
let upper_idx = lower_idx + bytes;
let lower_idx: usize = rng.random_range(0..max_lower_idx);
let upper_idx: usize = lower_idx + bytes;

Some((&self.inner[lower_idx..upper_idx], (lower_idx, bytes)))
Some((
&self.inner[lower_idx..upper_idx],
(
lower_idx
.try_into()
.expect("must fit into u32 by construction"),
bytes.try_into().expect("must fit in u32 by construction"),
),
))
}

/// Return a `&str` from the interior storage with size selected from `bytes_range`. Result will
Expand All @@ -120,8 +132,11 @@ impl Pool {

/// Given an opaque handle returned from `*_with_handle`, return the &str it represents
#[must_use]
#[inline]
pub(crate) fn using_handle(&self, handle: Handle) -> Option<&str> {
let (offset, length) = handle;
let offset = offset as usize;
let length = length as usize;
if offset + length < self.inner.len() {
let str = &self.inner[offset..offset + length];
Some(str)
Expand Down
Loading
Loading