Rust FFI,回调和生存期


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!