Skip to content

Instantly share code, notes, and snippets.

View miquels's full-sized avatar

Miquel van Smoorenburg miquels

  • Adyen
  • Amsterdam
View GitHub Profile
@miquels
miquels / average-geolocation.pl
Last active August 9, 2019 08:10 — forked from tlhunter/average-geolocation.js
Calculate the center/average of multiple GeoLocation coordinates
#
# Calculate the center/average of multiple GeoLocation coordinates
# Expects an array of array refs: ([lat, long], [lat, long]).
#
# @url http://stackoverflow.com/a/14231286/538646
#
use warnings;
use strict;
sub PI() { 4 * atan2(1, 1) }
@miquels
miquels / Cargo.toml
Created October 18, 2020 07:59
word count
[package]
name = "wc"
version = "0.1.0"
edition = "2018"
[dependencies]
regex = "1"
lazy_static = "1"
use std::{
fs::File,
io::{Read, Write},
time::Instant,
};
use tokio::task::{self, JoinHandle};
async fn compute() {
let handles: Vec<JoinHandle<_>> = (0..1000)
.map(|_| {
@miquels
miquels / rust_boxed_macro.md
Last active June 21, 2021 07:26
In-place box construction

The Rust Box::new(X) method first creates X on the stack, then allocates memory and copies X from the stack to the heap. That is very wasteful if X is large.

Simple macro that abuses vec! to do more or less the same as the copyless crate:

macro_rules! boxed {
@miquels
miquels / systemtime_into_unixtime_ns.rs
Last active November 2, 2022 10:52
Rust SystemTime conversion to unixtime in nanoseconds
trait SystemTimeToUnixTime {
fn unixtime(&self) -> i64;
fn unixtime_ns(&self) -> i64;
}
impl SystemTimeToUnixTime for std::time::SystemTime {
fn unixtime(&self) -> i64 {
match self.duration_since(std::time::SystemTime::UNIX_EPOCH) {
Ok(n) => n.as_secs().try_into().unwrap_or(i64::MAX),
Err(t) => t.duration().as_secs().try_into().map(|t: i64| -t).unwrap_or(i64::MIN),