diff options
author | Brian Cully <bjc@kublai.com> | 2019-09-26 08:37:31 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2019-09-26 08:37:31 -0400 |
commit | 0ff4f7c0076c937207f346d4593f99b47af04f86 (patch) | |
tree | ec191f16d92b50a5e55b4d0bfaea70ef955174fc /src | |
parent | b272c2a2f65ffda7de9b0066d1438f33c7bd6f1f (diff) | |
download | bootkbd-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.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -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 } } } |