This macro
is aimed to help enforce pure functions
.
While it does have known limitations (see below), we still believe it provides value to help keep mutation
in check.
- We parse each
function
arguments and check if they aremutable
(&mut
). - We rewrite the
function
as with an internalmodule
to preventglobals
usage.
Original:
#[pure_functional]
fn foo(arg: i32) -> i32 {
arg + 1
}
Rewrite:
fn foo(arg: i32) -> i32 {
mod inner {
pub fn foo(arg: i32) -> i32 {
arg + 1
}
}
inner::foo(arg)
}
-
Any
struct
that internally hidesmutability
will not be caught by thismacro
.For example, the following
struct
will not be caught by thismacro
:Arc<Mutex<T>>
.Cell<T>
.RefCell<T>
.RwLock<T>
.UnsafeCell<T>
.
All of these wrap
UnsafeCell<T>
internally, which is why they are not caught by thismacro
. -
async
functions are not supported. -
&self
and&mut self
are not supported.