From: Kostya Shishkov Date: Mon, 5 Apr 2021 10:27:36 +0000 (+0200) Subject: use MaybeUninit properly X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=fd8666bc8f48fd374571368cd4b9a0a42e3ea85b;p=nihav.git use MaybeUninit properly --- diff --git a/nihav-commonfmt/src/codecs/aac.rs b/nihav-commonfmt/src/codecs/aac.rs index 59cda62..8585420 100644 --- a/nihav-commonfmt/src/codecs/aac.rs +++ b/nihav-commonfmt/src/codecs/aac.rs @@ -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; 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; 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 } } } diff --git a/nihav-duck/src/codecs/vp3.rs b/nihav-duck/src/codecs/vp3.rs index 3507fdb..7b3411c 100644 --- a/nihav-duck/src/codecs/vp3.rs +++ b/nihav-duck/src/codecs/vp3.rs @@ -32,26 +32,29 @@ fn map_mbt(idx: usize) -> VPMBType { impl VP30Codes { fn new() -> Self { - let mut dc_cb: [Codebook; 5]; - let mut ac_i_cb: [Codebook; 5]; - let mut ac_p_cb: [Codebook; 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; 5]> = mem::MaybeUninit::uninit(); + let mut uac_i_cb: mem::MaybeUninit::<[Codebook; 5]> = mem::MaybeUninit::uninit(); + let mut uac_p_cb: mem::MaybeUninit::<[Codebook; 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; 16]; - let mut ac0_cb: [Codebook; 16]; - let mut ac1_cb: [Codebook; 16]; - let mut ac2_cb: [Codebook; 16]; - let mut ac3_cb: [Codebook; 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; 16]> = mem::MaybeUninit::uninit(); + let mut uac0_cb: mem::MaybeUninit::<[Codebook; 16]> = mem::MaybeUninit::uninit(); + let mut uac1_cb: mem::MaybeUninit::<[Codebook; 16]> = mem::MaybeUninit::uninit(); + let mut uac2_cb: mem::MaybeUninit::<[Codebook; 16]> = mem::MaybeUninit::uninit(); + let mut uac3_cb: mem::MaybeUninit::<[Codebook; 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; 16]; - let mut ac0_cb: [Codebook; 16]; - let mut ac1_cb: [Codebook; 16]; - let mut ac2_cb: [Codebook; 16]; - let mut ac3_cb: [Codebook; 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; 16]> = mem::MaybeUninit::uninit(); + let mut uac0_cb: mem::MaybeUninit::<[Codebook; 16]> = mem::MaybeUninit::uninit(); + let mut uac1_cb: mem::MaybeUninit::<[Codebook; 16]> = mem::MaybeUninit::uninit(); + let mut uac2_cb: mem::MaybeUninit::<[Codebook; 16]> = mem::MaybeUninit::uninit(); + let mut uac3_cb: mem::MaybeUninit::<[Codebook; 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; 7]; - let mut mv_y_cb: [Codebook; 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; 7]> = mem::MaybeUninit::uninit(); + let mut umv_y_cb: mem::MaybeUninit::<[Codebook; 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); diff --git a/nihav-indeo/src/codecs/imc.rs b/nihav-indeo/src/codecs/imc.rs index cb89b8e..f9a92c0 100644 --- a/nihav-indeo/src/codecs/imc.rs +++ b/nihav-indeo/src/codecs/imc.rs @@ -348,7 +348,6 @@ fn calc_maxcoef(coef: f32) -> (f32, f32) { impl IMCDecoder { fn new(is_imc: bool) -> Self { - let mut codes: [[Codebook; 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; 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(), diff --git a/nihav-rad/src/codecs/binkvid.rs b/nihav-rad/src/codecs/binkvid.rs index b0643b0..9c0dc9e 100644 --- a/nihav-rad/src/codecs/binkvid.rs +++ b/nihav-rad/src/codecs/binkvid.rs @@ -385,14 +385,14 @@ fn map_u8(idx: usize) -> u8 { idx as u8 } impl Default for BinkTrees { fn default() -> Self { - let mut cb: [Codebook; 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; 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 } } } diff --git a/nihav-realmedia/src/codecs/rv60codes.rs b/nihav-realmedia/src/codecs/rv60codes.rs index a830181..8effbb5 100644 --- a/nihav-realmedia/src/codecs/rv60codes.rs +++ b/nihav-realmedia/src/codecs/rv60codes.rs @@ -98,33 +98,37 @@ pub struct RV60Codebooks { impl RV60Codebooks { pub fn init() -> Self { - let mut cbp8_cb: [[Codebook; 4]; NUM_INTER_SETS]; - let mut cbp16_cb: [[Codebook; 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; 4]; NUM_INTER_SETS]> = mem::MaybeUninit::uninit(); + let mut ucbp16_cb: mem::MaybeUninit::<[[Codebook; 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 } }