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

Usage of inline const for static assertions #235

Open
hinto-janai opened this issue Jul 25, 2024 · 2 comments
Open

Usage of inline const for static assertions #235

hinto-janai opened this issue Jul 25, 2024 · 2 comments
Labels
C-proposal A proposal of some kind, and a request for comments. E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Medium difficulty. Experience needed to fix: Intermediate. P-low Low priority.

Comments

@hinto-janai
Copy link
Contributor

What

Rust 1.79 brought inline const expressions.

These can be used to conditionally make static assertions (among other things).

Why

Turns runtime panics into compile time errors.

Where/How

Certain runtime panics can be replaced with these new inline const expressions, e.g.:

fn resize_map(&self, resize_algorithm: Option<ResizeAlgorithm>) -> NonZeroUsize {
unreachable!()
}

Can be turned into:

fn resize_map(&self, resize_algorithm: Option<ResizeAlgorithm>) -> NonZeroUsize {
    const {
        assert!(
            Self::MANUAL_RESIZE,
            "This function should not be called as this database backend automatically resizes.",
        )
    }
}

This will make calling resize() from database backends that shouldn't be calling resize_map() in the first place to fail at compile time instead of at runtime.

The const expression will only be evaluated if resize() is called, e.g.:

fn main() {}

fn f<const B: bool>() {
    const {
        assert!(B);
    }
}

This program will only fail if f::<false>() is called. It is commented out (not called) so it compiles fine.

@hinto-janai hinto-janai added the C-proposal A proposal of some kind, and a request for comments. label Jul 25, 2024
@hinto-janai hinto-janai added E-medium Medium difficulty. Experience needed to fix: Intermediate. P-low Low priority. E-help-wanted Call for participation: Help is requested to fix this issue. labels Oct 22, 2024
@antonio-hickey
Copy link

Hello, anyone working on this?

I'll take it on if not

@SyntheticBird45
Copy link
Member

Hi @antonio-hickey, No one is working on it. You are welcome to take it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-proposal A proposal of some kind, and a request for comments. E-help-wanted Call for participation: Help is requested to fix this issue. E-medium Medium difficulty. Experience needed to fix: Intermediate. P-low Low priority.
Projects
None yet
Development

No branches or pull requests

3 participants