Skip to content

Instantly share code, notes, and snippets.

View Kerollmops's full-sized avatar
🍼
Meilisearch needs care

Clément Renault Kerollmops

🍼
Meilisearch needs care
View GitHub Profile
@Kerollmops
Kerollmops / rsync-ssh.sh
Created March 19, 2020 10:14
Remote sync a cargo repository by using fswatch and ssh with a custom identity key.
fswatch --exclude '.git' --exclude 'target' . | while read num; do
rsync -azP --exclude=.git --exclude=target -e 'ssh -i ~/.ssh/id_rsa' . [email protected]:<DEST>
done
@Kerollmops
Kerollmops / iter_kit.rs
Last active January 6, 2020 17:43
A kit of simple adapter Iterators for tricky situations (no external library needed)
/// An adapter function that returns an iterator associated
/// to a boolean indicating if this is the first item returned.
///
/// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7ea95cd91a26de21c680ad7de8669c8d
///
/// ```
/// let words = ["Hello", "world", "!"];
/// let mut iter = is_first(&words);
///
/// assert_eq!(iter.next(), Some((true, "Hello")));
@Kerollmops
Kerollmops / cor.rs
Last active December 2, 2019 22:08
Clone on Resize - An alternative to the Cow type, keep a mutable slice until you really need to grow or shrink it.
pub enum Cor<'a, T: 'a> {
Borrowed(&'a mut [T]),
Owned(Vec<T>),
}
impl<T: Clone> Cor<'_, T> {
pub fn as_resizable(&mut self) -> &mut Vec<T> {
match self {
Cor::Borrowed(slice) => {
*self = Cor::Owned(slice.to_vec());
@Kerollmops
Kerollmops / user-typing.lua
Last active November 26, 2019 20:54
wrk script that simulate user typing queries
full_query = "I love paris and new york with my friends "
length = 1
local char_to_hex = function(c)
return string.format("%%%02X", string.byte(c))
end
local function urlencode(url)
if url == nil then
return
@Kerollmops
Kerollmops / zerocopy-lmdb.rs
Last active October 4, 2019 08:13
This little bit of code could makes the lmdb usage really easy and fast
use std::{io, marker, str};
use std::borrow::Cow;
use zerocopy::{LayoutVerified, AsBytes, FromBytes};
use serde::{Serialize, Deserialize, de::DeserializeOwned};
// Do not forget to patch zerocopy to make it support str
// by using the `trivial_bounds` feature
pub trait EPAsBytes {
fn as_bytes(&self) -> Cow<[u8]>;
@Kerollmops
Kerollmops / easy-memlog.sh
Created September 16, 2019 20:01
Record %CPU and RSS
#!/bin/bash
while true; do
ps -p $1 -o\%cpu,rss | awk 'NR>1 { gsub(",",".",$1); print $1","$2}'
sleep 0.2
done
@Kerollmops
Kerollmops / memlog.sh
Last active July 24, 2021 12:55
Record memory usage of a process
#!/usr/bin/env bash
# usage: memlog.sh $(pidof PROCESS_NAME) [ PATH_FOLDER ]
# on osx pidof can be replaced by pgrep
# usage: memlog.sh $(pgrep PROCESS_NAME) [ PATH_FOLDER ]
set -e
PID=$1
@Kerollmops
Kerollmops / group-by-generic-function.rs
Created January 31, 2019 22:06
Generic function type thta is in fact a function pointer (`fn` != `Fn`)
fn linear_group(&self) -> LinearGroupBy<T, fn(&T, &T) -> bool>
where T: Ord
{
LinearGroupBy::new(self, |a, b| a == b)
}
@Kerollmops
Kerollmops / all-eq.rs
Created August 6, 2018 12:07
A function to test if all values of a slice are equal (to the first element).
fn all_eq<T: Eq>(slice: &[T]) -> bool {
match slice.split_first() {
Some((first, others)) => others.iter().all(|x| x == first),
None => true,
}
}
#[test]
fn all_eq_easy_eq() {
assert!(all_eq(&[1, 1, 1, 1, 1]));
@Kerollmops
Kerollmops / split-first.rs
Created August 6, 2018 12:06
The `split_first` function for any iterator
// prefer using IntoIterator ?
fn split_first<T, I>(mut iter: I) -> Option<(T, I)>
where I: Iterator<Item=T>
{
match iter.next() {
Some(first) => Some((first, iter)),
None => None,
}
}