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 / 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() {
@sug0
sug0 / church.go
Last active March 31, 2023 17:06
Church numerals in Go
// next up: scott encoding https://en.wikipedia.org/wiki/Mogensen%E2%80%93Scott_encoding
package main
import "fmt"
type Church[T any] func(func(T) T) func(T) T
func main() {
churchTest[int](0, func(x int) int { return x + 1 })
@sug0
sug0 / trie.rs
Last active December 10, 2022 00:59
Rust code generator to match against a set of N pre-defined strings, in logarithmic time
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::io::{self, BufRead};
#[derive(Debug, Default)]
struct Node {
terminal: bool,
children: BTreeMap<char, Node>,
}
@sug0
sug0 / xbps.c
Last active October 24, 2022 22:53
Small utility program to spin up xbps sub-commands.
// Copyright (C) 2022 Tiago Carvalho <[email protected]>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
@sug0
sug0 / merkle.rs
Created September 12, 2022 07:42
Simple Merkle tree in Rust
#![feature(iter_array_chunks)]
#![feature(hasher_prefixfree_extras)]
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug)]
struct Tree<T> {
root: Option<Node<T>>,
}