Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
#![feature(slice_split_once)]
use std::{
cmp::{max, min},
collections::hash_map::Entry,
env::args_os,
fs::File,
io::{stdout, Write as _},
path::Path,
};
@Lucretiel
Lucretiel / joint.rs
Last active May 23, 2022 18:56
A paired smart pointer. The value owned by a pair of Joint objects is dropped as soon as *either* of the Joints are dropped
use std::{
marker::PhantomData,
mem::MaybeUninit,
ops::Deref,
process::abort,
ptr::NonNull,
sync::atomic::{AtomicU32, Ordering},
};
const MAX_REFCOUNT: u32 = i32::MAX as u32;
@Lucretiel
Lucretiel / depth_limiter.rs
Created April 29, 2022 19:13
serde deserializer adapter that limits recursion depth
pub mod sort;
use serde::de;
#[derive(Debug, Clone, Copy)]
pub struct DepthLimiter<T> {
depth: usize,
inner: T,
}
@Lucretiel
Lucretiel / ti4-parameters.json
Last active April 23, 2022 14:07
My map balance parameters for the TI4 map lab
[
{
"title": "Nathan's",
"data": {
"BASE_PLANET_MOD": 1,
"RESOURCES_MULTIPLIER": 3,
"INFLUENCE_MULTIPLIER": 2,
"TECH_MOD": 5,
"TECH_PROPULSION_MOD": 0,
"TECH_WARFARE_MOD": 0,
@Lucretiel
Lucretiel / raw_unsound.rs
Created April 14, 2022 02:56
demonstration of a soundness flaw in rmp_serde
use rmp_serde::Raw;
use serde::{ser, Serialize};
use thiserror::Error;
/// Serializer that serializes strings as a list of char
struct CharListSerializer {
output: Vec<char>,
}
#[derive(Debug, Error)]
@Lucretiel
Lucretiel / serde_seq_adapters.rs
Created April 6, 2022 20:42
Adapters for `SeqAccess` and `MapAccess` that operate with move semantics
use std::{convert::Infallible, marker::PhantomData, mem};
use serde::de;
pub trait SeqAccess<'de>: Sized {
type Error: de::Error;
fn next_value_seed<S>(self, seed: S) -> Result<Option<(S::Value, Self)>, Self::Error>
where
S: de::DeserializeSeed<'de>;
@Lucretiel
Lucretiel / parser.rs
Created November 24, 2021 00:12
An example nom parser for reddit
use nom::{
branch::alt,
character::complete::{alpha1, char, digit1, multispace1},
combinator::eof,
IResult, Parser,
};
use nom_supreme::{error::ErrorTree, multi::collect_separated_terminated, ParserExt};
#[derive(Debug, Clone)]
struct Structure<'a> {
@Lucretiel
Lucretiel / lisplusplus.cpp
Created October 19, 2021 23:27
Demo of lisp implemented by overloading the comma operator
#include "liscpp.h"
using namespace std;
int main()
{
ReadEvalPrint(
(Let,
tok(fold), (Lambda, tok(F), tok(I), tok(L),
@Lucretiel
Lucretiel / fetch_paginated.js
Last active October 6, 2021 03:23
Utility that fetches from a paginated resource by repeatedly fetching & concatenating pages
// Fetch several rows from 1 or more pages from an API. Concatenate all
// the rows together.
function fetch_paginated(
// The URL to initiate the query
initial_url,
// A fetch function that retrieves a single page. Intended to wrap a fetch()
// API call (so that you can add your own headers, auth, response parsing,
// etc)
fetcher,