Skip to content

Instantly share code, notes, and snippets.

View matthewjberger's full-sized avatar
🦀
Writing something in Rust, probably

Matthew J. Berger matthewjberger

🦀
Writing something in Rust, probably
View GitHub Profile
@matthewjberger
matthewjberger / Cargo.toml
Created August 19, 2024 10:49
Standalone Winit Wgpu/Triangle
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
path = "src/lib.rs"
name = "app_core"
@matthewjberger
matthewjberger / main.rs
Last active August 12, 2024 19:32
Alternatives to drain to preventable multiple mutable refs to self
use std::collections::VecDeque;
#[derive(Debug, Clone)]
struct Event(u32);
struct Context {
queued_events: VecDeque<Event>,
}
impl Context {
@matthewjberger
matthewjberger / main.rs
Created August 6, 2024 01:34
Analysis of how embassy uses rust types for pins and peripherals
use std::sync::atomic::{AtomicBool, Ordering};
use std::marker::PhantomData;
// Simulate hardware-specific types
mod hal {
pub mod peripherals {
pub struct TIMER0;
pub struct UART0;
pub struct GPIO;
@matthewjberger
matthewjberger / sync-main.rs
Last active August 4, 2024 09:59
Teleport interactively in rust
// thiserror = "1.0.63"
use std::{
io::{BufRead, BufReader, Write},
process::{Child, Command, Stdio},
sync::{
mpsc::{self, Sender},
Arc, Mutex,
},
thread,
time::Duration,
@matthewjberger
matthewjberger / Justfile
Created August 1, 2024 08:06
A rust justfile with common commands
set windows-shell := ["powershell.exe"]
export RUST_LOG := "info"
export RUST_BACKTRACE := "1"
@just:
just --list
build:
cargo build -r
@matthewjberger
matthewjberger / macro.rs
Last active July 8, 2024 19:09
egui enum dropdown selection in rust
// This is a macro that can generate the egui enum dropdown pattern for a c-style Rust enum
// meaning it contains only fieldless, unit variants
#[macro_export]
macro_rules! generate_enum {
(
$name:ident {
$($variant:ident),* $(,)?
}
) => {
@matthewjberger
matthewjberger / pubsub.rs
Created July 4, 2024 22:48
An in-process pub/sub message broker in rust using async_std
// [dependencies]
// async-std = { version = "1.12.0", features = ["attributes"] }
// futures = "0.3.30"
use async_std::{
future::timeout,
sync::{Arc, Mutex},
};
use futures::{
channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
futures::stream::StreamExt,
// petgraph v0.6.5
use petgraph::graph::{NodeIndex, DiGraph};
fn number_children(graph: &DiGraph<(), ()>, parent: NodeIndex, depth: usize) -> Vec<(NodeIndex, usize, usize)> {
let mut tree = Vec::new();
let mut index = 0;
// Start numbering the children from index 0
for child in graph.neighbors(parent) {
tree.push((child, index, depth));
// petgraph v0.6.5
use petgraph::graph::DiGraph;
use petgraph::visit::{EdgeRef, Topo};
fn main() {
// Create a new directed graph
let mut graph = DiGraph::new();
// Add nodes and edges (similar to previous examples)
let a1 = graph.add_node("A1");
@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)
}