Skip to content

Instantly share code, notes, and snippets.

View DutchGhost's full-sized avatar

DutchGhost DutchGhost

View GitHub Profile
@DutchGhost
DutchGhost / type_theory.rs
Created September 22, 2021 11:34
type theory with traits and structs
//https://www.youtube.com/watch?v=BdXWlQsd7RI&t=8s
use core::marker::PhantomData;
trait Nat {}
struct Zero {}
struct Succ<N: Nat> {
number: PhantomData<N>,
}
@DutchGhost
DutchGhost / array.zig
Created September 9, 2021 13:36
arrayvec
const std = @import("std");
pub fn ArrayList(comptime T: type) type {
return struct {
const Self = @This();
items: []T,
length: usize = 0,
pub fn init(comptime SIZE: usize) Self {
#![allow(incomplete_features)]
#![feature(
const_trait_impl,
const_fn_trait_bound,
const_generics,
const_evaluatable_checked,
specialization
)]
use core::mem::ManuallyDrop;
#![allow(incomplete_features)]
#![feature(
const_trait_impl,
const_fn_trait_bound,
const_generics,
const_evaluatable_checked,
specialization
)]
trait RemoveRef {
@DutchGhost
DutchGhost / cout.rs
Created June 14, 2021 15:43
std::cout << in Rust
mod std {
use std::ops::Shl;
use std::fmt::Display;
pub struct cout;
impl<T: Display> Shl<T> for cout {
type Output = cout;
fn shl(self, data: T) -> cout {
print!("{}", data);
self
@DutchGhost
DutchGhost / rc.rs
Created May 16, 2021 12:43
Rc with custom counter
extern crate alloc;
use {
alloc::alloc::dealloc,
core::{
alloc::Layout,
cell::Cell,
marker::PhantomData,
ops::{Add, Sub},
ptr::{self, NonNull},
@DutchGhost
DutchGhost / mutex.zig
Created December 29, 2020 21:04
async mutex
const std = @import("std");
const Node = struct {
next: ?*Node = null,
frame: anyframe = undefined,
};
const List = struct {
const Self = @This();
macro_rules! monadic {
(
[$($e:tt)*] | [$head:ident : $tail:ident <- tails $origin:ident,
$($heads:ident : $tails:ident <- tails $origins:ident),*], $condition:expr
) => {{
let mut f = move || {
monadic![@INNER [$($e)*] | [$head : $tail <- tails $origin $(,$heads : $tails <- tails $origins)*], $condition ];
None
};
f()
@DutchGhost
DutchGhost / day01.zig
Created December 1, 2020 10:33
advent of code day01
const input = @embedFile("../../../Inputs/day01.txt");
const std = @import("std");
const mem = std.mem;
const fmt = std.fmt;
const ArrayList = std.ArrayList;
const TARGET = @as(usize, 2020);
pub fn parseInput(slice: []const u8, allocator: *mem.Allocator) !ArrayList(usize) {
@DutchGhost
DutchGhost / usingnamespace.zig
Created November 18, 2020 13:14
funny usingnamespcae weirdness
usingnamespace closure: {
var hashes: []const usize = &[0]usize{};
break :closure struct {
pub fn ctStrHash(comptime str: []const u8) usize {
const hash = yourAlgo(str);
comptime {
for (hashes) |existing| {
if (existing == hash) @compileError("collision");
}