Skip to content

Commit e14dfd0

Browse files
committed
feat: add AsyncStreamDeck::from_sync to wrap an already-opened StreamDeck
Allows opening a device via the synchronous API on a caller-chosen thread and then handing it to the async wrapper. This matters on macOS, where hidapi's IOHIDManager is bound to the run loop of the thread that created the HidApi: enumerate/open must happen on that thread, but the existing AsyncStreamDeck::connect runs them via block_in_place on whatever tokio worker is current, which can crash (EXC_BREAKPOINT in CoreFoundation) when the device set changes, e.g. after sleep/wake. With from_sync, an application can own the HidApi on a dedicated thread, open devices there with StreamDeck::connect, and still use the async API for everything else.
1 parent f7f8eff commit e14dfd0

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/asynchronous.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ impl AsyncStreamDeck {
4646
device: Arc::new(Mutex::new(device)),
4747
})
4848
}
49+
50+
/// Wraps an already-opened synchronous [StreamDeck] without using
51+
/// [block_in_place], so the HID open call (which on macOS must run on the
52+
/// thread that owns the IOHIDManager run loop) can be performed on a
53+
/// dedicated thread and the result handed to the async runtime afterwards.
54+
pub fn from_sync(device: StreamDeck) -> AsyncStreamDeck {
55+
AsyncStreamDeck {
56+
kind: device.kind(),
57+
device: Arc::new(Mutex::new(device)),
58+
}
59+
}
4960
}
5061

5162
/// Instance methods of the struct

0 commit comments

Comments
 (0)