aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2019-09-26 08:37:31 -0400
committerBrian Cully <bjc@kublai.com>2019-09-26 08:37:31 -0400
commit0ff4f7c0076c937207f346d4593f99b47af04f86 (patch)
treeec191f16d92b50a5e55b4d0bfaea70ef955174fc
parentb272c2a2f65ffda7de9b0066d1438f33c7bd6f1f (diff)
downloadbootkbd-0ff4f7c0076c937207f346d4593f99b47af04f86.tar.gz
bootkbd-0ff4f7c0076c937207f346d4593f99b47af04f86.zip
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.
-rw-r--r--src/lib.rs14
1 files 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<Device>; MAX_DEVICES] = {
+ let mut devs: [MaybeUninit<Option<Device>>; 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 }
}
}