#literals #structure #value #macro #data-structures

macro derive_lit

A tool to auto-generate literal macros for your data structure

1 unstable release

0.1.0 Oct 28, 2019

#12 in #literal

MIT license

10KB
110 lines

derive_lit

Are you developing a data structure?

struct GroceryList {
	num_items: usize,
	item_ids: Vec<usize>
}

And your data structure lets you add data to it?

impl GroceryList {
	fn new() -> Self {
		Self {
			num_items: 0,
			item_ids: vec![]
		}
	}

	fn push(&mut self, item_id: usize) {
		self.item_ids.push(item_id);
	}
}

Wouldn't it be cool if you could do this?

fn main() {
	let groceries = grocery_list![
		0,
		9,
		8,
		4
	];

	// do something intersting with your GroceryList...
}

What if you could just...

use derive_lit::VecLit;

#[derive(VecLit)]
struct GroceryList {
	num_items: usize,
	item_ids: Vec<usize>
}

You can! Use derive_lit::*. Just a derive_lit = "0.1.0" away!


lib.rs:

This is a crate for automatically generating macros that expand to literal values of your data structure. Here's an example.

use derive_lit::VecLit;

#[derive(VecLit)]
struct GroceryList {
	num_items: usize,
	item_ids: Vec<usize>
}

impl GroceryList {
	fn new() -> Self {
		Self {
			num_items: 0,
			item_ids: vec![]
		}
	}

	fn push(&mut self, item_id: usize) {
		self.item_ids.push(item_id);
	}
}

fn main() {
	let groceries = grocery_list![
		0,
		9,
		8,
		5
	];

	// do something intersting with your GroceryList...
}

Dependencies

~2MB
~44K SLoC