From 0ff4f7c0076c937207f346d4593f99b47af04f86 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Thu, 26 Sep 2019 08:37:31 -0400 Subject: Use MaybeUninit for device list. This allows us to use `MAX_DEVICES` of any size and doesn't require Rust's limited array initialization hacks. --- src/lib.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 334eefd..63e53e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,10 +48,16 @@ where /// `address` is the address of the USB device which received the /// report and `buffer` is the contents of the report itself. pub fn new(callback: F) -> Self { - Self { - devices: [None; MAX_DEVICES], - callback, - } + let devices: [Option; MAX_DEVICES] = { + let mut devs: [MaybeUninit>; MAX_DEVICES] = + unsafe { mem::MaybeUninit::uninit().assume_init() }; + for dev in &mut devs[..] { + unsafe { ptr::write(dev.as_mut_ptr(), None) } + } + unsafe { mem::transmute(devs) } + }; + + Self { devices, callback } } } -- cgit v1.2.3