Skip to content

Instantly share code, notes, and snippets.

#[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 / 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
@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
#![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),
}
trait Fruit {
fn accept<A: FruitVisitor>(self, visitor: &mut A);
}
#[derive(Debug)]
struct Orange;
#[derive(Debug)]
struct Apple;
#[derive(Debug)]
struct Banana;
# Description: Static linking of rustc on debian buster with glibc.
# Notes:
# * Obligatory rustc docs: https://doc.rust-lang.org/reference/linkage.html
# * proc-macro does not seem to work with static linking, regardless of libc flavor.
# cf. https://github.com/rust-lang/cargo/issues/7563#issuecomment-553526254
# * traditionally, to perform static linking static libraries are needed, hence the libgcc-8-dev install below.
# * static linking is the default with musl and rustc afaict, hence builds on, say, alpine
# should automatically be static, whereas for glibc we need to pass `-C target-feature=+crt-static`
# to `RUSTFLAGS`. You can alternatively place this in your `Cargo.toml` under the `rustflags` key, e.g.
# ```
@justanotherdot
justanotherdot / serde-byte-array-deser.rs
Last active December 22, 2020 03:34
Various ways (and non-ways) to deserialize to byte arrays use serde.
use csv::ReaderBuilder;
use std::error::Error;
use std::path::PathBuf;
use structopt::StructOpt;
//use assert_no_alloc::*;
//#[cfg(debug_assertions)]
//#[global_allocator]
//static A: AllocDisabler = AllocDisabler;