Skip to content

ohaddahan/pure_functional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Functional Macro

Background

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.

How does it work?

  1. We parse each function arguments and check if they are mutable (&mut).
  2. We rewrite the function as with an internal module to prevent globals 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)
}

Known Limitations

  1. Any struct that internally hides mutability will not be caught by this macro.

    For example, the following struct will not be caught by this macro:

    • 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 this macro.

  2. async functions are not supported.

  3. &self and &mut self are not supported.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages