diff --git a/CHANGELOG.md b/CHANGELOG.md index cdb8ec91a..d074c7244 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed build error that would occur on Windows systems. - Added path iteration utilities ([#47][]) +## Changed + +- Enforced const evaluation for `path!`. + [#47]: https://github.com/trussed-dev/littlefs2/pull/47 [#57]: https://github.com/trussed-dev/littlefs2/pull/57 diff --git a/src/lib.rs b/src/lib.rs index a7a78e2b5..8186606d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -165,7 +165,7 @@ pub struct Version { /// Creates a path from a string without a trailing null. /// -/// Panics if the string contains null bytes or non-ascii characters. +/// Panics and causes a compiler error if the string contains null bytes or non-ascii characters. /// /// # Examples /// @@ -173,6 +173,7 @@ pub struct Version { /// use littlefs2::{path, path::Path}; /// /// const HOME: &Path = path!("/home"); +/// let root = path!("/"); /// ``` /// /// Illegal values: @@ -186,11 +187,21 @@ pub struct Version { /// # use littlefs2::{path, path::Path}; /// const WITH_UTF8: &Path = path!("/höme"); // does not compile /// ``` +/// +/// The macro enforces const evaluation so that compilation fails for illegal values even if the +/// macro is not used in a const context: +/// +/// ```compile_fail +/// # use littlefs2::path; +/// let path = path!("te\0st"); // does not compile +/// ``` #[macro_export] macro_rules! path { - ($path:literal) => { - $crate::path::Path::from_str_with_nul(::core::concat!($path, "\0")) - }; + ($path:literal) => {{ + const _PATH: &$crate::path::Path = + $crate::path::Path::from_str_with_nul(::core::concat!($path, "\0")); + _PATH + }}; } #[cfg(test)]