Skip to content

Instantly share code, notes, and snippets.

@Plecra
Plecra / typing_rules.md
Last active January 5, 2026 05:06
A rundown of typing rules a la Rust

How to read typing rules, by Rust intuition

This is just a brief little walkthrough of how to interpret the simplest typing rules, by presenting them as Rust types to appeal to what you already know. There's plenty uncovered, but this should get you off to a great start!

(I originally wrote this in response to a particular question, so it's a little targeted. Anyone is very welcome to suggest edits, I might come by and fix it up later)

We want typing contexts because some expressions have free variables. To know the type of (x, y),

@Plecra
Plecra / rc_collection.rs
Created July 15, 2025 21:15
A simple presentation of garbage collection of reference counted graphs.
use std::cell::{Cell};
use std::rc::Rc;
use std::collections::HashMap;
use std::ops;
// seen = 0 for all objects
// for each object in the queue
// for child in object
// if child not aggregate, continue
#![allow(non_snake_case)]
use std::path::Path;
use std::cell::Cell;
use std::ffi::c_void;
use std::sync::atomic::{AtomicU32, Ordering};
use bindings::Windows::Win32::{
Foundation::{HWND, LPARAM, PWSTR, WPARAM, E_UNEXPECTED, E_NOTIMPL},
Media::{Audio::CoreAudio::GUID_NULL, MediaFoundation::*},
System::LibraryLoader::GetModuleHandleW,
@Plecra
Plecra / concvec.rs
Created April 7, 2021 11:41
Rust Concurrent Monotonic list
use core::ptr;
use core::sync::atomic::{fence, AtomicPtr, AtomicUsize, Ordering};
use std::alloc;
#[cfg(not(target_pointer_width = "32"))]
const DIVISIONS: usize = 24;
#[cfg(target_pointer_width = "32")]
const DIVISIONS: usize = 12;
struct MonotonicVec<T> {
@Plecra
Plecra / Cargo.toml
Created November 16, 2020 14:24
Logging demo
[package]
name = "keylogger"
version = "0.1.0"
authors = ["..."]
edition = "2018"
[dependencies]
winit = { version = "0.23.0", features = ["serde"] }
chrono = { version = "0.4.19", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
@Plecra
Plecra / modified-utf-8.rs
Last active June 14, 2020 13:24
A Rust Modified Utf-8 decoder
pub enum ModifiedUtf8Error {
Incomplete,
Malformed,
}
/// Decode a Modified UTF-8 encoded string.
///
/// This encoding is used by Java for fast serialization of its strings, and
/// has some minor deviations from standard UTF-8.
pub fn from_modified_utf8(mut bytes: &[u8]) -> Result<String, ModifiedUtf8Error> {
@Plecra
Plecra / leb128.rs
Created June 1, 2020 20:48
LEB128 VarInt decoding
const HIGH_BIT: u8 = 0b10000000;
fn hi_bit_set(byte: u8) -> bool {
byte & HIGH_BIT != 0
}
pub fn naive(buf: &mut &[u8]) -> Result<u64, ()> {
let mut value = 0;
let mut iter = buf.iter();
for (count, &byte) in (&mut iter).enumerate().take(10) {
value += u64::from(byte & !HIGH_BIT) << (count * 7);
@Plecra
Plecra / nom_complete.rs
Created May 9, 2020 21:37
Annotate your nom inputs to use complete parsers
use error::*;
use nom::*;
#[derive(Debug, Clone)]
struct Complete<T>(T);
impl<T> Complete<T> {
fn into_inner(self) -> T {
self.0
}
}
@Plecra
Plecra / proxy.rs
Created May 3, 2020 17:00
A smol proxy server
use anyhow::*;
use futures::io::{copy, AsyncReadExt, AsyncWriteExt};
use futures::stream::StreamExt;
use log::*;
use smol::Async;
use std::net;
const PORT: u16 = 5000;
@Plecra
Plecra / xinput_audio.cpp
Created April 21, 2020 17:20
Adventures into the cursed land of c++ #NaN.
#include <iostream>
#include <Windows.h>
#include <Xinput.h>
#pragma comment (lib, "xinput.lib")
int main()
{
std::cout << "Hello World!\n";