dcuddeback / libusb-rs

A safe Rust wrapper for libusb.
MIT License
199 stars 64 forks source link

Unable to store, or Box, a `DeviceHandle`. #32

Open stevenroose opened 5 years ago

stevenroose commented 5 years ago

I can't find a way to hold a DeviceHandle in a struct and pass it around. It has a lifetime that must be longer than the Context object that created it. However, even when Boxing the Context and the DeviceHandle together, it's impossible to store a handle.

Did anyone get this to work? I need to store the handle because of how I have a transport-agnostic library built. It also uses the hid crate where the Handle type does not have a lifetime defined, but just documents that it cannot outlive the HidManager type. So I can just manually drop the Handle before dropping the manager.

@dcuddeback I saw a comment where you assume all users will create the Context in main and then just keep it around. Well, I'm not an end user, but a library implementer. The library connects to devices that support with WebUSB, HID and an HTTP bridge daemon. So the Context is created inside the library code and it's impossible to create the context and have it outlive the interface client object that the library returns.

gdelazzari commented 3 years ago

I'm having the same issue, I need to return a struct from a function with a DeviceHandle inside. I'm fine with making the struct also hold another Context, specifically created for the struct instance, but I can't find a way to tell the borrow checker that what I'm trying to do is fine.

Did you happen to find a way without resorting to unsafe?

kevinmehall commented 3 years ago

Use rusb, which is a fork of this library that uses reference counting for its Context type, and is more actively maintained.