Open
Description
UsbDevice::poll
currently has a nasty warning:
/// Note: The list of classes passed in must be the same for every call while the device is
/// configured, or the device may enumerate incorrectly or otherwise misbehave. The easiest way
/// to do this is to call the `poll` method in only one place in your code, as follows:
Things will break in weird and wonderful ways if the user doesn't always pass in the same slice of classes in the same order.
What I would really want is for UsbDevice to hold references to the list of classes. However this is a problem because both UsbDevice and the user would want to have a &mut
to each class. Earlier versions used immutable references but this meant all classes had to be written using interior mutability which was nasty.
Possible solutions:
- Use RefCells. This means the user has to borrow_mut every time they want to use their serial port or whatever though. Not ideal.
- Have UsbDevice own the classes instead. They're mixed types but a tuple might work. Then the user could ask UsbDevice for a temporary
&mut
to their class to talk to them. If compile time device/descriptor generation is implemented this could possibly be quite ergonomic. However the ability to access multiple classes in parallel might be lost, which is currently possible (reading/writing from multiple endpoints at the same time with DMA could be a thing in the future) - Use something similar to smoltcp where they have a SocketSet type you pass to poll, and instead of the actual sockets you get socket handles you can then request access to via the SocketSet. This is probably even more convoluted to use than the RefCells though. (Example)
- Live with it.