From dd750c6f8630dc5cdf27896fe420da9d441459c1 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Tue, 31 Mar 2020 21:32:08 +0200 Subject: [PATCH] Introduce LayoutDom::to_layout_slice It generalises >::attrs. --- components/script/dom/bindings/root.rs | 9 +++++++++ components/script/dom/element.rs | 9 +-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/components/script/dom/bindings/root.rs b/components/script/dom/bindings/root.rs index 9afb421abbb..9f571f1379c 100644 --- a/components/script/dom/bindings/root.rs +++ b/components/script/dom/bindings/root.rs @@ -736,6 +736,15 @@ where debug_assert!(thread_state::get().is_layout()); self.value } + + /// Transforms a slice of Dom into a slice of LayoutDom. + // FIXME(nox): This should probably be done through a ToLayout trait. + pub unsafe fn to_layout_slice(slice: &'dom [Dom]) -> &'dom [LayoutDom<'dom, T>] { + // This doesn't compile if Dom and LayoutDom don't have the same + // representation. + let _ = mem::transmute::, LayoutDom>; + &*(slice as *const [Dom] as *const [LayoutDom]) + } } /// Helper trait for safer manipulations of `Option>` values. diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 446fffea01a..48ef41cbff3 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -598,14 +598,7 @@ impl<'dom> LayoutElementHelpers<'dom> for LayoutDom<'dom, Element> { #[allow(unsafe_code)] #[inline] fn attrs(self) -> &'dom [LayoutDom<'dom, Attr>] { - unsafe { - // FIXME(nox): This should probably be done through a ToLayout trait. - let attrs: &[Dom] = &self.unsafe_get().attrs.borrow_for_layout(); - // This doesn't compile if Dom and LayoutDom don't have the same - // representation. - let _ = mem::transmute::, LayoutDom>; - &*(attrs as *const [Dom] as *const [LayoutDom]) - } + unsafe { LayoutDom::to_layout_slice(self.unsafe_get().attrs.borrow_for_layout()) } } #[inline]