#macro #getter #single-line

macro single_line_macro

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

1 unstable release

Uses new Rust 2024

new 0.1.0 May 17, 2025

#540 in Procedural macros

MIT license

7KB
87 lines

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).

Installation

Add this to your crate’s Cargo.toml:

[dependencies]
single_line_macro = "0.1"

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 {
    /// Creates a new `Foo` with the given `x`.
    single_line![pub new(x: i32) -> Self => Self { x }];

    /// Returns the current `x`.
    single_line![pub fn get_x(&self) -> i32 => self.x];

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

    /// Resets `x` to zero.
    single_line![pub fn reset(&mut self) -> () => { self.x = 0; }];
}

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

more examples in the test folder

Dependencies

~200–630KB
~15K SLoC