我正在尝试围绕libuv(一个用C编写的事件循环库)构建一个不错的生锈包装器。我几乎“完成了”,但是在回调和生存期方面遇到了一些麻烦。
Being an event loop library, libuv relies heavily on callbacks. I have some code that can accept functions, closures, or a tuple of (obj, method-on-obj)
and it handles creating an appropriate "trampoline" to get that across the FFI boundary. That all works.
However, the problem I'm running into is that I cannot figure out what to do about lifetimes. At this point, I'm requiring that these functions/closures/objs have static lifetimes, but that creates a ton of headaches when it comes to actually using my library: any data accessed by these functions/closures/objs must also be static, then. In simple cases, a move
on a closure can "fix" the problem, but any real-world use case is going to very quickly become cumbersome - for example, creating a static object in rust is difficult, so the (obj, method-on-obj)
callback type isn't very useful - but I'd really like it to be!
不过,我真的想不出任何其他解决方案。回调及其数据必须存在,直到通过适当的libuv函数停止,取消或关闭它们为止-或直到循环结束。我不知道是否有可能通过FFI边界传递生命周期,但是即使是这样,我也很确定rust的分析会得出结论,生命周期本质上是静态的-我无法想象有什么方法可以静态地由于它是在运行时确定的,因此可以通过代码分析得出任何较短的生命周期。
因此,我想知道是否有人有什么想法可以使我的库更容易使用,要么以某种方式降低使用期限,要么使处理所有静态内容变得更轻松,更省力。任何帮助表示赞赏!