Skip to content

Instantly share code, notes, and snippets.

View durka's full-sized avatar

Alex Burka durka

View GitHub Profile
SystemTime::now().duration_since(UNIX_EPOCH
+ Duration::from_secs(process.start_time))
.unwrap_or(Duration::from_secs(0))
.as_secs()
@durka
durka / unborrow.rs
Last active January 6, 2016 04:19 — forked from anonymous/playground.rs
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
durka / trn.rs
Created December 22, 2015 21:23 — forked from anonymous/playground.rs
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
durka / halve.rs
Last active October 19, 2020 23:15 — forked from anonymous/playground.rs
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
durka / playground.rs
Last active October 8, 2015 17:53 — forked from anonymous/playground.rs
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
durka / guard.rs
Last active October 6, 2015 16:28 — forked from anonymous/playground.rs
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
durka / sticky_visibility.rs
Last active September 24, 2015 03:33 — forked from anonymous/playground.rs
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 {
// START INTERNAL RULES
// defeat the parser
(@as_item $i:item) => ($i);
@durka
durka / signaling.rs
Last active October 1, 2015 16:44 — forked from anonymous/playground.rs
Signaling-NaN simulator wrapper for f32/f64
#[cfg(debug_assertions)] use std::ops::*;
#[cfg(debug_assertions)] use std::{f32, f64, fmt};
#[cfg(debug_assertions)]
#[derive(Copy, Clone, Debug)]
struct Signaling<T>(T);
#[cfg(debug_assertions)]
impl<T: fmt::Display> fmt::Display for Signaling<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@durka
durka / clone_army.rs
Last active October 1, 2015 05:54 — forked from anonymous/playground.rs
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) => {
  • PREFACE

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