This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code)] | |
macro_rules! as_item { ($i:item) => ($i) } | |
macro_rules! s { | |
($t:ident) => { | |
impl $t { | |
} | |
}; | |
($t:ident<$($x:tt),+>) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Associated<T> { | |
fn associated(&self) -> T; | |
} | |
trait Thing : Associated<&'static str> { | |
fn whatever(&self); | |
} | |
macro_rules! associated { | |
($t:ident, $val:expr => $typ:ty) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)* })* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 );+ ) => | |
{ |