constify VList::new (#2293)

* Use const constructor for VList and fix EMPTY VList access

* Add comment
This commit is contained in:
Alexander Mescheryakov 2021-12-23 22:57:50 +03:00 committed by GitHub
parent 73b5f6b4c1
commit 61efc8042e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 18 deletions

3
.gitignore vendored
View File

@ -4,6 +4,7 @@ Cargo.lock
# backup files generated by rustfmt
**/*.rs.bk
# editor config directories
# editor config files and directories
*.iml
/.idea/
/.vscode/

View File

@ -19,11 +19,7 @@ pub struct VList {
impl Default for VList {
fn default() -> Self {
Self {
children: Default::default(),
key: None,
fully_keyed: true,
}
Self::new()
}
}
@ -88,8 +84,12 @@ impl<'s> ElementWriter<'s> {
impl VList {
/// Creates a new empty [VList] instance.
pub fn new() -> Self {
Self::default()
pub const fn new() -> Self {
Self {
children: Vec::new(),
key: None,
fully_keyed: true,
}
}
/// Creates a new [VList] instance with children.

View File

@ -8,10 +8,8 @@ use std::borrow::Cow;
use std::cmp::PartialEq;
use std::hint::unreachable_unchecked;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::rc::Rc;
use std::sync::Once;
use wasm_bindgen::JsCast;
use web_sys::{Element, HtmlInputElement as InputElement, HtmlTextAreaElement as TextAreaElement};
@ -338,14 +336,11 @@ impl VTag {
match &self.inner {
VTagInner::Other { children, .. } => children,
_ => {
static mut EMPTY: MaybeUninit<VList> = MaybeUninit::uninit();
static ONCE: Once = Once::new();
unsafe {
ONCE.call_once(|| {
EMPTY = MaybeUninit::new(VList::default());
});
&*EMPTY.as_ptr()
}
// This is mutable because the VList is not Sync
static mut EMPTY: VList = VList::new();
// SAFETY: The EMPTY value is always read-only
unsafe { &EMPTY }
}
}
}