Skip to content

Instantly share code, notes, and snippets.

View duarten's full-sized avatar
🤷‍♂️

Duarte Nunes duarten

🤷‍♂️
View GitHub Profile
@duarten
duarten / extension.rs
Created November 18, 2020 21:19
Rust AWS Lambda extension
use anyhow::{ensure, Result};
use reqwest::blocking::Client;
use reqwest::StatusCode;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::BufReader;
use std::time;
@duarten
duarten / meta-permutations.ts
Created September 19, 2020 05:37
Compile-time permutations in TypeScript
type Access<Tuple extends readonly unknown[], I extends string> =
I extends keyof Tuple ? Tuple[I] : never
type Swap<Tuple extends readonly unknown[], I extends string, J extends string> = {
[Index in keyof Tuple]: Index extends I ? Access<Tuple, J> : Index extends J ? Access<Tuple, I> : Tuple[Index]
}
type Length<Tuple extends readonly unknown[]> = Tuple["length"] extends number ? `${Tuple["length"]}` : never
type PushFront<Tuple extends readonly unknown[], E> = [E, ...Tuple]
@duarten
duarten / meta-unique.ts
Last active September 19, 2020 05:44
Tuple type containing only unique values
type ElementOf<T> = T extends (infer E)[] ? E : never;
type TupleHead<Tuple extends readonly unknown[]> =
Tuple extends [infer HeadElement, ...readonly unknown[]] ? HeadElement : never;
type TupleTail<Tuple extends readonly unknown[]> =
Tuple extends [unknown, ...infer TailElements] ? TailElements : never;
type Unique<Tuple extends readonly unknown[]>
= Tuple extends []
export function useAnimationDelay(
delay: number,
shouldShow: boolean,
onHide: () => void = () => {
return
},
): boolean {
const [display, setDisplay] = useState(shouldShow)
useEffect(() => {
@duarten
duarten / LICENSE
Last active July 16, 2022 08:31
Baze rule for 99designs/gqlgen
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
@duarten
duarten / raft.cc
Last active July 10, 2019 19:25
Raft protocol interfaces
#pragma once
#include <seastar/include/seastar/core/future.hh>
#include <seastar/include/seastar/core/lowres_clock.hh>
#include <seastar/include/seastar/core/reactor.hh>
#include <seastar/include/seastar/net/socket_defs.hh>
#include <cstdint>
#include <compare>
#include <vector>
@duarten
duarten / lazy_range.cc
Created September 26, 2018 11:28
A lazy sequence.
#include <iostream>
#include <functional>
#include <memory>
template<typename T>
class lazy_value {
union {
std::function<T()> _f;
mutable T _value;
};
@duarten
duarten / persistent_pipes_linux.md
Created April 25, 2018 00:10 — forked from CAFxX/persistent_pipes_linux.md
Persistent pipes/circular buffers for Linux

Persistent "pipes" in Linux

In a project I'm working on I ran into the requirement of having some sort of persistent FIFO buffer or pipe in Linux, i.e. something file-like that could accept writes from a process and persist it to disk until a second process reads (and acknowledges) it. The persistence should be both across process restarts as well as OS restarts.

AFAICT unfortunately in the Linux world such a primitive does not exist (named pipes/FIFOs do not persist

@duarten
duarten / CMakeLists.txt
Created June 15, 2017 11:54
Scylla CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(scylla)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z -DHAVE_HWLOC -DHAVE_DPDK")
include_directories(. build/release/gen seastar)
AUX_SOURCE_DIRECTORY(build/release/gen SOURCE_FILES)
AUX_SOURCE_DIRECTORY(build/release/gen/cql3 SOURCE_FILES)
AUX_SOURCE_DIRECTORY(. SOURCE_FILES)
@duarten
duarten / task_holder.cc
Last active March 27, 2017 19:19
Task holder with internal and external storage.
class task {
public:
virtual ~task() noexcept {}
virtual void run() noexcept = 0;
};
template <typename Func>
class lambda_task final : public task {
Func _func;
public: