Skip to content

supraerror/single_line_macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

single_line_macro

A small procedural macro that lets you define one-line functions and methods using => expr syntax.

Features

  • Define methods with &self or &mut self, with any number of parameters.
  • Define associated functions and free functions, with or without the fn keyword.
  • Support for doc comments (///) — they will appear in generated documentation.
  • Automatically expands a single-field expression x into self.x within methods.
  • Zero-dependency (besides syn and quote).
  • Support async keyword

Installation

Add this to your crate’s Cargo.toml:

[dependencies]
single_line_macro = "0.2.x"

And in your code:

use single_line_macro::single_line;

Or rename it:

use single_line_macro::single_line as sl;
use single_line_macro::single_line;

struct Foo { x: i32 }

impl Foo {
    single_line![/// Creates a new `Foo` with the given `x`.
        pub new(x: i32) -> Self => Self { x }];
    
    single_line![/// Returns the current `x`.
        pub fn get_x(&self) -> i32 => self.x];

    
    single_line![/// Multiplies `x` by `m`.
        pub fn mult(&self, m: i32) -> i32 => self.x * m];

    
    single_line![/// Resets `x` to zero.
        pub fn reset(&mut self) -> () => { self.x = 0; }];
    
    single_line![/// Multiplies async
        pub async mult_async(&self, m: i32) -> i32 => self.x * m];
}

// Free functions:
single_line![pub answer -> i32 => 42];
single_line![greet(name: &str) -> String => format!("Hello, {}", name)];

more examples in the test folder

About

A macro in rust to create single line methods and functions in a more readable way

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages