Skip to content

Instantly share code, notes, and snippets.

View segfo's full-sized avatar

segfo segfo

View GitHub Profile
@segfo
segfo / buffering_test.py
Created June 27, 2019 17:38
子プロセス起動&子プロセスの標準出力を順次取得
import time
cnt=0
while True:
cnt+=1
if cnt > 5:
break
time.sleep(1)
print("python: "+str(cnt))
@segfo
segfo / config_deserializer.rs
Last active June 26, 2019 15:54
汎用デシリアライザ
use std::marker::PhantomData;
use serde_derive::{Serialize,Deserialize};
use serde::de::DeserializeOwned;
struct TomlConfigDeserializer<T>{
_t:PhantomData<T>
}
use std::fs::OpenOptions;
use std::io::{BufReader,prelude::*};
@segfo
segfo / resource_pool.rs
Last active June 23, 2019 03:35
汎用リソースプール
use std::collections::{VecDeque,HashMap};
// リソースプール
pub struct ResourceAllocator<K,V>{
pool:VecDeque<V>,
used_pool:HashMap<K,V>
}
impl<K,V> ResourceAllocator<K,V>
where K:std::cmp::Eq+std::hash::Hash+Clone{
pub fn new()->Self{
ResourceAllocator{
@segfo
segfo / ideal_mixed_ground_meat_calclator.rs
Last active May 4, 2019 09:05
牛肉豚肉合いびき肉計算機初版
#[derive(Clone)]
struct RatioBuilder{
n:i64,
a_name:Option<String>,
a_ratio:Option<i64>,
b_name:Option<String>,
b_ratio:Option<i64>,
}
impl RatioBuilder{
@segfo
segfo / random_string.py
Last active April 7, 2019 13:01
ランダム文字列ワンライナー
l=123;s=string.ascii_letters+string.digits;print(''.join(random.sample((s)*math.ceil(l/(len(s))),k=l)))
@segfo
segfo / main.c
Last active February 28, 2019 13:25
名前付きMutexをつくるだけ。動作確認用。
// C版
// Rust版で作成するMutex名と同じにすると、競合させられる。
#include<windows.h>
#include<stdio.h>
int main() {
// 名前付きミューテックスを作り、確保する
CreateMutexA(NULL,0,"AAAAA_NAMED_MUTEX\0");
if(GetLastError()==183){
// すでに獲得済みのミューテックスの場合は183がGetLastErrorから返却される。
@segfo
segfo / polymorphism.rs
Last active June 4, 2023 18:03
ポリモーフィズムをRustでやってみる
// 参考:https://qiita.com/Nossa/items/a93024e653ff939115c6
trait Shape{
fn area(&self)->f32;
fn whoami(&self)->String;
}
struct Triangle {
pub base:f32,
pub height:f32
}
extern crate crypto_hash;
extern crate ssdeep;
use crypto_hash::{Algorithm, hex_digest};
extern crate levenshtein;
use levenshtein::levenshtein;
fn main() {
// ハッシュ化対象の文字列
let hashes=&[b"give me hashsing!!",b"give you hashsing!",b"AAAAAAAAAAAAAAAAA!"];
// 記録用のベクタ
use std::thread;
use std::time::Duration;
use std::sync::{Arc,Mutex};
use std::ptr::NonNull;
// いんなー
// 32ビット整数値と、外部の配列へのポインタ(C-FFIとかでよくやるやつ)
struct Inner{
num:u32,
u8ptr:SafeRawU8Ptr
@segfo
segfo / random_string.rs
Created April 29, 2018 09:10
Rustでランダム文字列をホイホイ出力
extern crate rand;
use rand::Rng;
fn main() {
let mut vec=(0x20..0x7f)
.filter_map(|n|std::char::from_u32(n as u32))
.collect::<Vec<char>>();
rand::thread_rng().shuffle(&mut vec);
vec.iter().for_each(|n|print!("{}",n));
}