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 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 / halve.rs
Last active October 19, 2020 23:15 — forked from anonymous/playground.rs
Rust macro that splits a list of expressions in half (at compile time)
/// 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 / 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 / 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
SystemTime::now().duration_since(UNIX_EPOCH
+ Duration::from_secs(process.start_time))
.unwrap_or(Duration::from_secs(0))
.as_secs()
@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 / main.rs
Last active March 23, 2017 18:26 — forked from barafael/main.rs
Confused about error handling
use std::process::Command;
fn main() {
println!("{}", git_name().unwrap_or("".to_string()));
}
pub fn git_name() -> Option<String> {
if let Ok(output) = Command::new("git")
.arg("config")
@durka
durka / twixt.rs
Last active May 4, 2017 00:52 — forked from anonymous/playground.rs
Twixt construct from Nickle
macro_rules! twixt {
(($cond:expr; $after:expr) { $($body:tt)* } else { $($els:tt)* }) => {{
if $cond {
struct Doop;
impl Drop for Doop { fn drop(&mut self) { $after; } }
let _d = Doop;
$($body)*
} else {
$($els)*
}
@durka
durka / playground.rs
Created June 20, 2017 23:38 — forked from anonymous/playground.rs
gensym macro
/// This auxiliary macro generates an ident for each argument passed to it,
/// and then calls another macro back with the generated idents.
macro_rules! gensym {
// base case: we send all the collected idents back to the other macro
(@go $callback:ident ($($args:tt)*) ($($i:ident)*)) => {
$callback!($($args)* $($i)*)
};
// recursion: here, we add in a new ident named "x" (but distinct from every other "x")
(@go $callback:ident $args:tt ($($i:ident)*) $head:tt $($tail:tt)*) => {
warning: unreachable pattern
--> src/routes.rs:66:1
|
66 | / handle_login! {
67 | | #[get("/<date>/<flow>/<idx>")]
68 | | pub fn episode/episode_login(user: User, users: State<ActiveUsers>, date: Datestamp, flow: Option<FlowType>, idx: u32) -> Template {
69 | | let flow = flow.ok_or(ErrorKind::BadParam("invalid flow type"))?;
... |
132 | | }
133 | | }