Skip to content

Instantly share code, notes, and snippets.

@rkjnsn
rkjnsn / void.ts
Created September 21, 2020 16:43
// This means "callback takes no arguments and can return anything, but I won't
// look at the result."
function foo(callback: ()=>void) {
callback();
}
// Since this uses the union operator, one would expect it to accept all of the
// types allowed by the previous signature and possibly more. Instead, now it
// will only accept undefined or a PromiseLike when called directly.
function bar(callback: ()=>void|PromiseLike<void>) {
@rkjnsn
rkjnsn / map2dmtable.py
Last active July 10, 2020 21:25 — forked from coderjo/map2dmtable.py
Quick Python script to generate a linux device mapper table from a ddrescue map file, placing errors where ddrescue had bad blocks.
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser(description='Create a device mapper table from a ddrescue map file. '
'Blocks that are marked as bad will be mapped to the error target, while successfully-read '
'blocks will be passed through to the device. The dm table will be written to stdout so it can '
'be piped directly into dmsetup.')
parser.add_argument('mapfile', help='The ddrescue map file to convert')
parser.add_argument('device', help='The device containing the image that corresponds to the map file. (typically a loop device)')
@rkjnsn
rkjnsn / genrc.rs
Last active June 13, 2017 17:13 — forked from anonymous/playground.rs
Generic ref counting
use std::ops::Deref;
use std::rc::Rc;
use std::sync::Arc;
trait RefCounted<T>: Clone + Deref<Target=T> {
fn new(value: T) -> Self;
}
impl<T> RefCounted<T> for Rc<T> {
fn new(value: T) -> Self {
@rkjnsn
rkjnsn / prime-sieve.cpp
Last active August 26, 2015 22:02 — forked from lelandbatey/prime-sieve.cpp
Sieve of Eratosthenes in C++ based on Python implementation written by David Eppstein.
#include <iostream>
#include <map>
#include <cmath>
#include <vector>
// Sieve of Eratosthenes
//
// This is a simple implementation of the Sieve of Eratosthenes, implemented in
// C++. This is a port of the excellent and very terse implementation by David
// Eppstein[0]. This port is meant to be a straightforward naive port of the
@rkjnsn
rkjnsn / scopedrc.rs
Last active August 29, 2015 14:20
A ScopedRc type that performs cycle collection with correct lifetime enforcement.
use std::boxed;
use std::cell::{Cell, UnsafeCell};
use std::cmp::{Ordering, max};
use std::fmt;
use std::hash::{Hasher, Hash};
use std::intrinsics::{abort, assume};
use std::isize;
use std::marker::PhantomData;
use std::mem::{self, forget, min_align_of, size_of, transmute};
use std::ops::Deref;
@rkjnsn
rkjnsn / nocyclerc.rs
Created April 27, 2015 21:05
Experiment with creating NoCycleRc
use std::boxed;
use std::cell::Cell;
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hasher, Hash};
use std::marker::PhantomData;
use std::mem::{forget, min_align_of, size_of};
use std::ops::Deref;
use std::ptr;
use std::rt::heap::deallocate;
@rkjnsn
rkjnsn / rc.rs
Created April 27, 2015 21:00
Initial attempt and implementing scoped RcGuard to clean up cycles.
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
@rkjnsn
rkjnsn / widen.rs
Created January 17, 2015 01:33
Sample implementation of a Widen trait for Rust
use widen::Widen;
use widen::widen as w; // Use w as shorthand for widening
// Contains Widen trait, impls, and widen() convenience function
mod widen {
// Performs a lossless conversion of an integer to a wider type
pub trait Widen<T> {
fn widen(self) -> T;
}