Skip to content

Instantly share code, notes, and snippets.

View matthewjberger's full-sized avatar

Matthew J. Berger matthewjberger

View GitHub Profile
@matthewjberger
matthewjberger / graph_equality.rs
Created May 15, 2024 22:55
Create a newtype for equating petgraph graphs (use this if you want to #[derive(PartialEq)] but your struct has a petgraph::Graph field)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomGraph {
pub graph: petgraph::Graph<MakelineNode, ()>,
}
impl PartialEq for CustomGraph {
fn eq(&self, other: &Self) -> bool {
graph_eq(&self.graph, &other.graph)
}
@matthewjberger
matthewjberger / types.rs
Last active May 3, 2024 03:52
Type id pattern matching in rust
use std::any::TypeId;
// Define some example types
struct Foo;
struct Bar;
struct Baz;
// Define a function to perform type-based pattern matching and return the value
fn match_type<T: 'static>(value: T) -> Option<T> {
// Get the TypeId of the value
@matthewjberger
matthewjberger / genvec.rs
Created April 28, 2024 18:15
Sparse vectors with generational indices in rust
use self::error::GenerationError;
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
pub type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
pub mod error {
use super::*;
@matthewjberger
matthewjberger / sm.rs
Created April 7, 2024 06:49
rust state machines
//#![no_std]
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy)]
pub enum State {
State1,
State2,
State3,
}
@matthewjberger
matthewjberger / broker.rs
Created March 31, 2024 03:42
A thread-safe pub/sub message broker in Rust
use std::{
collections::{HashMap, VecDeque},
sync::{Arc, RwLock, Weak},
};
use uuid::Uuid;
#[derive(Default)]
pub struct Broker<T: Clone> {
subscribers: Arc<RwLock<HashMap<String, Vec<Weak<RwLock<Client<T>>>>>>>,
}
@matthewjberger
matthewjberger / alternate.rs
Last active March 18, 2024 03:04
Snappy and bincode data streaming to/from file on disk
// [dependencies]
// bincode = "1.3.3"
// serde = { version = "1.0.197", features = ["derive"] }
// snap = "1.1.1"
use bincode::{deserialize, serialize};
use serde::{de::DeserializeOwned, Serialize};
use snap::{read::FrameDecoder, write::FrameEncoder};
use std::{
fs::{File, OpenOptions},
@matthewjberger
matthewjberger / cargo.toml
Created March 14, 2024 04:30
Snappy compression
[package]
name = "snapper"
version = "0.1.0"
edition = "2021"
[dependencies]
fake = "2.5"
snap = "1.0"
@matthewjberger
matthewjberger / Cargo.toml
Last active April 23, 2024 20:32
Rust window - winit 0.29.11, wgpu 0.19.1, egui 0.27.2
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[dependencies]
egui = "0.27.2"
egui-wgpu = { version = "0.27.2", features = ["winit"] }
egui-winit = "0.27.2"
pollster = "0.3.0"
@matthewjberger
matthewjberger / instructions.md
Created February 12, 2024 01:11
Create an orphan gh-pages branch in git
git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Initializing gh-pages branch"
git push origin gh-pages
git checkout main
@matthewjberger
matthewjberger / main.rs
Last active February 10, 2024 18:09
Create a window in rust (winit 0.29+ anyhow + env_logger)
// [dependencies]
// anyhow = "1.0.40"
// env_logger = "0.11.1"
// winit = "0.29.10"
fn main() -> anyhow::Result<()> {
env_logger::init();
let event_loop = winit::event_loop::EventLoop::new()?;
let _window = winit::window::WindowBuilder::new()