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

@ -1344,7 +1344,23 @@ pub trait LayoutNodeHelpers<'dom> {
fn children_count(self) -> u32;
fn get_style_and_opaque_layout_data(self) -> Option<&'dom StyleAndOpaqueLayoutData>;
/// Initialize the style and opaque layout data of this node.
///
/// # Safety
///
/// This method is unsafe because it modifies the given node during
/// layout. Callers should ensure that no other layout thread is
/// attempting to read or modify the opaque layout data of this node.
unsafe fn init_style_and_opaque_layout_data(self, data: Box<StyleAndOpaqueLayoutData>);
/// Unset and return the style and opaque layout data of this node.
///
/// # Safety
///
/// This method is unsafe because it modifies the given node during
/// layout. Callers should ensure that no other layout thread is
/// attempting to read or modify the opaque layout data of this node.
unsafe fn take_style_and_opaque_layout_data(self) -> Box<StyleAndOpaqueLayoutData>;
fn text_content(self) -> Cow<'dom, str>;
@ -1370,9 +1386,8 @@ impl<'dom> LayoutDom<'dom, Node> {
impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {
#[inline]
#[allow(unsafe_code)]
fn type_id_for_layout(self) -> NodeTypeId {
unsafe { self.unsafe_get().type_id() }
self.unsafe_get().type_id()
}
#[inline]
@ -1461,9 +1476,8 @@ impl<'dom> LayoutNodeHelpers<'dom> for LayoutDom<'dom, Node> {
}
#[inline]
#[allow(unsafe_code)]
fn children_count(self) -> u32 {
unsafe { self.unsafe_get().children_count.get() }
self.unsafe_get().children_count.get()
}
// FIXME(nox): How we handle style and layout data needs to be completely