single_line_macro

Crates.iosingle_line_macro
lib.rssingle_line_macro
version0.2.2
created_at2025-05-17 16:45:39.323505+00
updated_at2025-06-12 16:00:57.245401+00
descriptionA macro to create methods and functions on a single line in a more readable way.
homepage
repositoryhttps://github.com/philou404/single_line_macro/
max_upload_size
id1677989
size9,794
(supraerror)

documentation

README

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

Commit count: 16

cargo fmt