Skip to content

Instantly share code, notes, and snippets.

View Lucretiel's full-sized avatar

Nathan West Lucretiel

View GitHub Profile
@Lucretiel
Lucretiel / fetch_paginated.ts
Last active November 6, 2021 02:38
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 paginated<Request, Response, Row>(
// 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: (url: Request, cancel?: AbortSignal) => Promise<Response>,
// Given a response, get the URL of the next page, or null if this was the
// last page.
@Lucretiel
Lucretiel / atomic_transaction.rs
Created August 26, 2021 07:00
An example of a function that processess an atomic as a transaction, using 0 to indicate a locked state
pub fn use_atomic(value: &AtomicU64, op: impl FnOnce(NonZeroU64) -> NonZeroU64) {
loop {
let current = value.swap(0, Ordering::AcqRel);
match NonZeroU64::new(current) {
Some(current) => {
let computed = op(current);
// It might be more appropriate to use compare_exchange here
value.store(computed.get(), Ordering::Release);
break;
fn main() {
let data = vec![1, 2, 3, 4];
let iter = generate::generate![1, 2, ..data];
let data: Vec<i32> = iter.collect();
println!("{:#?}", data);
}
@Lucretiel
Lucretiel / retry_on_panic.rs
Last active May 5, 2021 06:27
A future adapter that retries the future if it panics
use std::{
future::Future,
panic::{catch_unwind, UnwindSafe},
pin::Pin,
task::{Context, Poll},
};
use pin_project::pin_project;
#[pin_project]
impl<'i, B> FromPercentEncoded<'i> for Cow<'i, B>
where
B: ToOwned,
&'i B: FromPercentEncoded<'i>,
<&'i B as FromPercentEncoded<'i>>::StrError: MaybeCannotGrowError,
<&'i B as FromPercentEncoded<'i>>::BytesError: MaybeCannotGrowError,
B::Owned: FromPercentEncoded<'i>,
{
type BytesError = CowError<
<B::Owned as FromPercentEncoded<'i>>::BytesError,
@Lucretiel
Lucretiel / webtoon-enhance.js
Created April 26, 2021 00:25
Enhancer script for webtoons.com
// ==UserScript==
// @name Webtoon Enhancer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://www.webtoons.com/en/*
// @icon https://www.google.com/s2/favicons?domain=webtoons.com
// @grant none
// @require https://raw.githubusercontent.com/Lucretiel/comics-enhance/master/enhance.js
@Lucretiel
Lucretiel / autodefault_demo.rs
Created April 10, 2021 01:06
Example code using autodefault
use autodefault::autodefault;
#[derive(Debug, Default)]
struct Big1 {
a: i32,
b: i32,
c: i32,
d: i32,
e: i32,
f: i32,
trait PrioritySearcher<'a, T> {
fn test(&mut self, item: &'a T) -> bool;
fn next_best(self) -> Option<&'a T>;
}
impl<'a, F, T> PrioritySearcher<'a, T> for F
where F: FnMut(&'a T) -> bool
{
fn test(&mut self, item: &'a T) -> bool {
(self)(item)
@Lucretiel
Lucretiel / dns_header_2.rs
Last active March 18, 2021 18:01
Alternative implementation of DNS headers
pub trait BuildFlags: Sized {
type Error;
fn try_build_flags(payload: [u8; 2]) -> Result<Self, Self::Error>;
}
pub struct Headers<F> {
pub id: u16,
pub flags: F,
pub qdcount: u16,
@Lucretiel
Lucretiel / dns_header.rs
Created March 17, 2021 03:01
Sample implementation of a header parser for DNS
use std::{convert::TryInto, time::Duration};
use nom::IResult;
use nom::{
bits::{bits, streaming::take as take_bits},
number::streaming::be_u16,
sequence::tuple,
Parser,
};
use nom_supreme::{error::ErrorTree, parser_ext::ParserExt};