script: Reduce the use of unsafe in LayoutDom (#31979)

Remove the use of unsafe code in the layout wrappers of the DOM. The
main change here is that `unsafe_get()` no longer needs to be an unsafe
method, which allows us to transitively remove or reduce unsafe blocks
from callers. The function itself is not renamed, because it's still
a bit dangerous to start removing the layers of abstraction from actual
DOM nodes.

In addition `init_style_and_opaque_layout_data` can be merged into
`initialize_data`, which removes one more unsafe method.

Finally, a "Safety" section is added to some unsafe methods.
This commit is contained in:
Martin Robinson 2024-04-03 10:41:19 +02:00 committed by GitHub
parent 8aaff61334
commit 18b37e676b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 89 additions and 86 deletions

View file

@ -641,9 +641,8 @@ pub trait LayoutElementHelpers<'dom> {
}
impl<'dom> LayoutDom<'dom, Element> {
#[allow(unsafe_code)]
pub(super) fn focus_state(self) -> bool {
unsafe { self.unsafe_get().state.get().contains(ElementState::FOCUS) }
self.unsafe_get().state.get().contains(ElementState::FOCUS)
}
}
@ -1059,12 +1058,11 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> {
#[allow(unsafe_code)]
fn local_name(self) -> &'dom LocalName {
unsafe { &(self.unsafe_get()).local_name }
&(self.unsafe_get()).local_name
}
#[allow(unsafe_code)]
fn namespace(self) -> &'dom Namespace {
unsafe { &(self.unsafe_get()).namespace }
&(self.unsafe_get()).namespace
}
fn get_lang_for_layout(self) -> String {
@ -1091,25 +1089,20 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> {
}
#[inline]
#[allow(unsafe_code)]
fn get_state_for_layout(self) -> ElementState {
unsafe { (self.unsafe_get()).state.get() }
(self.unsafe_get()).state.get()
}
#[inline]
#[allow(unsafe_code)]
fn insert_selector_flags(self, flags: ElementSelectorFlags) {
debug_assert!(thread_state::get().is_layout());
unsafe {
let f = &(self.unsafe_get()).selector_flags;
f.set(f.get() | flags);
}
let f = &(self.unsafe_get()).selector_flags;
f.set(f.get() | flags);
}
#[inline]
#[allow(unsafe_code)]
fn get_selector_flags(self) -> ElementSelectorFlags {
unsafe { self.unsafe_get().selector_flags.get() }
self.unsafe_get().selector_flags.get()
}
#[inline]