Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upHolding an InterfaceHandle in a struct #3
Comments
This comment has been minimized.
This comment has been minimized.
|
@awelkie Thanks for opening this issue. I wasn't aware that Rust's limitations would prevent storing a struct UsbDevice<'a> {
device:libusb::DeviceHandle<'a>,
interfaces: HashMap<u8,libusb::InterfaceHandle<'a>>,
}
impl<'a> UsbDevice<'a> {
fn interface(&mut self, iface: u8) -> ::libusb::UsbResult<&mut libusb::InterfaceHandle> {
// hand out references from `self.interfaces`
}
}but I run into lifetime issues every time I try to get something working, even when working with I think this (among other issues) needs to be overhauled for v0.2. A library shouldn't be this cumbersome to use. The only reason for each struct to hold references to the other structs was to have the Rust compiler enforce order of releasing resources ( |
This comment has been minimized.
This comment has been minimized.
|
Maybe the |
This comment has been minimized.
This comment has been minimized.
|
Actually, I think a simpler solution might be to use a 'PhantomData' objects in the structs instead of unused references to the parent struct. For example, the This way, objects have the right lifetimes but don't have references that keep them from being in the same struct. |
This comment has been minimized.
This comment has been minimized.
|
@awelkie I had never seen Since the libusb types are (internally) equivalent to |
This comment has been minimized.
This comment has been minimized.
|
@awelkie When I dug into it, I found that You can try it out now on the As an aside, I also replaced all of the references with |
This comment has been minimized.
This comment has been minimized.
|
Awesome, thanks for making these changes. I'm traveling now, so I won't be
|
dcuddeback
closed this
in
3c3a972
Oct 18, 2015
This comment has been minimized.
This comment has been minimized.
|
So this fixed the issue with holding an 'InterfaceHandle' and a 'DeviceHandle' in the same struct. But I'm still running into the same issue with holding a 'Context' and 'DeviceHandle' in the same struct. What do you think about having the |
This comment has been minimized.
This comment has been minimized.
|
What's your use case for holding a |
This comment has been minimized.
This comment has been minimized.
|
The issue I was having was that I needed to initialize the context and device within a C callback. So I was hoping to bundle the context and device together and return it from the callback. But I solved the issue by using initializing the context in thread local storage. So I'm all good as far as this issue is concerned. Thanks for the changes! |
This comment has been minimized.
This comment has been minimized.
meh
commented
Mar 24, 2016
|
I'm having some trouble with this myself, I'm working on a library to deal with Steam Controller and I'd like to store the This is a problem because Does |
This comment has been minimized.
This comment has been minimized.
|
See #5, where I fixed that. |
yuvadm
added a commit
to yuvadm/rustl-sdr
that referenced
this issue
Jul 26, 2018
This comment has been minimized.
This comment has been minimized.
yuvadm
commented
Jul 26, 2018
|
I'm attempting something similar to what @meh is describing. I'd like to create a library that uses The only real workaround is as @dcuddeback mentioned and that's to create the Is there any better way to do this? |
This comment has been minimized.
This comment has been minimized.
oberien
commented
Jul 26, 2018
|
What I did in my program was to move the Context into a Box and leak that box, giving me a |
awelkie commentedOct 7, 2015
I'd like to have a struct that contains an
InterfaceHandleobjects so that I can use thebulk_transfermethod. I'm having trouble doing this because anInterfaceHandleobject contains a reference to aDeviceHandleobject, which contains a reference to aContextobject. I'd be fine having theContextobject be global, but I'd like to have theDeviceHandleandInterfaceHandleobjects held in the same struct. But Rust has trouble with structs that contain references to objects within itself.So what's the best approach for having a struct contain an
InterfaceHandle? I can see a few options:RcandRefCellas is suggested by reem in this postDeviceHandle, and then useclaim_interfaceto get theInterfaceHandleevery time I need it.libusb-syscrate directly, and just carry around the pointers.Here's an example of what I'm trying to do:
This fails to compile, saying that
dev_handledoes not live long enough.