Skip to content

Instantly share code, notes, and snippets.

@Arnavion
Arnavion / async-await.ts
Last active May 30, 2021 06:25
Promise.any - async/await vs then()
async function any<T>(promises: Promise<T>[]): Promise<T> {
const rejections: any[] = [];
for (const promise of promises) {
try {
return await promise;
}
catch (reason) {
rejections.push(reason);
}
@Arnavion
Arnavion / async-await.ts
Last active May 30, 2021 06:24
Promise.first
async function first<T>(promises: Promise<T>[]): Promise<T> {
const rejections: any[] = [];
for (const promise of promises) {
try {
return await promise;
}
catch (reason) {
rejections.push(reason);
}
@Arnavion
Arnavion / make_deserializable.rs
Last active October 3, 2016 16:27
Macro for deserializing structs from JSON for Rust stable
//! Exports a single macro named make_deserializable that can be used in place of #[derive(serde::Deserialize)] on structs.
//! Supports regular structs and tuple structs of u64.
//! Only works with JSON deserialization.
//!
//! Eg: make_deserializable!(struct Foo { bar: String, baz: i32, });
//! Eg: make_deserializable!(struct Bar(u64));
macro_rules! impl_deserialize_struct {
(struct $struct_name:ident {
$($field_name:ident: $field_type:ty,)*
@Arnavion
Arnavion / doc.md
Created September 8, 2017 03:09
tiny-async-await

A tiny crate that provides async and await macros to create futures::Futures and futures::Streams.

Code that uses these macros requires #![feature(conservative_impl_trait, generators)]

  • Returning a futures::Future

     #![feature(conservative_impl_trait, generators)]
    
     #[macro_use]
@Arnavion
Arnavion / ip-flash.awk
Created November 18, 2019 19:34
Flash Raspberry Pi's IP address using its LED
#!/usr/bin/awk -f
BEGIN {
while (1) {
split(exec_line_match("ip addr show dev eth0", " inet "), ip_a_parts, " ")
split(ip_a_parts[2], ip_value_parts, "/")
split(ip_value_parts[1], ip_parts, ".")
reset();
@Arnavion
Arnavion / lib.rs
Created December 3, 2019 00:36
launder-rs
#![deny(rust_2018_idioms, warnings)]
#![deny(clippy::all, clippy::pedantic)]
//! This crate defines a `launder` macro that can be used to create a borrow that is scoped to the block where this macro is invoked,
//! even if the expression to create the borrow returns a `'static` borrow.
/// This macro can be used to create a borrow that is scoped to the block where this macro is invoked,
/// even if the expression to create the borrow returns a `'static` borrow.
///
/// For example, dereferencing a raw pointer with `&*` creates a borrow with an arbitrary lifetime which can be `'static`.
@Arnavion
Arnavion / Cargo.toml
Last active May 30, 2021 08:40
transducers-rs
[package]
name = "transducers"
version = "0.1.0"
authors = ["Arnavion <[email protected]>"]
edition = "2018"
[dependencies]
num-traits = { version = "0.2", default-features = false }
[dev-dependencies]
// If lines 72 and 73 are uncommented and 74 is commented out, the assert on line 77 succeeds (as expected).
// If lines 72 and 73 are commented out and 74 is uncommented, the assert on line 77 fails.
//
// $ zig run -O Debug ./main.zig
//
// Linux x86_64, 0.9.0-dev.2010+e8b39960b
const std = @import("std");
const Scanner = struct {
@Arnavion
Arnavion / gps-nmea
Created August 12, 2022 19:44
Pinephone pmoS GPS
# /etc/init.d/gps-nmea
#!/sbin/openrc-run
command=/usr/local/bin/gps-nmea.py
command_args=
command_background=true
pidfile="/run/${RC_SVCNAME}.pid"
@Arnavion
Arnavion / background.js
Created September 1, 2022 17:51
HN Blocklist Extension
browser.runtime.onMessage.addListener(async message => {
const { param } = message;
switch (param) {
case "get":
return getRecords();
case "set":
return setRecords(message.records);