Skip to content

Instantly share code, notes, and snippets.


Alex Burka durka

View GitHub Profile
View long_line
+ Duration::from_secs(process.start_time))
durka /
Last active Jan 6, 2016 — forked from anonymous/
Macro for pre-computing method arguments to avoid borrowck errors
/// Precompute a method's arguments before the call so that borrowck sees
/// them the same way that trans does.
macro_rules! unborrow {
// this rule fires when we have parsed all the arguments
// fall through to output stage
(@parse () -> ($names:tt $lets:tt) $($thru:tt)*) => {
unborrow!(@out $names $lets $($thru)*)
// parse an argument and continue parsing
// this is the key rule, assigning a name for the argument and generating the let statement
durka /
Created Dec 22, 2015 — forked from anonymous/
ternary operator
macro_rules! trn {
(@parse () -> (($a:expr) ($b:expr) ($c:expr))) => {
if $a { $b } else { $c }
(@parse (? $head:tt $($tail:tt)*) -> ($a:tt () ())) => {
trn!(@parse ($($tail)*) -> ($a ($head) ()))
(@parse (: $head:tt $($tail:tt)*) -> ($a:tt $b:tt ())) => {
trn!(@parse ($($tail)*) -> ($a $b ($head)))
durka /
Last active Oct 19, 2020 — forked from anonymous/
Rust macro that splits a list of expressions in half (at compile time)
/// strategy:
/// - first copy the list of exprs -- the copy will just be used as a counter
/// - then, starting with all the exprs in a "left" list and an empty "right" list:
/// - move one expr at a time from "left" to "right", and decrement the counter _twice_
/// - when the counter is zero, the halving is complete
/// - if there were an odd number of exprs, a compile error tells you about it
/// the macro is in continuation passing style, so you pass in a macro invocation and it will be called
/// twice, first with the "left" and then with the "right" (enclosed in square brackets) inserted as the last argument
macro_rules! halve {
// internal rules
durka /
Last active Oct 8, 2015 — forked from anonymous/
Shared via Rust Playground
fn main() {
let values = vec![1, 2, 3];
for x in values {
println!("{}", x);
// Rough translation of the iteration without a `for` iterator.
let values = vec![1, 2, 3];
let mut it = values.into_iter();
durka /
Last active Oct 6, 2015 — forked from anonymous/
let...else macro
#![allow(dead_code, unused_variables)]
//#![feature(trace_macros)] trace_macros!(true);
// strategy: scan the pattern for idents and pull them out, so we can create a let statement in the enclosing scope
macro_rules! guard {
(@as_stmt $s:stmt) => { $s };
(@collect () -> ($($idents:ident)*), [($pattern:pat) ($rhs:expr) ($diverge:expr)]) => {
guard!(@as_stmt let ($($idents,)*) = if let $pattern = $rhs { ($($idents,)*) } else { $diverge })
durka /
Last active Sep 24, 2015 — forked from anonymous/
Rust tt-muncher macro implementing C++-like visibility labels for struct members
/// Wrapper for a struct declaration using C++-like "pub:" and "priv:" labels instead of Rust's individual member annotations
/// Syntax is similar to a normal pub struct declaration (see example below)
/// The struct is given an automatic pub fn new() method which simply takes all members in order -- without this, there would be no way to construct an instance due to the private members
macro_rules! sticky_visibility {
// defeat the parser
(@as_item $i:item) => ($i);
durka /
Last active Oct 1, 2015 — forked from anonymous/
Signaling-NaN simulator wrapper for f32/f64
#[cfg(debug_assertions)] use std::ops::*;
#[cfg(debug_assertions)] use std::{f32, f64, fmt};
#[derive(Copy, Clone, Debug)]
struct Signaling<T>(T);
impl<T: fmt::Display> fmt::Display for Signaling<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
durka /
Last active Oct 1, 2015 — forked from anonymous/
Rust clone_army! macro
use std::sync::Arc;
macro_rules! clone_army {
($vars:tt | | $body:expr) => {
clone_army!(@emit $vars [] [] $body)
($vars:tt move | | $body:expr) => {
clone_army!(@emit $vars [] [move] $body)
($vars:tt || $body:expr) => {
View gist:ee8ed545dd8d75263703

This is a distilled overview of the Lojban language.

Major concepts of the language are introduced by saying as much with as little as possible.

That is to say:

  • For each concept the most crucial aspects are presented