typeof for Rust

typeof for C c语言的typeof的应用: 1 2 3 #define SWAP(a, b) do { \ typeof(a) tmp = a; b = a; a = tmp; \ } while(0) 1 2 3 4 5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) typeof for rust 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 use std::any::type_name; fn type_of<T>(_: T) -> &'static str { type_name::<T>() } fn main() { let x = 21; let y = 2.5; println!("{}", type_of(&y)); println!("{}", type_of(x)); println!("{}", type_of("abc")); println!("{}", type_of(String::from("abc"))); println!("{}", type_of(vec![1, 2, 3])); println!("{}", type_of(main)); println!("{}", type_of(&type_of::<i32>)); }

December 8, 2021

linux btop 101

背景 最近发现一个很好用的系统监控命令:btop。 btop一站式实时显示cpu,memory,disk,network,processes的统计信息。 ...

September 25, 2021

tips for learning rust

资料 The Rust Programming Language Rust Language Cheat Sheet Zero to Production in Rust This Week in Rust awesome-rust awesome-cli-rust tools and libaries cargo-asm A cargo subcommand that displays the generated assembly of Rust source code. cargo-fuzz A cargo subcommand for fuzzing with libFuzzer hyperfine A command-line benchmarking tool flamegraph sqlx yarte opentelemetry thiserror rust-clippy Serde Tokio Rocketi Rocket Diesel Rayon Actix Hyper Anyhow Thiserror PyO3 Rkyv 心法 保持耐心,不要心急,毕竟rust学习曲线陡峭 编译器是最好的老师,同时记住编译器永远是正确的 克服传统语言的思维定势,建立新思维模型如资源与变量分离,同时尝试整合范型编程,类型编程,函数式编程,异步编程,meta-programming 从简单开始,从基础开始 深入从rust内存管理原理开始,扩展到多线程与异步环境 学以致用,可以用rust做一些个人的项目

September 8, 2021

writing a tree in rust

define the tree structure 1 2 3 4 5 6 #[derive(PartialEq)] struct TreeNode<T> { pub value: Option<T>, pub children: Vec<Rc<RefCell<TreeNode<T>>>>, pub parent: Option<Rc<RefCell<TreeNode<T>>>>, } new tree 1 2 3 4 5 6 7 pub fn new() -> TreeNode<T> { TreeNode { value: None, children: vec![], parent: None, } } add_child 1 2 3 pub fn add_child(&mut self, new_node: Rc<RefCell<TreeNode<T>>>) { self.children.push(new_node); } del_child 1 2 3 4 pub fn del_child(&mut self, node_to_remove: Rc<RefCell<TreeNode<T>>>) { self.children .retain(|child| !Rc::ptr_eq(child, &node_to_remove)); } print 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 pub fn print(&self) -> String where T: ToString, { if let Some(ref value) = self.value { value.to_string() } else { String::from("[") + &self .children .iter() .map(|tn| tn.borrow().print()) .collect::<Vec<String>>() .join(",") + "]" } } code The code as follows: ...

September 7, 2021

uniswap的非永久性损失公式的推导

说明 公式推导基于uniswap v2,不适用于uniswap v3。 非永久性损失 用户为流动性池提供流动性时,由于交易对价格变化使池内的交易对的代币数量发生变化,相比较于不参加提供流动性的情况下,这种变化会导致用户资产有一定的损失,而这种损失被称为非永久性损失(国内很多人将其称为无常损失)。 ...

August 16, 2021

Error: ENS is not supported on network private

Error 1 2 3 4 5 6 7 \node_modules\web3-eth-ens\lib\ENS.js:482 throw new Error("ENS is not supported on network " + networkType); ^ Error: ENS is not supported on network private at ENS.checkNetwork (\node_modules\web3-eth-ens\lib\ENS.js:482:19) at processTicksAndRejections (node:internal/process/task_queues:96:5) 原因 将web3实例作为参数,误传给函数,导致web3实例对应的内存被踩,后面使用该web3实例导致出现上面的问题。 ...

July 8, 2021

常用Defi工具与网站

DeBank DeBank 是一款DeFi资产跟踪和投资组合管理工具。 优点: 支持多链 不仅支持链上帐户资产也支持TVL资产 支持多种协议 支持帐号approve风险提示 缺点: 新协议支持速度慢 vFat Tools vFat Tools为defi农民提供挖矿分析,收益计算,数据面板等服务。 ...

July 8, 2021

应对复杂性的原则

原则 不追求完美 解决核心问题 跳出框架,摆脱限制 从自身,事实,简单开始,多迭代

July 3, 2021

说说519事件

背景 加密货币历史上几次大跌: 2017年94事件 2020年312事件 2021年423事件 2021年519事件 每次大跌都验证一句话:币圈一天,人间一年。 ...

May 21, 2021

PoS更有利于去中心化

背景 PoW和PoS作为现在最常用的两大共识算法,PoW与PoS之争已经有多年了。 下面从去中心化角度来对比两条公链: 采用PoW的以太坊 采用EPoS的Harmony 以太坊 随着以太坊矿池兴起加上PoW天生容易导致算力集中,以太坊独立挖矿的节点越来越少,出块节点也越来越少,越来越集中,下图就是一个很好的说明。 ...

May 1, 2021