Skip to content

Instantly share code, notes, and snippets.

View tesaguri's full-sized avatar

Daiki Mizukami tesaguri

View GitHub Profile
@tesaguri
tesaguri / Cargo.toml
Last active April 6, 2025 11:00
Example code for inter-process communication in Rust using Unix domain socket.
[package]
name = "tokio-serde-json-ipc"
version = "0.0.0"
edition = "2018"
publish = false
[dependencies]
futures = "0.1"
serde_json = "1"
tokio = "0.1"
@tesaguri
tesaguri / Cargo.toml
Last active August 31, 2019 16:15
HTTP/2 client example with hyper
[package]
name = "hyper-h2"
version = "0.0.0"
edition = "2018"
publish = false
[dependencies]
ct-logs = "0.6"
hyper = "0.12"
hyper-rustls = "0.17"
@tesaguri
tesaguri / switch.rs
Created September 15, 2020 00:32
A `switch` macro with "fall-through" behavior
macro_rules! switch {
(($target:expr) { $($inner:tt)* }) => {
switch! { @match ($target) { $($inner)* } {} }
};
(@match ($target:expr) { $pat:pat => $blk:expr, $($rest:tt)* } { $($buf:tt)* }) => {
switch! { @match ($target) { $($rest)* } {
$($buf)*
$pat => switch! { @fall $blk; $($rest)* },
}}
};
@tesaguri
tesaguri / array_default.rs
Created October 9, 2020 10:46
An experimental implementation of a `default` constructor for `[T; N]`
#![feature(min_const_generics)]
use std::mem::{self, MaybeUninit};
use std::ptr;
pub fn array_default<T: Default, const N: usize>() -> [T; N] {
struct Guard<T> {
dst: *mut T,
i: usize,
}
@tesaguri
tesaguri / lib.rs
Created December 1, 2020 23:36
A wrapper for `serde::Deserializer` that returns error when deserialzing `IgnoredAny`
use std::fmt::{self, Formatter};
use std::marker::PhantomData;
use serde::{de, Deserialize};
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Deserialize)]
@tesaguri
tesaguri / maybe_pin.rs
Created December 29, 2020 05:05
A pointer type which may or may not be pinned.
#![feature(arbitrary_self_types)]
use std::marker::{PhantomPinned, Unpin};
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use pin_project_lite::pin_project;
#[derive(Copy, Clone)]
pub enum MaybePin<P> {
@tesaguri
tesaguri / pin_project_cfg.rs
Last active January 15, 2022 05:35
A wrapper around `pin_project_lite::pin_project!` to handle `cfg` attributes on enum variants and fields. (cf. <https://github.com/taiki-e/pin-project-lite/issues/3>)
macro_rules! pin_project_cfg {
($(#[$($attr:tt)*])* $vis:vis enum $($rest:tt)+) => {
pin_project_cfg! {
@outer [$(#[$($attr)*])* $vis enum] $($rest)+
}
};
// Accumulate type parameters and `where` clause.
(@outer [$($accum:tt)*] $tt:tt $($rest:tt)+) => {
pin_project_cfg! {
@outer [$($accum)* $tt] $($rest)+
@tesaguri
tesaguri / io_future.rs
Created February 6, 2021 22:34
An example code for `type_alias_impl_trait` feature
#![feature(type_alias_impl_trait)]
use std::error;
use std::io;
use futures_core::TryFuture;
use futures_util::future::{MapErr, TryFutureExt};
#[allow(type_alias_bounds)] // The bound seems not to be removable.
pub type IoFuture<F: TryFuture> = MapErr<F, impl Fn(<F as TryFuture>::Error) -> io::Error>;
@tesaguri
tesaguri / fmt_cmp.rs
Created October 10, 2021 12:24
`x.to_string().cmp(&y.to_string())` without allocating the string (not throughly tested yet)
use std::cmp::Ordering;
use std::fmt::{self, Display, Write};
struct FmtCmp<T>(T);
impl<T: Display> PartialEq for FmtCmp<T> {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
@tesaguri
tesaguri / cmp_dec.rs
Last active December 17, 2021 14:35
Lexicographic comparison of integers
#![cfg_attr(test, feature(test))]
#![cfg_attr(feature = "int_log", feature(int_log))]
use std::cmp::Ordering;
macro_rules! imp {
($lhs:expr, $rhs:expr, |$min:ident, $max:ident| $divisor:expr) => {{
// Because `'0' < '9' < 'A' < 'Z' (< 'a' < 'z')` holds, we don't need to special-case
// radixes greater than 10.