Skip to content

Instantly share code, notes, and snippets.

View CosmicSyntax's full-sized avatar

Daniel Choi CosmicSyntax

View GitHub Profile
@CosmicSyntax
CosmicSyntax / main.zig
Created September 1, 2025 03:12
Dynamic Dispatch in Zig
const std = @import("std");
pub const Trait = struct {
ptr: *anyopaque,
vtable: Vtable,
const Vtable = struct {
hello: *const fn (*anyopaque) void,
};
};
@CosmicSyntax
CosmicSyntax / main.rs
Created July 11, 2025 17:28
vtable in Rust
use std::{marker::PhantomData, ptr::NonNull};
trait Foo {
fn foo(&self);
}
struct Anydata<'a> {
_p: PhantomData<&'a ()>,
data: NonNull<()>,
fns: &'static Anyfn,
@CosmicSyntax
CosmicSyntax / main.rs
Created November 28, 2022 15:43
Sudoku Solver
fn main() {
// 2d array
let mut set = [
[0, 0, 7, 5, 0, 0, 0, 0, 0],
[1, 0, 5, 2, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 3, 0, 0, 9, 0],
[7, 0, 0, 0, 0, 0, 0, 8, 0],
[3, 0, 0, 0, 0, 0, 0, 4, 0],
[2, 5, 0, 0, 0, 6, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 4],
@CosmicSyntax
CosmicSyntax / main.rs
Last active February 6, 2022 03:35
Pallindrome Finder
use std::env;
fn main() {
if let Some(s) = env::args().nth(1) {
let x = palfinder(&s);
println!("For {}:", s);
x.into_iter().for_each(|x| println!("{}", x));
}
@CosmicSyntax
CosmicSyntax / main.rs
Created December 10, 2021 05:00
Unsafe mutate of string using byte indexing
fn main() {
let x = Hi(String::from("HELLO!"));
let y = x.0.as_bytes() as *const [u8] as *mut [u8];
unsafe {
(*y)[0] = "X".as_bytes()[0];
}
println!("{:?}", x);
}
@CosmicSyntax
CosmicSyntax / lib.rs
Created October 9, 2021 02:56
"Mixed" data type in array through const generics
pub trait All {
fn print(&self);
}
pub struct Dynamic<'a, const SIZE: usize>(pub [&'a dyn All; SIZE]);
impl All for u8 {
fn print(&self) {
println!("{}", self);
}
@CosmicSyntax
CosmicSyntax / main.rs
Created July 26, 2021 01:13
Default Generic Parameter
// Defining a struct called Test with a default generic param
struct Test<A, B = Sample> {
a: A,
b: B,
}
struct Sample {
one: u8,
}
@CosmicSyntax
CosmicSyntax / main.rs
Created May 27, 2021 21:10
Leaking memory and reclaiming that leaked memory
use std::cell::UnsafeCell;
use std::sync::atomic::{AtomicI32, Ordering};
use std::thread;
use std::time::Duration;
struct Mutex<T> {
data: UnsafeCell<T>,
}
impl<T> Mutex<T> {
@CosmicSyntax
CosmicSyntax / lib.rs
Created February 22, 2021 03:05
Subtyping and Variance
// Lifetime is just regions of code
/*
Argument s is invariant over &'b str, can only be mutated with the same lifetime
Argument delimiter is by-value
*/
pub fn strtok<'a, 'b: 'a>(s: &'a mut &'b str, delimiter: char) -> &'b str {
if let Some(i) = s.find(delimiter) {
let prefix = &s[..i];
let suffix = &s[(i + 1)..];
*s = suffix; // suffix and s has the same lifetime
@CosmicSyntax
CosmicSyntax / main.rs
Created February 13, 2021 23:03
Uninitialized Memory Usage
use std::mem::MaybeUninit;
/*
Why do something like this?
To save a few CPU cycles and initializing to a value that will be overwritten.
*/
// This is wrapped in unsafe because you can do something like
// initializer(123456 as *const string); 123456 being some memory address.
unsafe fn initializer(uninit: *mut String) {