Skip to content

Instantly share code, notes, and snippets.

View tesaguri's full-sized avatar

Daiki Mizukami tesaguri

View GitHub Profile
@tesaguri
tesaguri / extract_from_arrays.rb
Last active January 6, 2016 14:41
Extract values from multiple arrays one by one.
# The arrays should not contain nil.
ary1 = [1, 2]
ary2 = %w(A B C)
ary3 = %i(a b)
while (elm = ary1.shift || ary2.shift || ary3.shift) do
p elm
end
#=> 1
#=> 2
@tesaguri
tesaguri / recurrence_relation_iter.rs
Created January 31, 2017 18:53
Create an iterator from a closure representing a recurrence relation.
use std::mem;
struct RecRel<T,F> {
val: T,
succ: F,
}
impl<T,F> RecRel<T,F> {
fn new(init: T, succ: F) -> Self where F: Fn(&T) -> T {
RecRel {
@tesaguri
tesaguri / Cargo.toml
Last active May 13, 2017 06:31
011_BOT-rs
[package]
name = "ikeay_011_BOT-rs"
version = "0.1.0"
[dependencies]
futures = "0.1"
serde_json = "1"
tokio-core = "0.1"
tweetust = "0.7"
twitter-stream = { git = "https://github.com/dmizuk/twitter-stream-rs", features = ["tweetust"] }
@tesaguri
tesaguri / Cargo.toml
Last active May 13, 2017 06:33
011_BOT-rs (egg-mode)
[package]
name = "ikeay_011_BOT-rs"
version = "0.1.0"
[dependencies]
egg-mode = "0.8"
futures = "0.1"
serde_json = "1"
tokio-core = "0.1"
twitter-stream = { git = "https://github.com/dmizuk/twitter-stream-rs", features = ["egg-mode"] }
@tesaguri
tesaguri / boxed_str_vs_string.rs
Last active July 9, 2017 02:26
Benchmark test: Box<str> vs String
#![feature(test)]
extern crate test;
use std::collections::HashMap;
use test::Bencher;
const KEY0: &str = "A benchmark test";
const KEY1: &str = "for";
const KEY2: &str = "`Box<str>` and `String`";
@tesaguri
tesaguri / untyped.rs
Last active November 1, 2021 03:51
Poor man's type erasure in Rust
use std::any::TypeId;
use std::mem;
#[derive(Debug)]
pub struct Untyped {
type_id: TypeId,
data: *mut [()],
}
impl Untyped {
@tesaguri
tesaguri / uppercase.rs
Created September 9, 2018 10:04
Converting a `Display` into another `Display`
use std::fmt::{self, Display, Formatter, Write};
pub struct Uppercase<T>(T);
impl<T: Display> Display for Uppercase<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
struct Adapter<'a, 'b: 'a>(&'a mut Formatter<'b>);
impl<'a, 'b: 'a> Write for Adapter<'a, 'b> {
fn write_str(&mut self, s: &str) -> fmt::Result {

Keybase proof

I hereby claim:

  • I am tesaguri on github.
  • I am tesaguri (https://keybase.io/tesaguri) on keybase.
  • I have a public key whose fingerprint is 293A DA14 A1CB 0B1C 2313 3101 1047 8E59 8B94 4AA2

To claim this, I am signing this object:

@tesaguri
tesaguri / debug_any.rs
Last active September 15, 2024 00:24
Experiment to implement a `Debug`-like trait for any type using specialization.
#![feature(specialization)]
// #![feature(min_specialization)] // error: cannot specialize on trait `Debug`
use core::fmt::{self, Debug, Formatter};
#[test]
#[should_panic(
expected = "called `Result::unwrap_any()` on an `Err` value: playground::unwrap_non_debug::NonDebug"
)]
fn unwrap_non_debug() {
@tesaguri
tesaguri / request.rs
Created December 4, 2018 00:59
Defining pseudo custom attributes in a macro
macro_rules! request {
(
@parsing $(#[$attrs:meta])*; #[get($uri:expr)] $(#[$rest_attrs:meta])*
struct $Name:ident;
$($rest:tt)*
) => {
request! {
@GET($uri)
$(#[$attrs])*
$(#[$rest_attrs])*