Skip to content

Instantly share code, notes, and snippets.

View durka's full-sized avatar

Alex Burka durka

View GitHub Profile
@durka
durka / playground.rs
Last active August 29, 2015 14:25 — forked from anonymous/playground.rs
Fixed version of Nashenas88's test-case macro
macro_rules! as_item { ($i:item) => ($i) } // <-- empirically, this is needed to placate the compiler
macro_rules! test_cases {
(
$test_name:ident
$param_names:tt, // <-- just treat the param names as a tuple (type 'tt' to delay parsing)
[$($case_name:ident : $test_params:tt)+] // <-- same for the param values
$test_code:block
) => (as_item!(
#[cfg(test)]
pub mod $test_name {
@durka
durka / munch.rs
Created July 22, 2015 21:42 — forked from anonymous/playground.rs
Simple TT muncher
// switch to nightly and uncomment these for debugging
//#![feature(trace_macros)]
//trace_macros!(true);
/// Trick the macro parser into outputting an item without checking its syntax
macro_rules! as_item( ($i:item) => ($i) );
macro_rules! foo(
// first rule: define the invocation syntax and call myself again with some
// easily parseable parameters (notice everything is bracketed)
@durka
durka / star.rs
Created July 31, 2015 16:09 — forked from anonymous/playground.rs
Rust star operator
trait Star<Ret> {
type F: ?Sized;
fn star(self, f: &Self::F) -> Ret;
}
macro_rules! star_impl {
($($n:ident),*) => {
impl<Ret,$($n),*> Star<Ret> for ($($n,)*) {
type F = Fn($($n),*) -> Ret;
@durka
durka / readext.rs
Last active August 29, 2015 14:26 — forked from anonymous/playground.rs
read_to_vec
use std::io::{Cursor, Read, self};
trait ReadExt {
fn read_to_vec(&mut self, buf: &mut Vec<u8>) -> io::Result<usize>;
}
impl<T: Read> ReadExt for T {
fn read_to_vec(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.take(buf.capacity() as u64).read_to_end(buf)
}
  • 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
@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 / 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) => {
@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 / 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 / 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();