Skip to content

Commit 7b810a4

Browse files
committed
Adopt Box::new_uninit and Box::assume_init
As promised, now that our MSRV is greater than 1.82.
1 parent 53ea632 commit 7b810a4

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/yaml/chunker/parser.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! [libyaml]: https://pyyaml.org/wiki/LibYAML
99
1010
use std::error::Error;
11-
use std::ffi::{CStr, c_char, c_void};
11+
use std::ffi::{c_char, c_void, CStr};
1212
use std::fmt::Display;
1313
use std::io::{self, Read};
1414
use std::mem::MaybeUninit;
@@ -44,21 +44,16 @@ where
4444
R: Read,
4545
{
4646
pub(super) fn new(reader: R) -> Parser<R> {
47-
let mut parser: Box<yaml_parser_t>;
48-
4947
// SAFETY: This comes from libyaml, which we assume is implemented
5048
// correctly. We expect initialization functions to properly handle
5149
// uninitialized memory; empirical tests in Miri show this to be true.
52-
unsafe {
53-
// TODO: Use Box::new_uninit and Box::assume_init if our MSRV hits
54-
// or exceeds 1.82.
55-
let mut uninit = Box::new(MaybeUninit::<yaml_parser_t>::uninit());
56-
if yaml_parser_initialize(uninit.as_mut_ptr()).ok {
57-
parser = Box::from_raw(Box::into_raw(uninit).cast());
58-
} else {
50+
let mut parser = unsafe {
51+
let mut uninit = Box::new_uninit();
52+
if yaml_parser_initialize(uninit.as_mut_ptr()).fail {
5953
panic!("out of memory for yaml_parser_initialize");
6054
}
61-
}
55+
uninit.assume_init()
56+
};
6257

6358
// libyaml needs a raw ReadState<R> pointer for Self::read_handler.
6459
// Since mutable access through a Box invalidates derived pointers,

0 commit comments

Comments
 (0)