Skip to content

Instantly share code, notes, and snippets.

View ZhangHanDong's full-sized avatar
🦀
Rustacean

Alex ZhangHanDong

🦀
Rustacean
  • Beijing, China
View GitHub Profile
@ZhangHanDong
ZhangHanDong / github2deepwiki.js
Created April 27, 2025 10:53
添加一个按钮,点击后将当前 GitHub 地址转换为 DeepWiki 地址并在新标签页打开
// GitHub 到 DeepWiki 按钮脚本
// 添加一个按钮,点击后将当前 GitHub 地址转换为 DeepWiki 地址并在新标签页打开
(() => {
// 创建并添加按钮到页面
const createButton = () => {
// 检查是否已存在按钮(避免重复添加)
if (document.getElementById('deepwiki-button')) {
return;
}
@ZhangHanDong
ZhangHanDong / temporary_lifetime_extension.rs
Created February 23, 2025 04:10 — forked from rust-play/playground.rs
Code shared from the Rust Playground
// 常量中的扩展
const C: &'static Vec<i32> = &Vec::new(); // Vec 的生命周期被扩展为 'static
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn temp_point() -> Point {
@ZhangHanDong
ZhangHanDong / layernorm.rs
Created April 9, 2024 06:59 — forked from rust-play/playground.rs
Code shared from the Rust Playground
use std::fs::File;
use std::io::prelude::*;
use std::mem;
fn layernorm_forward(output: &mut [f32], mean: &mut [f32], rstd: &mut [f32],
input: &[f32], weight: &[f32], bias: &[f32],
batch_size: usize, time_steps: usize, channels: usize) {
let epsilon = 1e-5;
for b in 0..batch_size {
for t in 0..time_steps {
@ZhangHanDong
ZhangHanDong / main.rs
Created February 4, 2024 10:31 — forked from kiyov09/main.rs
Rust house builder
#![allow(unused)]
use std::marker::PhantomData;
/// Our beloved house
#[derive(Debug)]
struct House {
floors: u32,
rooms: u32,
has_garage: bool,
}
@ZhangHanDong
ZhangHanDong / crate-health.md
Created January 2, 2023 02:10 — forked from repi/crate-health.md
Guidelines on evaluating health & quality of third-party crates at Embark

note: I wrote this for our internal documentation & guidelines at Embark so not all of it is likely relevant for other companies, but sharing here as others expressed interest in seeing it


What to evaluate and consider before adding usage of new third-party crates.

These are not exact requirements but questions to investigate and discuss to help reason around the health, safety, maintainability, and more around crates.

This can also be read as an opinionated guide for crate authors of what our (Embark's) guidelines and recommendations are, though should not be taken too literally.

@ZhangHanDong
ZhangHanDong / main.rs
Created November 24, 2022 02:03 — forked from jix/main.rs
Lifetime GAT emulation on stable rust
// This is a technique to emulate lifetime GATs (generic associated types) on stable rust starting
// with rustc 1.33.
//
// I haven't seen this exact technique before, but I would be surprised if no one else came up with
// it. I think this avoids most downsides of other lifetime GAT workarounds I've seen.
//
// In particular, neither implementing nor using traits with emulated lifetime GATs requires adding
// any helper items. Only defining the trait requires a single helper trait (+ a single helper impl
// for the 2nd variant) per GAT. This also makes the technique viable without any boilerplate
// reducing macros.
@ZhangHanDong
ZhangHanDong / jit.rs
Created November 2, 2022 15:16 — forked from CharlyCst/jit.rs
An almost legit Rust JIT compiler
mod jit {
use libc::{c_void, dlclose, dlopen, dlsym, RTLD_NOW};
use std::ffi::CString;
use std::fs::File;
use std::io::prelude::*;
use std::io::SeekFrom;
use std::process::Command;
const SOURCE_PATH: &'static str = "/tmp/jit.rs";
const LIB_PATH: &'static str = "/tmp/librsjit.so";
@ZhangHanDong
ZhangHanDong / cell-tests.rs
Created December 23, 2021 11:14 — forked from jonhoo/cell-tests.rs
cell-refcell-rc
// these aren't _quite_ functional tests,
// and should all be compile_fail,
// but may be illustrative
#[test]
fn concurrent_set() {
use std::sync::Arc;
let x = Arc::new(Cell::new(42));
let x1 = Arc::clone(&x);
std::thread::spawn(move || {
#![no_std]
#![no_main]
#![feature(asm)]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {}
}
#[no_mangle]
@ZhangHanDong
ZhangHanDong / TypeArithmetic.rs
Created March 19, 2021 14:39 — forked from ddfisher/TypeArithmetic.rs
Type-level Arithmetic in Rust
#![feature(core_intrinsics)]
// This requires unstable features to compile so we can print out the type names. The type
// arithmetic itself works fine on stable.
use std::marker::PhantomData;
enum Zero {}
enum Succ<T> {
Succ(PhantomData<T>), // only required to satisfy the compiler