Skip to content

Instantly share code, notes, and snippets.

@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
#[derive(Debug)]
struct Resource(i32);
impl Drop for Resource {
fn drop(&mut self) {
println!("goodbye from {}", self.0);
}
}
enum Error {
use libc;
#[derive(Debug)]
#[repr(C)]
struct Resource(i32);
impl Drop for Resource {
fn drop(&mut self) {
println!("goodbye from {}", self.0);
}
@justanotherdot
justanotherdot / unimported
Last active January 19, 2021 03:51
Find all javascript source files imported zero or one times.
suffix="js"
for f in $(rg -lt $suffix . src | xargs basename | sed -e "s/\.$suffix//"); do
count=$(rg $f -l | wc -l | xargs );
if [ $(( $count )) -eq 1 ] || [ $(( $count )) -eq 0 ]; then
echo "count: $count file: $f";
fi;
done
@justanotherdot
justanotherdot / last-touched
Created January 19, 2021 03:43
Compare main branch to topic/foo and who touched files last, and when they were touched.
for f in $(git diff --name-only topic/foo) ; do git --no-pager log -z --pretty=format:"$f was last touched by %cn %cr%n" $f | head -1; done
#![feature(step_trait)]
#![feature(step_trait_ext)]
// This is unsightly.
use std::iter::Step;
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone)]
enum Thing {
One,
@justanotherdot
justanotherdot / cargo-bench-template.rs
Last active January 13, 2021 09:43
Drop in benchmark snippet for Rust projects that want to use cargo-bench on nightly.
// NB. this must be placed at the top of the module.
#![feature(test)]
#[cfg(test)]
mod tests {
extern crate test;
use super::*;
use test::{black_box, Bencher};
use tokio::net::{TcpListener, TcpStream};
struct Server {
listener: TcpListener,
}
#[derive(Debug)]
enum Error {
IoError(std::io::Error),
}