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

Enforce const evaluation in path! macro #60

Merged
merged 1 commit into from
May 24, 2024

Conversation

robin-nitrokey
Copy link
Member

By using a temporary const value, we can enforce that the path! macro always causes a compiler error for illegal values even if it is not used in a const context. This makes it easier to write correct code.

By using a temporary const value, we can enforce that the path! macro
always causes a compiler error for illegal values even if it is not used
in a const context.  This makes it easier to write correct code.
Copy link
Contributor

@sosthene-nitrokey sosthene-nitrokey left a comment

Choose a reason for hiding this comment

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

With 1.78 it will also be possible to use an inline const:

[macro_export]
macro_rules! path {
    ($path:literal) => {
        const { $crate::path::Path::from_str_with_nul(::core::concat!($path, "\0")) }
    };
}

@robin-nitrokey
Copy link
Member Author

Right, but I would prefer to not break compatibility with older Rust versions for this. Or is there a benefit to the const block other than better readability?

@sosthene-nitrokey
Copy link
Contributor

sosthene-nitrokey commented May 24, 2024

Certainly not worth using in this case where the current solution works well. The only benefit would be the ability to use it in match statements but I don't think is ever useful.

@robin-nitrokey robin-nitrokey merged commit 498fee7 into trussed-dev:main May 24, 2024
4 checks passed
@robin-nitrokey robin-nitrokey deleted the path-const branch May 24, 2024 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants