]> git.nihav.org Git - nihav.git/commitdiff
use MaybeUninit properly
authorKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 5 Apr 2021 10:27:36 +0000 (12:27 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Mon, 5 Apr 2021 10:27:36 +0000 (12:27 +0200)
nihav-commonfmt/src/codecs/aac.rs
nihav-duck/src/codecs/vp3.rs
nihav-indeo/src/codecs/imc.rs
nihav-rad/src/codecs/binkvid.rs
nihav-realmedia/src/codecs/rv60codes.rs

index 59cda6290b8166b219e32fc26fd52a30c83f2247..85854206ab3e3470b4fe175027a14c17cbb6cea1 100644 (file)
@@ -556,14 +556,14 @@ impl Codebooks {
     fn new() -> Self {
         let mut coderead = TableCodebookDescReader::new(AAC_SCF_CODEBOOK_CODES, AAC_SCF_CODEBOOK_BITS, scale_map);
         let scale_cb = Codebook::new(&mut coderead, CodebookMode::MSB).unwrap();
-        let mut spec_cb: [Codebook<u16>; 11];
-        unsafe {
-            spec_cb = mem::MaybeUninit::uninit().assume_init();
-            for i in 0..AAC_SPEC_CODES.len() {
-                let mut coderead = TableCodebookDescReader::new(AAC_SPEC_CODES[i], AAC_SPEC_BITS[i], cb_map);
-                ptr::write(&mut spec_cb[i], Codebook::new(&mut coderead, CodebookMode::MSB).unwrap());
-            }
-        }
+        let spec_cb = unsafe {
+                let mut uspec_cb: mem::MaybeUninit::<[Codebook<u16>; 11]> = mem::MaybeUninit::uninit();
+                for i in 0..AAC_SPEC_CODES.len() {
+                    let mut coderead = TableCodebookDescReader::new(AAC_SPEC_CODES[i], AAC_SPEC_BITS[i], cb_map);
+                    ptr::write(&mut (*uspec_cb.as_mut_ptr())[i], Codebook::new(&mut coderead, CodebookMode::MSB).unwrap());
+                }
+                uspec_cb.assume_init()
+            };
         Self { scale_cb, spec_cb }
     }
 }
index 3507fdb67d767c5f1928865ffb510b5d9c458af8..7b3411ccffb162c3cd9c806f332b11cd91cfdbe2 100644 (file)
@@ -32,26 +32,29 @@ fn map_mbt(idx: usize) -> VPMBType {
 
 impl VP30Codes {
     fn new() -> Self {
-        let mut dc_cb:   [Codebook<u8>; 5];
-        let mut ac_i_cb: [Codebook<u8>; 5];
-        let mut ac_p_cb: [Codebook<u8>; 5];
+        let dc_cb;
+        let ac_i_cb;
+        let ac_p_cb;
         let mut cr = TableCodebookDescReader::new(&VP30_MBTYPE_CODES, &VP30_MBTYPE_BITS, map_mbt);
         let mbtype_cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
         unsafe {
-            dc_cb = mem::MaybeUninit::uninit().assume_init();
-            ac_i_cb = mem::MaybeUninit::uninit().assume_init();
-            ac_p_cb = mem::MaybeUninit::uninit().assume_init();
+            let mut udc_cb: mem::MaybeUninit::<[Codebook<u8>; 5]> = mem::MaybeUninit::uninit();
+            let mut uac_i_cb: mem::MaybeUninit::<[Codebook<u8>; 5]> = mem::MaybeUninit::uninit();
+            let mut uac_p_cb: mem::MaybeUninit::<[Codebook<u8>; 5]> = mem::MaybeUninit::uninit();
             for i in 0..5 {
                 let mut cr = TableCodebookDescReader::new(&VP30_DC_CODES[i], &VP30_DC_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut dc_cb[i], cb);
+                ptr::write(&mut (*udc_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP30_AC_INTRA_CODES[i], &VP30_AC_INTRA_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac_i_cb[i], cb);
+                ptr::write(&mut (*uac_i_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP30_AC_INTER_CODES[i], &VP30_AC_INTER_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac_p_cb[i], cb);
+                ptr::write(&mut (*uac_p_cb.as_mut_ptr())[i], cb);
             }
+            dc_cb   = udc_cb.assume_init();
+            ac_i_cb = uac_i_cb.assume_init();
+            ac_p_cb = uac_p_cb.assume_init();
         }
         Self { dc_cb, ac_i_cb, ac_p_cb, mbtype_cb }
     }
@@ -67,68 +70,78 @@ struct VP31Codes {
 
 impl VP31Codes {
     fn new() -> Self {
-        let mut dc_cb: [Codebook<u8>; 16];
-        let mut ac0_cb: [Codebook<u8>; 16];
-        let mut ac1_cb: [Codebook<u8>; 16];
-        let mut ac2_cb: [Codebook<u8>; 16];
-        let mut ac3_cb: [Codebook<u8>; 16];
+        let dc_cb;
+        let ac0_cb;
+        let ac1_cb;
+        let ac2_cb;
+        let ac3_cb;
         unsafe {
-            dc_cb = mem::MaybeUninit::uninit().assume_init();
-            ac0_cb = mem::MaybeUninit::uninit().assume_init();
-            ac1_cb = mem::MaybeUninit::uninit().assume_init();
-            ac2_cb = mem::MaybeUninit::uninit().assume_init();
-            ac3_cb = mem::MaybeUninit::uninit().assume_init();
+            let mut udc_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac0_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac1_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac2_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac3_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
             for i in 0..16 {
                 let mut cr = TableCodebookDescReader::new(&VP31_DC_CODES[i], &VP31_DC_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut dc_cb[i], cb);
+                ptr::write(&mut (*udc_cb.as_mut_ptr())[i], cb);
 
                 let mut cr = TableCodebookDescReader::new(&VP31_AC_CAT0_CODES[i], &VP31_AC_CAT0_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac0_cb[i], cb);
+                ptr::write(&mut (*uac0_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP31_AC_CAT1_CODES[i], &VP31_AC_CAT1_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac1_cb[i], cb);
+                ptr::write(&mut (*uac1_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP31_AC_CAT2_CODES[i], &VP31_AC_CAT2_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac2_cb[i], cb);
+                ptr::write(&mut (*uac2_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP31_AC_CAT3_CODES[i], &VP31_AC_CAT3_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac3_cb[i], cb);
+                ptr::write(&mut (*uac3_cb.as_mut_ptr())[i], cb);
             }
+            dc_cb = udc_cb.assume_init();
+            ac0_cb = uac0_cb.assume_init();
+            ac1_cb = uac1_cb.assume_init();
+            ac2_cb = uac2_cb.assume_init();
+            ac3_cb = uac3_cb.assume_init();
         }
         Self { dc_cb, ac0_cb, ac1_cb, ac2_cb, ac3_cb }
     }
     fn new_vp4() -> VP31Codes {
-        let mut dc_cb: [Codebook<u8>; 16];
-        let mut ac0_cb: [Codebook<u8>; 16];
-        let mut ac1_cb: [Codebook<u8>; 16];
-        let mut ac2_cb: [Codebook<u8>; 16];
-        let mut ac3_cb: [Codebook<u8>; 16];
+        let dc_cb;
+        let ac0_cb;
+        let ac1_cb;
+        let ac2_cb;
+        let ac3_cb;
         unsafe {
-            dc_cb = mem::MaybeUninit::uninit().assume_init();
-            ac0_cb = mem::MaybeUninit::uninit().assume_init();
-            ac1_cb = mem::MaybeUninit::uninit().assume_init();
-            ac2_cb = mem::MaybeUninit::uninit().assume_init();
-            ac3_cb = mem::MaybeUninit::uninit().assume_init();
+            let mut udc_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac0_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac1_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac2_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
+            let mut uac3_cb: mem::MaybeUninit::<[Codebook<u8>; 16]> = mem::MaybeUninit::uninit();
             for i in 0..16 {
                 let mut cr = TableCodebookDescReader::new(&VP40_DC_CODES[i], &VP40_DC_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut dc_cb[i], cb);
+                ptr::write(&mut (*udc_cb.as_mut_ptr())[i], cb);
 
                 let mut cr = TableCodebookDescReader::new(&VP40_AC_CAT0_CODES[i], &VP40_AC_CAT0_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac0_cb[i], cb);
+                ptr::write(&mut (*uac0_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP40_AC_CAT1_CODES[i], &VP40_AC_CAT1_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac1_cb[i], cb);
+                ptr::write(&mut (*uac1_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP40_AC_CAT2_CODES[i], &VP40_AC_CAT2_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac2_cb[i], cb);
+                ptr::write(&mut (*uac2_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP40_AC_CAT3_CODES[i], &VP40_AC_CAT3_BITS[i], map_idx);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut ac3_cb[i], cb);
+                ptr::write(&mut (*uac3_cb.as_mut_ptr())[i], cb);
             }
+            dc_cb = udc_cb.assume_init();
+            ac0_cb = uac0_cb.assume_init();
+            ac1_cb = uac1_cb.assume_init();
+            ac2_cb = uac2_cb.assume_init();
+            ac3_cb = uac3_cb.assume_init();
         }
         VP31Codes { dc_cb, ac0_cb, ac1_cb, ac2_cb, ac3_cb }
     }
@@ -146,19 +159,21 @@ fn map_mv(idx: usize) -> i8 {
 
 impl VP40AuxCodes {
     fn new() -> Self {
-        let mut mv_x_cb: [Codebook<i8>; 7];
-        let mut mv_y_cb: [Codebook<i8>; 7];
+        let mv_x_cb;
+        let mv_y_cb;
         unsafe {
-            mv_x_cb = mem::MaybeUninit::uninit().assume_init();
-            mv_y_cb = mem::MaybeUninit::uninit().assume_init();
+            let mut umv_x_cb: mem::MaybeUninit::<[Codebook<i8>; 7]> = mem::MaybeUninit::uninit();
+            let mut umv_y_cb: mem::MaybeUninit::<[Codebook<i8>; 7]> = mem::MaybeUninit::uninit();
             for i in 0..7 {
                 let mut cr = TableCodebookDescReader::new(&VP40_MV_X_CODES[i], &VP40_MV_X_BITS[i], map_mv);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut mv_x_cb[i], cb);
+                ptr::write(&mut (*umv_x_cb.as_mut_ptr())[i], cb);
                 let mut cr = TableCodebookDescReader::new(&VP40_MV_Y_CODES[i], &VP40_MV_Y_BITS[i], map_mv);
                 let cb = Codebook::new(&mut cr, CodebookMode::MSB).unwrap();
-                ptr::write(&mut mv_y_cb[i], cb);
+                ptr::write(&mut (*umv_y_cb.as_mut_ptr())[i], cb);
             }
+            mv_x_cb = umv_x_cb.assume_init();
+            mv_y_cb = umv_y_cb.assume_init();
         }
         let mut cr0 = TableCodebookDescReader::new(&VP40_MBPAT_CODES[0], &VP40_MBPAT_BITS[0], map_idx);
         let mut cr1 = TableCodebookDescReader::new(&VP40_MBPAT_CODES[1], &VP40_MBPAT_BITS[1], map_idx);
index cb89b8ee403d198460080715a0f5c204aa411679..f9a92c0595c7918183c7e42ee9cc55667862a65a 100644 (file)
@@ -348,7 +348,6 @@ fn calc_maxcoef(coef: f32) -> (f32, f32) {
 
 impl IMCDecoder {
     fn new(is_imc: bool) -> Self {
-        let mut codes: [[Codebook<u8>; 4]; 4];
         let mut cycle1: [usize; BANDS] = [0; BANDS];
         let mut cycle2: [usize; BANDS] = [0; BANDS];
         let mut weights1: [f32; BANDS-1] = [0.0; BANDS-1];
@@ -359,15 +358,16 @@ impl IMCDecoder {
             weights1.copy_from_slice(&IMC_WEIGHTS1);
             weights2.copy_from_slice(&IMC_WEIGHTS2);
         }
-        unsafe {
-            codes = mem::MaybeUninit::uninit().assume_init();
-            for i in 0..4 {
-                for j in 0..4 {
-                    let mut cr = IMCCodeReader::new(i, j);
-                    ptr::write(&mut codes[i][j], Codebook::new(&mut cr, CodebookMode::MSB).unwrap());
+        let codes = unsafe {
+                let mut ucodes: mem::MaybeUninit::<[[Codebook<u8>; 4]; 4]> = mem::MaybeUninit::uninit();
+                for i in 0..4 {
+                    for j in 0..4 {
+                        let mut cr = IMCCodeReader::new(i, j);
+                        ptr::write(&mut (*ucodes.as_mut_ptr())[i][j], Codebook::new(&mut cr, CodebookMode::MSB).unwrap());
+                    }
                 }
-            }
-        }
+                ucodes.assume_init()
+            };
         IMCDecoder {
             is_imc,
             chmap:      NAChannelMap::new(),
index b0643b0e9655ae3a25a9d7c1d4b1441b56ec08cb..9c0dc9e37dd976780c48263dd091377b89794767 100644 (file)
@@ -385,14 +385,14 @@ fn map_u8(idx: usize) -> u8 { idx as u8 }
 
 impl Default for BinkTrees {
     fn default() -> Self {
-        let mut cb: [Codebook<u8>; 16];
-        unsafe {
-            cb = std::mem::MaybeUninit::uninit().assume_init();
-            for i in 0..16 {
-                let mut cr = TableCodebookDescReader::new(&BINK_TREE_CODES[i], &BINK_TREE_BITS[i], map_u8);
-                std::ptr::write(&mut cb[i], Codebook::new(&mut cr, CodebookMode::LSB).unwrap());
-            }
-        }
+        let cb = unsafe {
+                let mut ucb: std::mem::MaybeUninit::<[Codebook<u8>; 16]> = std::mem::MaybeUninit::uninit();
+                for i in 0..16 {
+                    let mut cr = TableCodebookDescReader::new(&BINK_TREE_CODES[i], &BINK_TREE_BITS[i], map_u8);
+                    std::ptr::write(&mut (*ucb.as_mut_ptr())[i], Codebook::new(&mut cr, CodebookMode::LSB).unwrap());
+                }
+                ucb.assume_init()
+            };
         Self { cb }
     }
 }
index a8301813fd09a9ae519c47bcbd8d6cee8f9e1f9e..8effbb57daf8af616324a2c277fca799c38238fc 100644 (file)
@@ -98,33 +98,37 @@ pub struct RV60Codebooks {
 
 impl RV60Codebooks {
     pub fn init() -> Self {
-        let mut cbp8_cb:  [[Codebook<u16>; 4];  NUM_INTER_SETS];
-        let mut cbp16_cb: [[Codebook<u16>; 12]; NUM_INTER_SETS];
+        let cbp8_cb;
+        let cbp16_cb;
         unsafe {
-            cbp8_cb = mem::MaybeUninit::uninit().assume_init();
-            cbp16_cb = mem::MaybeUninit::uninit().assume_init();
+            let mut ucbp8_cb:  mem::MaybeUninit::<[[Codebook<u16>; 4];  NUM_INTER_SETS]> = mem::MaybeUninit::uninit();
+            let mut ucbp16_cb: mem::MaybeUninit::<[[Codebook<u16>; 12]; NUM_INTER_SETS]> = mem::MaybeUninit::uninit();
             for set_no in 0..NUM_INTER_SETS {
                 for i in 0..4 {
                     let mut cbr = RV60CodebookDescReader::new(&RV60_CBP8_TABS[set_no][i], NUM_CBP_ENTRIES, false);
-                    ptr::write(&mut cbp8_cb[set_no][i], Codebook::new(&mut cbr, CodebookMode::MSB).unwrap());
+                    ptr::write(&mut (*ucbp8_cb.as_mut_ptr())[set_no][i], Codebook::new(&mut cbr, CodebookMode::MSB).unwrap());
                 }
                 for i in 0..12 {
                     let mut cbr = RV60CodebookDescReader::new(&RV60_CBP16_TABS[set_no][i], NUM_CBP_ENTRIES, false);
-                    ptr::write(&mut cbp16_cb[set_no][i], Codebook::new(&mut cbr, CodebookMode::MSB).unwrap());
+                    ptr::write(&mut (*ucbp16_cb.as_mut_ptr())[set_no][i], Codebook::new(&mut cbr, CodebookMode::MSB).unwrap());
                 }
             }
+            cbp8_cb  = ucbp8_cb.assume_init();
+            cbp16_cb = ucbp16_cb.assume_init();
         }
-        let mut intra_coeff_cb: [CoeffCodebooks; NUM_INTRA_SETS];
-        let mut inter_coeff_cb: [CoeffCodebooks; NUM_INTER_SETS];
+        let intra_coeff_cb;
+        let inter_coeff_cb;
         unsafe {
-            intra_coeff_cb = mem::MaybeUninit::uninit().assume_init();
+            let mut uintra_coeff_cb: mem::MaybeUninit::<[CoeffCodebooks; NUM_INTRA_SETS]> = mem::MaybeUninit::uninit();
             for set_no in 0..NUM_INTRA_SETS {
-                ptr::write(&mut intra_coeff_cb[set_no], CoeffCodebooks::init(set_no, true));
+                ptr::write(&mut (*uintra_coeff_cb.as_mut_ptr())[set_no], CoeffCodebooks::init(set_no, true));
             }
-            inter_coeff_cb = mem::MaybeUninit::uninit().assume_init();
+            let mut uinter_coeff_cb: mem::MaybeUninit::<[CoeffCodebooks; NUM_INTER_SETS]> = mem::MaybeUninit::uninit();
             for set_no in 0..NUM_INTER_SETS {
-                ptr::write(&mut inter_coeff_cb[set_no], CoeffCodebooks::init(set_no, false));
+                ptr::write(&mut (*uinter_coeff_cb.as_mut_ptr())[set_no], CoeffCodebooks::init(set_no, false));
             }
+            intra_coeff_cb = uintra_coeff_cb.assume_init();
+            inter_coeff_cb = uinter_coeff_cb.assume_init();
         }
         Self { cbp8_cb, cbp16_cb, intra_coeff_cb, inter_coeff_cb }
     }