Skip to content

Unconditionally use MaybeUninit #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ rust:
- nightly
- beta
- stable
- 1.31.0
- 1.36.0

script:
- cargo build --verbose
Expand Down
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.25.9"
version = "0.26.0"
authors = [ "Simon Sapin <[email protected]>" ]

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

[build-dependencies]
autocfg = "0.1.4"
syn = { version = "1", features = ["extra-traits", "fold", "full"] }
quote = "1"
proc-macro2 = "1"
Expand Down
3 changes: 0 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate autocfg;
#[macro_use]
extern crate quote;
#[macro_use]
Expand Down Expand Up @@ -50,7 +49,5 @@ fn main() {
println!("cargo:rustc-cfg=rustc_has_pr45225")
}

autocfg::new().emit_has_path("std::mem::MaybeUninit");

codegen::main();
}
64 changes: 27 additions & 37 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::mem::MaybeUninit;

// See docs of the `procedural-masquerade` crate.
define_invoke_proc_macro!(cssparser_internal__invoke_proc_macro);

Expand Down Expand Up @@ -110,42 +112,16 @@ macro_rules! ascii_case_insensitive_phf_map {
#[doc(hidden)]
macro_rules! cssparser_internal__to_lowercase {
($input: expr, $BUFFER_SIZE: expr => $output: ident) => {
let mut buffer;
// Safety: `buffer` is only used in `_internal__to_lowercase`,
// which initializes with `copy_from_slice` the part of the buffer it uses,
// before it uses it.
#[allow(unsafe_code)]
let buffer = unsafe { cssparser_internal__uninit!(buffer, $BUFFER_SIZE) };
let mut buffer = unsafe {
::std::mem::MaybeUninit::<[::std::mem::MaybeUninit<u8>; $BUFFER_SIZE]>::uninit()
.assume_init()
};
let input: &str = $input;
let $output = $crate::_internal__to_lowercase(buffer, input);
let $output = $crate::_internal__to_lowercase(&mut buffer, input);
};
}

#[cfg(has_std__mem__MaybeUninit)]
#[macro_export]
#[doc(hidden)]
macro_rules! cssparser_internal__uninit {
($buffer: ident, $BUFFER_SIZE: expr) => {
{
$buffer = ::std::mem::MaybeUninit::<[u8; $BUFFER_SIZE]>::uninit();
&mut *($buffer.as_mut_ptr())
}
}
}

// FIXME: remove this when we require Rust 1.36
#[cfg(not(has_std__mem__MaybeUninit))]
#[macro_export]
#[doc(hidden)]
macro_rules! cssparser_internal__uninit {
($buffer: ident, $BUFFER_SIZE: expr) => {
{
$buffer = ::std::mem::uninitialized::<[u8; $BUFFER_SIZE]>();
&mut $buffer
}
}
}

/// Implementation detail of match_ignore_ascii_case! and ascii_case_insensitive_phf_map! macros.
///
/// **This function is not part of the public API. It can change or be removed between any verisons.**
Expand All @@ -154,14 +130,28 @@ macro_rules! cssparser_internal__uninit {
/// Otherwise, return `input` ASCII-lowercased, using `buffer` as temporary space if necessary.
#[doc(hidden)]
#[allow(non_snake_case)]
pub fn _internal__to_lowercase<'a>(buffer: &'a mut [u8], input: &'a str) -> Option<&'a str> {
pub fn _internal__to_lowercase<'a>(
buffer: &'a mut [MaybeUninit<u8>],
input: &'a str,
) -> Option<&'a str> {
if let Some(buffer) = buffer.get_mut(..input.len()) {
if let Some(first_uppercase) = input.bytes().position(|byte| matches!(byte, b'A'..=b'Z')) {
buffer.copy_from_slice(input.as_bytes());
buffer[first_uppercase..].make_ascii_lowercase();
// `buffer` was initialized to a copy of `input` (which is &str so well-formed UTF-8)
// then lowercased (which preserves UTF-8 well-formedness)
unsafe { Some(::std::str::from_utf8_unchecked(buffer)) }
unsafe {
// This cast doesn’t change the pointer’s validity
// since `u8` has the same layout as `MaybeUninit<u8>`:
let input_bytes = &*(input.as_bytes() as *const [u8] as *const [MaybeUninit<u8>]);

buffer.copy_from_slice(&*input_bytes);

// Same as above re layout, plus these bytes have been initialized:
let buffer = &mut *(buffer as *mut [MaybeUninit<u8>] as *mut [u8]);

buffer[first_uppercase..].make_ascii_lowercase();
// `buffer` was initialized to a copy of `input`
// (which is `&str` so well-formed UTF-8)
// then ASCII-lowercased (which preserves UTF-8 well-formedness):
Some(::std::str::from_utf8_unchecked(buffer))
}
} else {
// Input is already lower-case
Some(input)
Expand Down