#reference #mutable #self #mut #generate #fn

nightly macro fn_mut

fn_mut macro generates function which takes mutable reference to self and returns mutable reference

1 unstable release

Uses old Rust 2015

0.1.0 Mar 11, 2017

#38 in #fn

MIT license

10KB
201 lines

fn_mut macro

Build Status

fn_mut macro generates function which takes mutable reference to self and returns mutable reference.


#[fn_mut(disable_self, disable_output, enable_attrs = "attr1,attr2,...")]
fn(attr1: &T1, attr2: &T2, ...) -> OutT { ... }


lib.rs:

Usage


#[fn_mut(disable_self, disable_output, enable_attrs = "attr1,attr2,...")]
fn(attr1: T1, attr2: T2, ...) -> OutT { ... }

By default fn_mut macro generates function which takes mutable reference to self and returns mutable reference, if in original function there is any reference.

Possible options are:

  • disable_self: do not change &self to &mut self
  • disable_output: do not change output value
  • enable_attrs = "attr1,attr2,...": change mutability in attr

Requires nightly compiler and #![feature(proc_macro)].

Examples


#![feature(proc_macro)]

extern crate fn_mut;
use fn_mut::fn_mut;

struct Test(u64);

impl Test {
    #[fn_mut(enable_attrs = "text")]
    fn test(&self, text: &str) -> Option<&u64> {
        if_mut! {
            println!("This is mut fn: {}", text);
        }
        if_const! {
            println!("This is const fn: {}", text);
        }
        Some(ptr!(self.0))
    }
}

This example expands to:


struct Test(u64);

impl Test {
    fn test(&self, text: &str) -> Option<&u64> {
        println!("This is const fn: {}", text);
        Some(&self.0)
    }
    fn test_mut(&mut self, text: &mut str) -> Option<&mut u64> {
        println!("This is mut fn: {}", text);
        Some(&mut self.0)
    }
}

Dependencies

~5MB
~115K SLoC