Skip to content

Instantly share code, notes, and snippets.

@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()
}
@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 / 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 / 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 / timing.rs
Created March 2, 2023 13:48
log slow functions, optimized out in release, example of cancelling a thread
use std::{
sync::mpsc,
time::{Duration, Instant},
};
/// Warns on any function that is behaving slow
///
/// Useful for when your code appears to be getting stuck somewhere
/// and you want a fast "debug print" way of knowing where.
#[inline]
@arifd
arifd / CPoint.rs
Last active February 5, 2023 11:59
const QUANT: f32 = u16::MAX as f32;
struct CPoint(u16);
impl From<f32> for CPoint {
fn from(f: f32) -> Self {
assert!((0.0..=1.0).contains(&f), "given float {f} out of bounds");
CPoint((f * QUANT) as u16)
}
}
impl From<CPoint> for f32 {
@arifd
arifd / non-crpytographically secure hash checklist
Created January 30, 2023 12:46
When to know if you can get away with a non-cryptographically secure hashing algorithm
Would it be bad if someone figured out how to manipulate the hash function to get a certain result?
Would it be bad if someone found out the input from the hash?
@arifd
arifd / virtual_mic.sh
Last active July 10, 2022 10:49
Very high quality DSP for microphone input
#!/bin/bash
SCRIPT_NAME="$(basename "$0")"
AUDIO_INTERFACE_NAME="Scarlett Solo USB" # Discover name with command `arecord -l`
AUDIO_INTERFACE_DETAILS=$(arecord -l | grep "${AUDIO_INTERFACE_NAME}")
AUDIO_CARD=$(echo "${AUDIO_INTERFACE_DETAILS}" | grep -o -E 'card [0-9]+:' | tr -dc '0-9')
AUDIO_DEVICE=$(echo "${AUDIO_INTERFACE_DETAILS}" | grep -o -E 'device [0-9]+:' | tr -dc '0-9')
AUDIO_BUFFER_SIZE="2048" # The lower the number, the less latency introduced, but more likely buffer overruns
@arifd
arifd / test.rs
Last active June 26, 2022 15:38
uppercase, lowercase, Unicode, chars and Rust
fn main() {
let y = "y̆";
let mut chars = y.chars();
assert_eq!(Some('y'), chars.next()); // not 'y̆'
assert_eq!(Some('\u{0306}'), chars.next());
// INTERESTING: chars is unicode compatible, it just gives you code points in between your graphemes
for c in y.chars() {
println!("{}", c.is_lowercase()); // true, false
}
@arifd
arifd / yuyv422_to_rgb24.rs
Last active October 20, 2024 11:35
YUV to RGB in Rust
#![allow(dead_code)]
//! https://www.kernel.org/doc/html/v4.17/media/uapi/v4l/pixfmt-yuyv.html
//!
//! V4L2_PIX_FMT_YUYV — Packed format with ½ horizontal chroma resolution, also known as YUV 4:2:2
//! Description
//!
//! In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr. Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels. As you can see, the Cr and Cb components have half the horizontal resolution of the Y component. V4L2_PIX_FMT_YUYV is known in the Windows environment as YUY2.
//!
//! Example 2.19. V4L2_PIX_FMT_YUYV 4 × 4 pixel image