Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
justanotherdot / box.txt
Last active March 28, 2022 11:41
Box unicode characters for diagrams in documentation, terminals, etc.
─ ━ │ ┃ ┄ ┅ ┆ ┇ ┈ ┉ ┊ ┋ ╌ ╍ ╎ ╏
┌ ┍ ┎ ┏ ┐ ┑ ┒ ┓
└ ┕ ┖ ┗ ┘ ┙ ┚ ┛
├ ┝ ┞ ┟ ┠ ┡ ┢ ┣ ┤ ┥ ┦ ┧ ┨ ┩ ┪ ┫
┬ ┭ ┮ ┯ ┰ ┱ ┲ ┳
@justanotherdot
justanotherdot / box-example.txt
Last active March 28, 2022 08:12
Box unicode characters.
┏━━━┓
┃ ┃
┗━━━┛
┏━┳━┳━┓
┃ ┃ ┃ ┃
┗━┻━┻━┛
╭───╮
│ │
#![feature(allocator_api)]
use std::alloc::Allocator;
use std::alloc::Global;
use std::marker::PhantomData;
// Basicaly a Vec.
pub struct V<T, A: Allocator = Global> {
pub buf: Rv<T, A>,
pub len: usize,
use rand::Rng;
use std::default::Default;
use std::{cell::RefCell, collections::HashMap, time::Duration};
use tokio::{self, select, task, task_local, time::timeout};
fn rand_gen_millis() -> (u64, u64) {
let mut rng = rand::thread_rng();
let t1 = rng.gen_range(0..10);
let t2 = rng.gen_range(0..10);
(t1, t2)
use tokio;
use tokio::{task, task_local};
use std::cell::RefCell;
use std::collections::HashMap;
#[tokio::main]
async fn main() {
let handler = task::spawn(async {
task_local! {
pub static WRITER: RefCell<HashMap<String, i64>>;
@justanotherdot
justanotherdot / rust-underscore-idiom.rs
Created January 29, 2021 21:16
Avoid prefixing variables you will use with an underscore.
// Rust Idiom:
// prefixing a variable with an underscore
// signals to others that the variable
// isn't going to be used, not that it is
// intentionally private or an updated
// variable.
main () {
let x = 12;
// _x reads as
@justanotherdot
justanotherdot / sans-clone-derive.rs
Created January 29, 2021 21:10
Cloning borrows to things that lack a Clone impl simply return the original borrow.
// NB. fix
// #[derive(Debug, Clone)]
#[derive(Debug)]
struct Context;
fn dupe_context(context: &Context) -> Context {
context.clone()
}
fn main() {
@justanotherdot
justanotherdot / asref-borrow.rs
Created January 29, 2021 20:57
You may be borrowing the wrapper rather than the contents that you want.
#[derive(Debug, Clone)]
struct Resource(usize);
#[derive(Debug)]
struct Error;
fn may_fail(ix: usize) -> Result<Resource, Error> {
if rand::random::<f64>() < 0.5 {
Err(Error)
} else {
@justanotherdot
justanotherdot / date-cmp.sh
Created January 28, 2021 02:15
Compare twwo dates.
: {d1:=1}
: {d2:=2}
: {op:?"provide an operator for comparison"}
lhs=date -j -f "%F" "$d1" +%s 2>/dev/null
rhs=date -j -f "%F" "$d2" +%s 2>/dev/null
case $op in
"eq")
[ lhs -eq rhs ] ;;
"ge")
@justanotherdot
justanotherdot / clone-repos.sh
Last active January 28, 2021 02:08
Clone all repos for an org via the gh tool.
: {org:?"need to set an org name"}
: {max_pages:?"need to set maximum number of pages"}
for PAGE in $( seq $max_pages ); do
gh api ‘orgs/$org/repos?per_page=100&page=‘“$PAGE” | jq -r ‘map(.name) | .[]' | while read NAME; do
git clone “git@github.com:$org/$NAME”
done