Skip to content

Instantly share code, notes, and snippets.

Avatar
👾

Thom Chiovoloni thomcc

👾
View GitHub Profile
@thomcc
thomcc / fix_submodules.rs
Created Oct 29, 2021
The old "magic git submodule wrangling" script from imgui-rs.
View fix_submodules.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 Aug 3, 2021
alternative (more efficient, but still sound) way to implement `zeroize`'s memory wiping
View zeroize_impl.rs
//! 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
View pixel_average.rs
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 Jul 9, 2021
Extracting the message from a `std:🧵:Result::Err` (e.g. a `Box<dyn Any + Send>`).
View extract_message.rs
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();
}
View with_sections.rs
//! 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
View try_flat_map.rs
/// Something like a `flat_map` over a fallible iterator.
///
/// Note that the `map` itself is not fallible.
///
/// See test at end of file for usage.
//
// TODO this should probably take `impl IntoIterator<IntoIter = I>` but the
// generics here are already so fucking complicated, and i'm worried that
// will cause inference errors
View clonable_error.rs
/// Wrapper around an `impl std::error::Error` that makes it "mostly" clonable.
pub struct ClonableError<E> {
error: Option<E>,
// tbh these are all anybody cares about for Errors anyway.
display: Box<str>,
debug: Box<str>,
}
impl<E> Clone for ClonableError<E> {
@thomcc
thomcc / resolve_version.sh
Created Apr 2, 2021
bash function to resolve a crate version in a terrible way
View resolve_version.sh
# helper fn that returns version number given $crate or $crate/$semver.
# both must be url-encoded but the / must be a / if provided.
# doesn't handle cases where the version/crate/whatever doesn't exist.
resolve_version() {
# Perform `HEAD` request to `docs.rs/$1` and dump the redirected URL.
# We expect this to go to `docs.rs/crate/$crate/$version`
# or `docs.rs/$crate/$version/$crate/` depending on if the crate has
# docs or idrk. Maybe a couple other urls are possible, it
# took a bit of testing for the `sed` script to handle all cases I found.
#
View approx_rat_cfrac.rs
// type Int = i128;
type Int = i64;
type Rat = (Int, Int);
/// Quick and dirty (no overflow handling) continued fraction-based
/// rational approximation.
///
/// Returns `(p/q, was_exact)` where p/q is an approximation of `n/d`,
/// where neither `p` nor `q` are above `limit`. `was_exact` is true if
View lfstack.rs
//! This is a (hopefully) correct implementation of a lockfree stack.
//!
//! Doing this without a memory reclamation implementation requires pointer
//! tagging, which is slightly architecture specific. This supports aarch64 and
//! x86_64 so long as 5-level page tables aren't in use (rare to the point of
//! nonexistence), and arm64e (or other hardware pointer authentication) is not
//! enabled, which is not currently supported by Rust. On x86_64/aarch64 where
//! this doesn't hold, it will panic.
//!
//! That said, while this implementation is believed to be correct, it could