sh1marin on Nostr: 就算用了 teloxide 两年,我还是想劝大部分的人放过自己,别用 ...
就算用了 teloxide 两年,我还是想劝大部分的人放过自己,别用 teloxide 写 bot。他们的 dptree 玩 abstraction 玩的太抽象了,出现任何 type system 的问题,真正错误全被隐藏在他们的 abstraction 后面,于是写 tg bot 变成了一个实验,一点点实验什么类型能满足他那个莫名其妙的 trait bound。
今天又撞了一个很傻逼的坑,报错不满足 Injectable<Input, Output, FnArgs> ,然后自己慢慢推断类型,基本上就是要求 input output 都是 Send + Sync 的,而我所有的 async 函数单测都正常工作的。自己在函数 signature 加了 Send Sync ‘static constraint 也完全没有问题,折腾了半个多小时也想不明白究竟是哪里让 teloxide 不开心了。
还好后来想到了能用 tokio::spawn 包一下,才终于发现根本原因:Rc<UnsafeCell<ReseedingRng<rand_chacha::chacha::ChaCha12Core, OsRng>>> cannot be sent between threads safely。在 async 函数里调用了 rand::thread_rng,又没有在 await 之前 drop 掉 ThreadRng,导致函数本身不 Send 了。而这个问题却会被 dptree 的顶层 abstraction trait bound 给藏起来。
Published at
2024-02-17 12:29:58Event JSON
{
"id": "c692be1629407526be58c2207ab39fc21dbbfac27d951d90569ba39371bd5be0",
"pubkey": "71ea5c9d9ef4f01c1ecb1a116f7d15ffb26a241620bf28c856593523a5e8edcb",
"created_at": 1708172998,
"kind": 1,
"tags": [
[
"proxy",
"https://dabr.ca/objects/f171843c-b64a-4ee5-a7ff-ccd6745e7998",
"activitypub"
]
],
"content": "就算用了 teloxide 两年,我还是想劝大部分的人放过自己,别用 teloxide 写 bot。他们的 dptree 玩 abstraction 玩的太抽象了,出现任何 type system 的问题,真正错误全被隐藏在他们的 abstraction 后面,于是写 tg bot 变成了一个实验,一点点实验什么类型能满足他那个莫名其妙的 trait bound。\n\n今天又撞了一个很傻逼的坑,报错不满足 Injectable\u003cInput, Output, FnArgs\u003e ,然后自己慢慢推断类型,基本上就是要求 input output 都是 Send + Sync 的,而我所有的 async 函数单测都正常工作的。自己在函数 signature 加了 Send Sync ‘static constraint 也完全没有问题,折腾了半个多小时也想不明白究竟是哪里让 teloxide 不开心了。\n\n还好后来想到了能用 tokio::spawn 包一下,才终于发现根本原因:Rc\u003cUnsafeCell\u003cReseedingRng\u003crand_chacha::chacha::ChaCha12Core, OsRng\u003e\u003e\u003e cannot be sent between threads safely。在 async 函数里调用了 rand::thread_rng,又没有在 await 之前 drop 掉 ThreadRng,导致函数本身不 Send 了。而这个问题却会被 dptree 的顶层 abstraction trait bound 给藏起来。",
"sig": "71f07c69eeca1184568646eaa0d180a1fc6625bd718c920c9dabbe9291420b5021a4cf451f8627bf4b1a4d82b5d8fe471111172591dc9a437d954a549ddc60a6"
}