Skip to content

Instantly share code, notes, and snippets.

View durka's full-sized avatar

Alex Burka durka

View GitHub Profile
@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)
}
@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 / countmacro.rs
Last active April 13, 2016 23:50 — forked from anonymous/playground.rs
Rust macros: counting with an accumulator
// cargo-deps: lazy_static
#[macro_use] extern crate lazy_static;
macro_rules! declare_array {
// INTERNAL
// last element (proceed to output)
(@parse $size:expr, ($val:expr) -> [$($accs:expr),*] $thru:tt) => {
declare_array!(@output $size + 1usize, [$($accs,)* $val] $thru);
@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 / 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 / playground.rs
Created July 8, 2015 04:26 — forked from anonymous/playground.rs
Syntactic sugar for getters + setters
trait GetSet<G,S> {
fn get(&self) -> G;
fn set(&self) -> S;
}
macro_rules! field {
($s:ident . $f:ident) => ($s.get().$f(&$s));
($s:ident . $f:ident = $v:expr) => ($s.set().$f(&mut $s, $v));
(
@durka
durka / playground.rs
Created June 25, 2015 20:42 — forked from anonymous/playground.rs
Macro capturing generic parameters
#![allow(dead_code)]
macro_rules! as_item { ($i:item) => ($i) }
macro_rules! s {
($t:ident) => {
impl $t {
}
};
($t:ident<$($x:tt),+>) => {
@durka
durka / associated_const.rs
Last active August 29, 2015 14:23 — forked from anonymous/playground.rs
Hack to have something like associated consts in stable
trait Associated<T> {
fn associated(&self) -> T;
}
trait Thing : Associated<&'static str> {
fn whatever(&self);
}
macro_rules! associated {
($t:ident, $val:expr => $typ:ty) => {
@durka
durka / struct_with_constructor.rs
Last active August 29, 2015 14:23 — forked from anonymous/playground.rs
Macro for making a struct with an automatic ::new()
macro_rules! as_item {
($i:item) => ($i);
}
macro_rules! struct_with_constructor {
(
$(#[$meta:meta])*
struct $name:ident {
$($arg:ident : $typ:ty),*;
$(fn $fn_name:ident $args:tt $(-> $ret_ty:ty)* { $($fn_body:tt)* })*
@durka
durka / tuple_method_macro.rs
Last active August 29, 2015 14:23 — forked from anonymous/playground.rs
Macro that creates fn impls on a tuple
// due to Enamex on #rust
macro_rules! overload {
( $trait_name:ident ($($args:ident : $typs:ty),+) => $( fn $fn_name:ident -> $ret:ty => $body:block );+ ) => {
overload!(INTERNAL:DUPLICATE $trait_name ( ($($args),+) ($($args),+) : ($($typs),+) ($($typs),+) ) => $( fn $fn_name -> $ret => $body );+ );
};
(INTERNAL:DUPLICATE $trait_name:ident ( $all_args:tt ($($args:ident),+) : $all_typs:tt ($($typs:ty),+) ) => $( fn $fn_name:ident -> $ret:ty => $body:block );+ ) =>
{