| Crates.io | single_line_macro |
| lib.rs | single_line_macro |
| version | 0.2.2 |
| created_at | 2025-05-17 16:45:39.323505+00 |
| updated_at | 2025-06-12 16:00:57.245401+00 |
| description | A macro to create methods and functions on a single line in a more readable way. |
| homepage | |
| repository | https://github.com/philou404/single_line_macro/ |
| max_upload_size | |
| id | 1677989 |
| size | 9,794 |
A small procedural macro that lets you define one-line functions and methods using => expr syntax.
&self or &mut self, with any number of parameters.fn keyword.///) — they will appear in generated documentation.x into self.x within methods.syn and quote).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