Crates.io | with_closure |
lib.rs | with_closure |
version | 0.1.2 |
source | src |
created_at | 2023-07-21 04:27:29.909185 |
updated_at | 2023-07-23 06:35:50.786083 |
description | Ensure that the `noalias` optimization takes effect by expanding to closure call |
homepage | |
repository | https://github.com/823984418/with_closure |
max_upload_size | |
id | 922077 |
size | 7,700 |
Ensure that the noalias
optimization takes effect by expanding to closure call.
This library only contains one macro definition, but the first 12 items have been expanded to ensure parameter transfer.
#[doc(hidden)]
#[inline(always)]
pub fn with<A, F: FnOnce(A) -> R, R>(a: A, f: F) -> R {
f(a)
}
#[macro_export]
macro_rules! with {
($($a:pat = $va:expr,)* $f:block) => {
$crate::with(($($va,)*), |($($a,)*)| $f)
};
}
Due to compiler limitations, some code cannot achieve complete alias optimization.
pub fn foo(mut x: Vec<i32>) {
x[0] = 1;
println!("do something");
if x[0] != 1 {
println!("branch");
}
}
The compiler cannot delete the branch.
After passing a function, the compiler learned about this.
pub fn foo(mut x: Vec<i32>) {
with!(x = x.as_mut_slice(), {
x[0] = 1;
println!("do something");
if x[0] != 1 {
println!("branch");
}
});
}
Branch deleted.