mirror of https://github.com/yewstack/yew
constify VList::new (#2293)
* Use const constructor for VList and fix EMPTY VList access * Add comment
This commit is contained in:
parent
73b5f6b4c1
commit
61efc8042e
|
@ -4,6 +4,7 @@ Cargo.lock
|
|||
# backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# editor config directories
|
||||
# editor config files and directories
|
||||
*.iml
|
||||
/.idea/
|
||||
/.vscode/
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue