Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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);
}