libusb-rs icon indicating copy to clipboard operation
libusb-rs copied to clipboard

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

Open stevenroose opened this issue 5 years ago • 2 comments

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.

stevenroose avatar May 20 '19 13:05 stevenroose

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?

gdelazzari avatar Aug 30 '20 18:08 gdelazzari

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

kevinmehall avatar Aug 31 '20 03:08 kevinmehall