redb:嵌入式DB
use redb::{Database, Error, ReadableTable, TableDefinition};
const TABLE: TableDefinition<str, u64> = TableDefinition::new("my_data");
let main() -> Result<(), Error> {
let db = unsafe {Database::create("my_db.redb", 1024 * 1024)? };
let write_txn = db.begin_writer()?;
{
let mut talbe = write_txt.open_table(TABLE)?;
table.insert("my_key", &123)?;
}
write_txt.commit()?;
let read_txt = db.begin_read()?;
let table = read_txn.open_table(TABLE)?;
assert_eq!(table.get("my_key")?.unwrap(), 123);
Ok(())
}
+--------------------+--------+--------+--------+
| | redb | lmdb | sled |
+===============================================+
| bulk load | 1605ms | 1294ms | 4642ms |
|--------------------+--------+--------+--------|
| individual writes | 516ms | 411ms | 527ms |
|--------------------+--------+--------+--------|
| batch writes | 7444ms | 3938ms | 1465ms |
|--------------------+--------+--------+--------|
| large writes | 12s | 11s | 315s |
|--------------------+--------+--------+--------|
| random reads | 716ms | 649ms | 1552ms |
|--------------------+--------+--------+--------|
| removals | 1282ms | 1012ms | 1911ms |
+--------------------+--------+--------+--------+
Rust异步框架评测
-
async_std 和 smol 非常接近标准库,并在某些负载上更优。 -
Tokio 似乎受到 CPU-bound(Rust)异步任务的不利影响。 -
Tokio 在 localhost 引入 8µs、在网络上引入 10 µs 的延迟。
Passerine:小巧简洁可扩展的功能脚本语言
rust-apache-age:AGE连接器
use apache_age::{AgeClient, Client, NoTls, AgType};
use serde::{Deserialize, Serialize};
let mut client = Client::connect_age(
"host=localhost user=postgres password=passwd port=8081",
NoTls
).unwrap();
client.create_graph("my_apache_graph");
#[derive(Debug, Serialize, Deserialize, Clone)]
struct Person {
pub name: String,
pub surname: String,
}
match client.query_cypher::<()>(
"my_apache_graph",
"MATCH (n: Person) WHERE n.name = 'Alfred' RETURN {name: n.name, surname: n.surname}",
None,
) {
Ok(rows) => {
let x: AgType<Person> = rows[0].get(0);
// do whatever you need
}
Err(e) => {
// handle error
}
}
client.drop_graph("my_apache_graph");
ripsecrets:防止密钥提交
-
关注预提交:从一开始就防止密钥被提交比后面处理要容易的多。 -
速度极快:比其他工具快 95 倍。 -
始终本地操作:仅本地执行,永远不会发送数据。 -
误报率低:基于概率方法,比其他工具更准确。 -
没有依赖的单个二进制文件。
stevedore:Win下更好地使用的Docker
pydantic-core:pydantic验证
from pydantic_core import SchemaValidator, ValidationError
v = SchemaValidator({
'title': 'MyModel',
'type': 'model',
'fields': {
'name': {
'type': 'str',
},
'age': {
'type': 'int-constrained',
'ge': 18,
},
'is_developer': {
'type': 'bool',
'default': True,
},
},
})
print(v)
"""
SchemaValidator(title="MyModel", validator=ModelValidator ...
"""
r1 = v.validate_python({'name': 'Samuel', 'age': 35})
print(r1)
"""
(
{'name': 'Samuel', 'age': 35, 'is_developer': True}, <- validated data
{'age', 'name'} <- fields set
)
"""
# pydantic-core can also validate JSON directly
r2 = v.validate_json('{"name": "Samuel", "age": 35}')
assert r1 == r2
try:
v.validate_python({'name': 'Samuel', 'age': 11})
except ValidationError as e:
print(e)
"""
1 validation error for MyModel
age
Value must be greater than or equal to 18
[kind=int_greater_than_equal, context={ge: 18}, input_value=11, input_type=int]
"""
Rust平台crate
pg2rs:pg到rust源码
enums
和 structs
:mod structs;
use structs::{User};
let result = client.query("SELECT * FROM \"Users\"", &[]).await.unwrap();
let users: Vec<User> = result.into_iter().map(|row| User::from(row)).collect();
println!("{:#?}", users);
资源和教程
-
新手资源:https://apollolabsblog.hashnode.dev/35-rust-learning-resources-every-beginner-should-know-in-2022 -
Nim,Zig,Rust,C++编译时间评测:https://castillodel.github.io/compile-time-evaluation/ -
一个项目中引入两个不同版本的 crates:https://reltech.substack.com/p/including-two-versions-of-a-rust?s=r -
对 DynamoDB 全文检索:https://jakejscott.com/full-text-search-for-dynamodb-using-lambda-efs-tantivy-and-rust -
命令行参数解析工具 clap 使用:https://blog.logrocket.com/command-line-argument-parsing-rust-using-clap/
From 日报小组 长琴
-
Rustcc 论坛:支持 rss -
微信公众号:Rust 语言中文社区
文章评论