Skip to content

Commit

Permalink
const fn-ify what can be const of the API
Browse files Browse the repository at this point in the history
This helps to remove lazy initialization to initialize
global static variables containing `FxHashMap`/`FxHashSet`.
  • Loading branch information
korken89 committed Feb 28, 2024
1 parent 77c651c commit d44be4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const K: usize = 0x517cc1b727220a95;
fn take_first_chunk<'a, const N: usize>(slice: &mut &'a [u8]) -> Option<&'a [u8; N]> {
// TODO: use [T]::split_first_chunk() when stable
if slice.len() < N {
return None
return None;
}

let (first, rest) = slice.split_at(N);
Expand All @@ -94,15 +94,20 @@ fn take_first_chunk<'a, const N: usize>(slice: &mut &'a [u8]) -> Option<&'a [u8;

impl FxHasher {
/// Creates `fx` hasher with a given seed.
pub fn with_seed(seed: usize) -> FxHasher {
pub const fn with_seed(seed: usize) -> FxHasher {
FxHasher { hash: seed }
}

/// Create a default `fq` hasher.
pub const fn default() -> FxHasher {
FxHasher { hash: 0 }
}
}

impl Default for FxHasher {
#[inline]
fn default() -> FxHasher {
FxHasher { hash: 0 }
Self::default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/seeded_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct FxSeededState {

impl FxSeededState {
/// Constructs a new `FxSeededState` that is initialized with a `seed`.
pub fn with_seed(seed: usize) -> FxSeededState {
pub const fn with_seed(seed: usize) -> FxSeededState {
Self { seed }
}
}
Expand Down

0 comments on commit d44be4b

Please sign in to comment.