Skip to content

Instantly share code, notes, and snippets.

@arifd
arifd / macro_branch.rs
Last active March 14, 2023 18:17
detect if token in macro present, branch inside macro
/// # Exmaple
/// ```
/// select! {
/// { $($yes)? } { if_present() } { if_absent() }
/// }
macro_rules! select {
({ $($_:tt)+ } { $($keep:tt)* } { $($discard:tt)* } ) => { $($keep)* };
({} { $($discard:tt)* } { $($keep:tt)* } ) => { $($keep)* };
}
@arifd
arifd / select.rs
Created April 17, 2023 15:02
rust tokio select perculiarities
#[cfg(test)]
mod tests {
use std::{
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
time::Duration,
};
@arifd
arifd / variable_args.rs
Created May 23, 2023 13:27
variable args
/// creates a nested tuple from the given args
/// ```
/// assert_eq!(
/// vararg!("a", 15, true, 10.0),
/// ("a", (15, (true, (10.0, ()))))
/// )
macro_rules! vararg {
($first:expr $(, $rest:expr)* $(,)?) => {
($first, vararg!( $($rest),* ) )
};
@arifd
arifd / bench.rs
Created October 20, 2023 21:49
bench iter flatten collect vs concat
use criterion::{black_box, criterion_group, criterion_main, Criterion};
pub fn concat(v: Vec<Vec<u32>>) -> Vec<u32> {
v.concat()
}
pub fn iter_flatten_collect(v: Vec<Vec<u32>>) -> Vec<u32> {
v.into_iter().flatten().collect()
}