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 / 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>>,
}
@sug0
sug0 / conts.py
Last active September 8, 2022 20:51
Promises-like API in Python
import multiprocessing
from collections import namedtuple
from concurrent.futures import ThreadPoolExecutor
Poll = namedtuple('Poll', ['resolved', 'value'])
_CURRENT_EXECUTOR = None
def enter_runtime(main=None, threads=0):
@sug0
sug0 / parsers.scm
Last active May 30, 2022 18:55
Parser combinators in Chez Scheme
;; TODO: add backtracking point, to avoid parsing all the way from the beginning!
;;
;; e.g.
;;
;; (json-value (new-string-state "[\" \" 123]"))
;; ==> (#f "Exhausted all possible JSON value types: Failed to parse JSON number: Expected character '9', but found '[' instead")
;;
;; we know we are looking for a comma, so add a backtracking point after the string
;; return 1 >>= thing1 >>= thing2 >>= thing3
@sug0
sug0 / specialization.rs
Last active April 24, 2022 21:05
Specialization idiom in stable Rust
use std::fmt::Debug;
use std::marker::PhantomData;
enum Specialized {}
enum Generic {}
struct Thing<T, K = Generic> {
inner: T,
_phantom: PhantomData<K>,