Skip to content

Commit 376f4c6

Browse files
committed
Use MaybeUninit when available
Fix deprecation warnings for mem::uninitialized. (They also affected other crates, since it’s used in a public macro.)
1 parent 5bc93b5 commit 376f4c6

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cssparser"
3-
version = "0.25.7"
3+
version = "0.25.8"
44
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
55

66
description = "Rust implementation of CSS Syntax Level 3"
@@ -30,6 +30,7 @@ serde = {version = "1.0", optional = true}
3030
smallvec = "0.6"
3131

3232
[build-dependencies]
33+
autocfg = "0.1.4"
3334
syn = { version = "0.15.12", features = ["extra-traits", "fold", "full"] }
3435
quote = "0.6"
3536
proc-macro2 = "0.4"

build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
extern crate autocfg;
56
#[macro_use]
67
extern crate quote;
78
#[macro_use]
@@ -49,5 +50,7 @@ fn main() {
4950
println!("cargo:rustc-cfg=rustc_has_pr45225")
5051
}
5152

53+
autocfg::new().emit_has_path("std::mem::MaybeUninit");
54+
5255
codegen::main();
5356
}

src/macros.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,26 @@ macro_rules! ascii_case_insensitive_phf_map {
110110
#[doc(hidden)]
111111
macro_rules! cssparser_internal__to_lowercase {
112112
($input: expr, $BUFFER_SIZE: expr => $output: ident) => {
113-
// mem::uninitialized() is ok because `buffer` is only used in `_internal__to_lowercase`,
113+
let mut buffer;
114+
// Safety: `buffer` is only used in `_internal__to_lowercase`,
114115
// which initializes with `copy_from_slice` the part of the buffer it uses,
115116
// before it uses it.
116117
#[allow(unsafe_code)]
117-
let mut buffer: [u8; $BUFFER_SIZE] = unsafe { ::std::mem::uninitialized() };
118+
let buffer = unsafe {
119+
// FIXME: remove this when we require Rust 1.36
120+
#[cfg(not(has_std__mem__MaybeUninit))]
121+
{
122+
buffer = ::std::mem::uninitialized::<[u8; $BUFFER_SIZE]>();
123+
&mut buffer
124+
}
125+
#[cfg(has_std__mem__MaybeUninit)]
126+
{
127+
buffer = ::std::mem::MaybeUninit::<[u8; $BUFFER_SIZE]>::uninit();
128+
&mut *(buffer.as_mut_ptr())
129+
}
130+
};
118131
let input: &str = $input;
119-
let $output = $crate::_internal__to_lowercase(&mut buffer, input);
132+
let $output = $crate::_internal__to_lowercase(buffer, input);
120133
};
121134
}
122135

0 commit comments

Comments
 (0)