8000 Prevent unnecessary runtime validation for newtype generated macros by edmorley · Pull Request #331 · heroku/libcnb.rs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Prevent unnecessary runtime validation for newtype generated macros #331

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

Merged
merged 1 commit into from
Feb 22, 2022
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
1 change: 1 addition & 0 deletions libcnb-data/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add `#[must_use]` to `BuildPlan` and `BuildPlanBuilder` ([#288](https://github.com/Malax/libcnb.rs/pull/288)).
- Add `exec_d` module with types representing the output of an `exec.d` program ([#324](https://github.com/Malax/libcnb.rs/pull/324)).
- Increase minimum supported Rust version from 1.56 to 1.58 ([#318](https://github.com/Malax/libcnb.rs/pull/318)).
- Adjust newtype generated compile-time validation macros so that they don't also perform redundant validation at runtime. In cases where only compile-time validation is being performed (for example `exec.d` scripts), this results in a significant reduction in binary size. ([#331](https://github.com/Malax/libcnb.rs/pull/331))

## [0.4.0] 2022-01-14

Expand Down
15 changes: 14 additions & 1 deletion libcnb-data/src/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/// - [`Debug`]
/// - [`Display`](std::fmt::Display)
/// - [`Eq`]
/// - [`Hash`]
/// - [`PartialEq`]
/// - [`serde::Deserialize`]
/// - [`serde::Serialize`]
Expand Down Expand Up @@ -130,6 +131,18 @@ macro_rules! libcnb_newtype {
}
}

impl $name {
/// Construct an instance of this type without performing validation.
///
/// This should not be used directly, and is only public so that it
/// can be used by the compile-time validation macro.
#[must_use]
#[doc(hidden)]
pub fn new_unchecked(value: &str) -> Self {
Self(String::from(value))
}
}

#[macro_export]
$(#[$macro_attributes])*
macro_rules! $macro_name {
Expand All @@ -139,7 +152,7 @@ macro_rules! libcnb_newtype {
$value,
{
use $crate::$path as base;
$value.parse::<base::$name>().unwrap()
base::$name::new_unchecked($value)
},
compile_error!(concat!(
stringify!($value),
Expand Down
0