Skip to content

Instantly share code, notes, and snippets.

View miquels's full-sized avatar

Miquel van Smoorenburg miquels

  • XS4ALL
  • Amsterdam
View GitHub Profile
@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),
@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 {
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 / 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"
@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) }
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <resolv.h>
#include <netdb.h>
@miquels
miquels / pam.c
Created December 7, 2018 10:22
PAM authentication boilerplate.
/* cc -DTEST -Wall -o pamtest pam.c -lpam */
#include <security/pam_appl.h>
#include <sys/resource.h>
#include <string.h>
#include <stdlib.h>
struct creds {
char *user;
char *password;
@miquels
miquels / trait_alias_macro.rs
Last active September 19, 2018 22:07
Rust trait alias macro
extern crate futures;
use futures::prelude::*;
use std::io;
macro_rules! trait_alias {
($alias:ident = $($trait_:tt)*) => {
trait $alias: $($trait_)* {}
impl<T: $($trait_)*> $alias for T {}
}
@miquels
miquels / native-tls-acceptor-from-pem-files.rs
Created August 9, 2018 16:00
native_tls::TlsAcceptor from .key/.crt PEM files instead of .p12 file.
use std::io::{self,Error,ErrorKind};
use std::path::Path;
use openssl::pkey::PKey;
use openssl::x509::X509;
use openssl::pkcs12::Pkcs12;
use openssl::stack::Stack;
use native_tls::{self,Identity};
fn read_pems(key: impl AsRef<Path>, cert: impl AsRef<Path>, password: &str) -> io::Result<Vec<u8>> {
@miquels
miquels / enable_so_peercred_dgram.rs
Created August 2, 2018 20:42
enable SO_PEERCRED in Rust on a UNIX domain socket
extern crate libc;
extern crate tokio_uds;
use std::os::unix::io::AsRawFd;
// this is automatically enabled on stream socket - only for
// datagram sockets do you need to manually enable it.
fn enable_so_passcred(unix_socket: &tokio_uds::UnixListener) {
let fd = unix_socket.as_raw_fd();
let val: libc::c_int = 1;