Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const fn-ify what can be const of the API #31

Merged
merged 1 commit into from
Feb 29, 2024
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
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: fq

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: what's the point of creating a default function in this impl when Default is already impl'd? I'm actually surprised this even works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point is that it can be const. I think a different name would be nicer though.

Copy link
Member

@fmease fmease Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default is not a #[const_trait] yet and feature const_trait_impl/effects is still very much WIP. The inherent associated function default can be used in const contexts contrary to the trait associated function. Inherent associated items have precedence over their trait counterpart.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, it could be called new

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
Loading