Skip to content

Instantly share code, notes, and snippets.

View matthewjberger's full-sized avatar

Matthew J. Berger matthewjberger

View GitHub Profile
@matthewjberger
matthewjberger / wasm.md
Last active November 19, 2023 23:05
Check if a rust crate is compatible with wasm + tips

Does it build in wasm?

cargo build --target wasm32-unknown-unknown

How do I specify crates just for the wasm target?

Here's an example using legion:

//! Tokio Async Topic-based Pub/Sub Messaging
//!
//! This module provides a broker-client communication system where
//! multiple clients can communicate with each other through a central broker.
//! The broker is responsible for routing messages between clients based on topics.
//!
//! # Structures
//!
//! - `Broker`: Represents the central message router. It manages topics and routes messages between subscribers.
//! - `Client`: Represents a client that can subscribe to topics, send, and receive messages through the broker.
@matthewjberger
matthewjberger / main.rs
Created September 30, 2023 16:37
Use serde Serialize and Deserialize attributes as re-exports
// From: https://github.com/serde-rs/serde/issues/1465#issuecomment-800686252
use common::serde::{self, Deserialize, Serialize}
#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "self::serde")] // must be below the derive attribute
struct Vertex {
position: [u32; 3],
}
#[derive(Default, Debug, Clone, Copy, PartialEq)]
struct Point {
x: f64,
y: f64,
}
impl Point {
fn lerp(self, other: Self, t: f64) -> Self {
Self {
x: (1.0 - t) * self.x + t * other.x,
use std::{
cell::RefCell,
collections::{HashMap, VecDeque},
rc::{Rc, Weak},
};
use uuid::Uuid;
pub type ClientHandle<T> = Rc<RefCell<Client<T>>>;
pub struct Client<T> {
/// Time of day as seconds since midnight. Used for clock in demo app.
pub(crate) fn seconds_since_midnight() -> f64 {
use chrono::Timelike;
let time = chrono::Local::now().time();
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64)
}
@matthewjberger
matthewjberger / fuzzy.rs
Last active August 12, 2023 18:20
Fuzzy search string lists in rust using Levehnstein distance
fn levenshtein(a: &str, b: &str) -> usize {
let (a_len, b_len) = (a.chars().count(), b.chars().count());
let mut matrix = vec![vec![0; b_len + 1]; a_len + 1];
for i in 0..=a_len {
matrix[i][0] = i;
}
for j in 0..=b_len {
matrix[0][j] = j;
}
@matthewjberger
matthewjberger / rust-debug.md
Last active September 7, 2023 16:11
Rust debugging
use serde_json::Value;
fn get_value<'a>(j: &'a Value, p: &str) -> Option<&'a Value> {
let keys = p.split('/').collect::<Vec<&str>>();
let mut current_value = j;
for key in keys {
current_value = match current_value.get(key) {
Some(value) => value,
None => return None,
@matthewjberger
matthewjberger / main.rs
Last active July 21, 2023 05:33
Order of Operations example in rust
fn main() {
let (a, b, c) = (1_i32, 2_i32, 3_f32);
let first_result = (a - b) as f32 / c * 1000.0;
let second_result = (a - b) as f32 / (c * 1000.0);
assert_eq!(first_result, second_result);
}