Skip to content

Instantly share code, notes, and snippets.

View ssrlive's full-sized avatar

ssrlive

  • telegram: realssrlive
  • ssrlivebox(at)gmail(dot)com
View GitHub Profile
@ssrlive
ssrlive / get_interface_info.rs
Last active September 2, 2023 03:33
GetInterfaceInfo function (iphlpapi.h)
//
// https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getinterfaceinfo#examples
//
// [target.'cfg(target_os="windows")'.dependencies]
// windows = { version = "0.51", features = [
// "Win32_NetworkManagement_IpHelper",
// "Win32_NetworkManagement_Ndis",
// "Win32_Networking_WinSock",
// "Win32_Foundation",
// ] }
@ssrlive
ssrlive / mio-demo.rs
Last active September 8, 2023 08:02
Mio demo with rust
// [dependencies]
// mio = { version = "0.8", features = ["net", "os-ext", "os-poll"] }
use mio::{
event::Event,
net::{TcpListener, TcpStream},
Events, Interest, Poll, Token,
};
use std::{cell::RefCell, collections::HashMap, io::Read, net::SocketAddr, rc::Rc};
@ssrlive
ssrlive / node_tree.rs
Last active June 16, 2023 01:42
Demo of a Group/Node tree system in rust.
/*
[dependencies]
as-any = "0.3"
erased-serde = "0.3"
rustfmt = "0.10"
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1" }
uuid = { version = "1.2", features = ["v4", "serde"] }
*/
@ssrlive
ssrlive / guard_thread.rs
Last active May 4, 2023 04:29
Guard a thread and restart it when it exits unexpectedly.
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::{
signal::ctrl_c,
sync::mpsc::{channel, Sender},
task::spawn,
};
static STOP_FLAG: AtomicBool = AtomicBool::new(false);
// worker function to be executed in a separate thread
@ssrlive
ssrlive / callback4rust.md
Last active April 25, 2023 08:50
Rust FFI for C style callback
use std::{ffi::c_void, marker::PhantomData};

struct CBProxy<F, Sig>(PhantomData<(F, Sig)>);

impl<F, A1, A2, R> CBProxy<F, fn(A1, A2) -> R>
where
    F: FnMut(A1, A2) -> R,
{
#! /usr/bin/bash
# Please set the following parameters according to your environment
BYPASS_IP=123.45.67.89
PROXY_IP=127.0.0.1
PROXY_PORT=1080
PROXY_TYPE=SOCKS5
# check if user is root
function is_root() {
@ssrlive
ssrlive / m-x.sh
Created February 24, 2023 01:48
extern memory script
#!/bin/bash
sudo dd if=/dev/zero of=/opt/swap bs=1024 count=2048000
sudo chmod 600 /opt/swap # ×¢Òâ¸ü¸Ä swap ÎļþµÄȨÏÞ
sudo mkswap /opt/swap
sudo swapon /opt/swap
@ssrlive
ssrlive / bound2.rs
Last active March 6, 2023 15:48
lower_bound and upper_bound in rust.
use std::{cmp::Ordering, result::Result};
/// find first index where arr[idx] >= v; assume arr is sorted
pub trait LowerBound {
type Item;
fn lower_bound(&self, x: &Self::Item) -> Result<usize, usize>;
}
/// find first index where arr[idx] > v; assume arr is sorted
trait UpperBound {
@ssrlive
ssrlive / coin_change.rs
Last active February 13, 2023 04:10
最小紙幣找零算法
// 最小紙幣找零算法
// 1. 用最大的紙幣找零
// 2. 用次大的紙幣找零
fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let mut coins = coins.iter().map(|&x| x as usize).collect::<Vec<_>>();
coins.sort();
let amount = amount as usize;
let mut dp = vec![amount + 1; amount + 1];
dp[0] = 0;
@ssrlive
ssrlive / postgresql-macos.md
Last active February 4, 2023 02:52
Getting Started with PostgreSQL on macOS

Getting Started with PostgreSQL on macOS

goodgeek@mypc ~ % /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

goodgeek@mypc ~ % brew install postgresql

goodgeek@mypc ~ % brew services restart postgresql

goodgeek@mypc ~ % psql postgres