Skip to content

Instantly share code, notes, and snippets.

View thomcc's full-sized avatar
🦀

Thom Chiovoloni thomcc

🦀
View GitHub Profile
@thomcc
thomcc / min_max.hh
Created December 1, 2013 18:51
C++11 variadic max, min, and minmax implementations.
#pragma once
#include <utility>
// varidic max, min, and minmax implementation (named maximum, minimum and min_max to avoid confusion)
// no support for custom comparators (mainly because i'm not sure how they should be specified)
namespace detail {
// max base case
template <class T>

Steps to faster git on mac.

  1. Update to git 2.16

  2. Setup file monitoring with hgwatchman

    1. Run
    $ brew install watchman
    $ cd /path/to/mozilla-central
    

$ watchman watch-project .

@thomcc
thomcc / Jelly-Physics.markdown
Created February 2, 2014 20:07
A Pen by Thom Chiovoloni.
@thomcc
thomcc / bakery_mutex.rs
Created November 30, 2021 17:57
quick and dirty impl of lamports bakery algorithm in rust as an example. untested.
//! Implementation of [Lamport's bakery algorithm][bakery]. This is somewhat
//! interesting, because it's a mutex which can be implemented even if the
//! target only has atomic load/store, and no CAS (in principal it can be even
//! more general than this).
//!
//! [bakery]: https://en.wikipedia.org/wiki/Lamport%27s_bakery_algorithm
//!
//! Major caveat: This is not tested, and this algo is no longer appropriate
//! for modern code. Some variations of it can be useful in a thread pool, but
//! it's mostly useful to understand the concepts.
@thomcc
thomcc / fix_submodules.rs
Created October 29, 2021 17:33
The old "magic git submodule wrangling" script from imgui-rs.
use anyhow::Result;
use std::{
path::{Path, PathBuf},
sync::atomic::{AtomicBool, Ordering::Relaxed},
};
/// "Automagic" submodule wrangling (sync, clean, fix, etc)
///
/// Ported from rustc's `x.py` code, which runs something like this
/// automatically (even for `./x.py --help`)
@thomcc
thomcc / zeroize_impl.rs
Created August 3, 2021 21:55
alternative (more efficient, but still sound) way to implement `zeroize`'s memory wiping
//! Sketch of a alternative way to implement the backend of `zeroize` (the part
//! that does memory zeroing).
//!
//! It should be:
//!
//! - More efficient, possibly much more (for byte arrays), as it can zero in
//! the most efficient way available, rather than e.g. being forced to
//! perform element-at-a-time writes (where "element" often is "byte").
//! - Still guaranteed even in the face of aggressive compiler optimizations
//! as the semantics of volatile loads forbid the compiler from being able
pub type Argb8888 = u32;
/// Average of 2 pixels, rounding up on tie.
///
/// It's fine if they're actually BGRA8888 or any other format,
/// this treats every channel the same.
pub fn pixel_average(c0: Argb8888, c1: Argb8888) -> Argb8888 {
(c0 | c1).wrapping_sub(((c0 ^ c1) >> 1) & 0x7f7f7f7f)
// version that rounds down:
// (c0 & c1).wrapping_add(((c0 ^ c1) >> 1) & 0x7f7f7f7f)
@thomcc
thomcc / extract_message.rs
Created July 9, 2021 00:38
Extracting the message from a `std::thread::Result::Err` (e.g. a `Box<dyn Any + Send>`).
use std::any::Any;
pub fn extract_message(err: &(dyn Any + Send)) -> String {
fn extract(err: &(dyn Any + Send), max_tries: usize) -> String {
if let Some(&s) = err.downcast_ref::<&'static str>() {
return s.into();
}
if let Some(s) = err.downcast_ref::<String>() {
return s.into();
}
@thomcc
thomcc / strip-json-comments.rs
Created November 4, 2019 20:35
strip-json-comments rust (untested, kept here so i can find it later)
pub fn strip_json_comments(s: &str) -> String {
let mut result = String::with_capacity(s.len());
let mut iter = s.chars();
while let Some(c) = iter.next() {
if c == '"' {
// It's a string, skip to end without touching any comments inside.
result.push(c);
while let Some(c) = iter.next() {
//! initial version courtesy of @danielhenrymantilla.
//!
//! extended to support fns with args/return types,
//! visibility, #[attributes], unsafe, const, async,
//! extern "abi" ...
//!
//! left as an exercise for later (cuz it sux):
//! - generics?
//! - where clauses?
//! - probably other shit im missing