Skip to content

Instantly share code, notes, and snippets.

View md5.rs
fn imagehash(path: &Path) -> std::io::Result<Digest> {
let mut f = File::open(path)?;
let mut buffer = [0u8; 1024*1024];
let mut context = Context::new();
loop {
let read = f.read(&mut buffer)?;
if 0 == read {
return Ok(context.compute());
}
View bashrc-prompt.sh
# Append history
shopt -s histappend
#HISTCONTROL="ignorespace:ignoredups:erasedups"
HISTCONTROL=ignorespace:erasedups
HISTSIZE=100000
HISTFILESIZE=-1
CAA_AT_PROMPT=1
function __caa_before_command {
if [ -z "$CAA_AT_PROMPT" ]; then
View st.rs
#!/usr/bin/env run-cargo-script
use std::process::Command;
fn main() -> std::io::Result<()> {
let args: Vec<std::ffi::OsString> = std::env::args_os().collect();
if args.len() > 1 {
eprintln!("no arguments allowed");
std::process::exit(1);
}
View st.rs
#!/usr/bin/env run-cargo-script
//! ```cargo
//! [dependencies]
//! ```
use std::process::Command;
fn main() -> std::io::Result<()> {
let mut path = std::env::current_dir()?;
View yaml_overrides.py
import collections
import yaml
# Try to preserve order in the front matter.
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))
View datausage.py
#!/usr/bin/env python3
from xfinity_usage.xfinity_usage import XfinityUsage
import datetime
import json
import os
results = XfinityUsage(
os.environ['XFINITY_USER'],
os.environ['XFINITY_PASSWORD'],
@chadaustin
chadaustin / tmpfile.cpp
Last active Jan 11, 2018
How to atomically write a file without giving it a temporary name
View tmpfile.cpp
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
// Can't get O_TMPFILE to work for some reason.
#ifndef __O_TMPFILE
# define __O_TMPFILE (020000000 | __O_DIRECTORY)
#endif
View mmaptest.cpp
// g++ -Wall -o mmaptest mmaptest.cpp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <assert.h>
int main() {
View Promise.cpp
#include <stdio.h>
#include <typeinfo>
#include <functional>
#include <string>
#include <type_traits>
#include <vector>
struct Unit {};
template<typename Result, typename Error>
@chadaustin
chadaustin / ub.md
Last active Jun 14, 2017
Undefined Behavior in Unit Test
View ub.md

Imagine an API like this:

bool double_if_positive(float input, float* output) {
  if (input > 0) {
    *output = input * 2.0f;
    return true;
  } else {
    return false;
  }
You can’t perform that action at this time.