Fix AttrHelpersForLayout

We should never be returning 'static stuff from attrs, that's a big lie.
This commit is contained in:
Anthony Ramine 2020-03-29 19:46:10 +02:00
parent 4a17950331
commit 05c71dff88

View file

@ -233,28 +233,27 @@ impl Attr {
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub trait AttrHelpersForLayout { pub trait AttrHelpersForLayout<'dom> {
unsafe fn value_forever(&self) -> &'static AttrValue; unsafe fn value_forever(self) -> &'dom AttrValue;
unsafe fn value_ref_forever(&self) -> &'static str; unsafe fn value_ref_forever(self) -> &'dom str;
unsafe fn value_tokens_forever(&self) -> Option<&'static [Atom]>; unsafe fn value_tokens_forever(self) -> Option<&'dom [Atom]>;
unsafe fn local_name_atom_forever(&self) -> LocalName; unsafe fn local_name_atom_forever(self) -> LocalName;
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
impl AttrHelpersForLayout for LayoutDom<'_, Attr> { impl<'dom> AttrHelpersForLayout<'dom> for LayoutDom<'dom, Attr> {
#[inline] #[inline]
unsafe fn value_forever(&self) -> &'static AttrValue { unsafe fn value_forever(self) -> &'dom AttrValue {
// This transmute is used to cheat the lifetime restriction. (*self.unsafe_get()).value.borrow_for_layout()
mem::transmute::<&AttrValue, &AttrValue>((*self.unsafe_get()).value.borrow_for_layout())
} }
#[inline] #[inline]
unsafe fn value_ref_forever(&self) -> &'static str { unsafe fn value_ref_forever(self) -> &'dom str {
&**self.value_forever() &**self.value_forever()
} }
#[inline] #[inline]
unsafe fn value_tokens_forever(&self) -> Option<&'static [Atom]> { unsafe fn value_tokens_forever(self) -> Option<&'dom [Atom]> {
// This transmute is used to cheat the lifetime restriction. // This transmute is used to cheat the lifetime restriction.
match *self.value_forever() { match *self.value_forever() {
AttrValue::TokenList(_, ref tokens) => Some(tokens), AttrValue::TokenList(_, ref tokens) => Some(tokens),
@ -263,7 +262,7 @@ impl AttrHelpersForLayout for LayoutDom<'_, Attr> {
} }
#[inline] #[inline]
unsafe fn local_name_atom_forever(&self) -> LocalName { unsafe fn local_name_atom_forever(self) -> LocalName {
(*self.unsafe_get()).identifier.local_name.clone() (*self.unsafe_get()).identifier.local_name.clone()
} }
} }