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

Allow to invoke some functions as const fn #43

merged 1 commit into from Dec 11, 2022


Copy link

This change enables to invoke functions marked as const in this change in const context.

By this change, for example, we can construct Ipv6Net or Ipv4Net as a global constant during a compile time. This is useful when we crate a giant table.

Copy link

Thank you @tetsuharuohzeki, I was about to create a similar PR myself.

@krisprice Would you consider merging this? I was able to merge master into this branch without any conflicts and the tests are passing.

This would be useful for declaring constants. Currently, I have

#[derive(Debug, Clone, Copy)]
pub struct Configuration {
    pub something: ...,
    pub bind_address: Ipv4Addr,
    pub remote_network: Ipv4Net,

I cannot create a constant holding example configuration:

pub const SIMULATOR_CONFIG: Configuration = Configuration {
  something: 123, // this works
  bind_address: Ipv4Addr::new(192, 168, 10, 1), // this works, Ipv4Addr::new is const
  remote_network: Ipv4Net::new( // This does not work, Ipv4Net::new is not const
    Ipv4Addr::new(10, 0, 0, 0),

Instead, I have to create a function to return a simple configuration like this. Having Ipv4Net::new as const would make this easier and cleaner.

@krisprice krisprice merged commit 029948a into krisprice:master Dec 11, 2022
Copy link

Sorry I meant to include it in the last release. Merged and new crate published. Thanks guys.

@tetsuharuohzeki tetsuharuohzeki deleted the const_fn branch December 12, 2022 16:09
Copy link
Contributor Author

@krisprice thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants