Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Burka durka

View GitHub Profile
View long_line
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 Jan 6, 2016 — forked from anonymous/playground.rs
Macro for pre-computing method arguments to avoid borrowck errors
View unborrow.rs
/// 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 Dec 22, 2015 — forked from anonymous/playground.rs
ternary operator
View trn.rs
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 Oct 19, 2020 — forked from anonymous/playground.rs
Rust macro that splits a list of expressions in half (at compile time)
View halve.rs
/// 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 Oct 8, 2015 — forked from anonymous/playground.rs
Shared via Rust Playground
View playground.rs
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 Oct 6, 2015 — forked from anonymous/playground.rs
let...else macro
View guard.rs
#![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 Sep 24, 2015 — forked from anonymous/playground.rs
Rust tt-muncher macro implementing C++-like visibility labels for struct members
View sticky_visibility.rs
/// 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 Oct 1, 2015 — forked from anonymous/playground.rs
Signaling-NaN simulator wrapper for f32/f64
View signaling.rs
#[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 Oct 1, 2015 — forked from anonymous/playground.rs
Rust clone_army! macro
View clone_army.rs
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
  • 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