Skip to content

Instantly share code, notes, and snippets.

View durka's full-sized avatar

Alex Burka durka

View GitHub Profile
@durka
durka / a.md
Last active May 2, 2016 15:52 — forked from anonymous/a

Proofs of Send and Sync laws

T: Sync ⟺ &T: Send

Explanation

Consider a value a: T. Assume T: Sync. Then, to access a on multiple threads, it must be possible to send a reference to another thread, thus &T: Send. Therefore, T: Sync ⟹ &T: Send.

Assume &T: Send. Sending &a: &T to another thread means a can be accessed concurrently, thus T: Sync is required. Therefore, &T: Send ⟹ T: Sync.

@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);
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 / 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 / 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) => {
@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);