Skip to content

Instantly share code, notes, and snippets.

View sug0's full-sized avatar
💭
debugging

Tiago Carvalho sug0

💭
debugging
View GitHub Profile
@sug0
sug0 / likely_unlikely_cond.rs
Created April 26, 2025 10:48
Cond Rust macro with likely/unlikely branch optimizations
macro_rules! cold {
($expr:expr) => {{
#[cold]
#[inline(always)]
fn __cold() {}
__cold();
$expr
}}
}
@sug0
sug0 / dynfields.rs
Last active March 20, 2025 20:02
Rust - Find the address of a field implementing a trait and call it
#![allow(dead_code)]
use std::ops::Add;
fn main() {
check_if_a::<_A>();
check_if_a::<_B>();
check_if_a::<_C>();
type T1<'a> = Cons<'a, _A, Cons<'a, _B, Cons<'a, _C, ()>>>;
@sug0
sug0 / cps.rs
Last active December 19, 2024 12:12
CPS Rust state machines
use std::marker::PhantomData;
use std::panic;
use std::sync::Once;
fn main() {
for _ in 0..3 {
println!("RUNNING STATE MACHINE");
println!("=====================");
run_state_machine((), beginning_state);
println!();
@sug0
sug0 / str.rs
Created November 24, 2024 10:47
Constant type strings in stable Rust in <150 LOC
use std::any::type_name;
use std::marker::PhantomData;
use std::ops::Index;
macro_rules! sbytes {
() => { () };
($head:expr $(,)?) => {
Byte<$head, ()>
};
($head:expr, $($tail:expr),* $(,)?) => {
@sug0
sug0 / packed.rs
Last active December 30, 2023 21:43
Pack arbitrary bits into a pointer offset in Rust
// TODO: set constraints in no. of bits allowed. currently it's possible to crash a program with invalid bitfield sizes
use std::alloc::{alloc, dealloc, Layout, LayoutError};
use std::ops::Drop;
use std::ptr::NonNull;
fn main() {
let mut ptr: FlagPointer<2, _> = FlagPointer::new(1234).unwrap();
println!("value before set = {}", ptr.as_ref());
@sug0
sug0 / merda.c
Created October 25, 2023 20:57
What the fuck even is this
#include <stdio.h>
void lol_ganda_merda(void)
{
int cenas = 0;
static void *labels[] = {&&tas_fodido};
tas_fodido:
puts("bruh");
@sug0
sug0 / post-commit
Created July 27, 2023 20:46
ZSH git hook to sign commits with git-signify
#!/bin/zsh
# get git-signify from: https://github.com/sug0/git-signify
set -e
printf 'Sign commit (y/n)? '
read -sk yn
echo $yn
case $yn in
y|Y)
_commit=$(git rev-parse HEAD)
for i in {1..3}; do
@sug0
sug0 / log.rs
Last active July 25, 2023 23:29
Lock-free Rust log entries with concurrent readers and writers
#![allow(dead_code)]
use std::cell::UnsafeCell;
use std::mem::MaybeUninit;
use std::ops::Drop;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use arc_swap::ArcSwap;
@sug0
sug0 / generators.rs
Last active June 4, 2023 17:37
Draft design of stackless generators in stable Rust
use std::pin::Pin;
use std::ops::ControlFlow;
use syn;
use quote;
/// Stackless generator, with no internal data stored
/// in itself.
pub trait Generator {
/// Data persisted across yield points, as well as
@sug0
sug0 / record_desktop.sh
Last active March 31, 2023 16:56
Desktop recording script
#!/bin/sh
set -e
main() {
parse_args $@
capture_screen
}
parse_args() {