layout: Remove TryFrom<Contents> for NonReplacedContents (#37815)

This implementation is quite confusing as it makes it harder to tell
that we are just looking for the case that `Contents` contains
`NonReplacedContents`.

Testing: This shouldn't have any functional change, so is covered by
existing tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-07-01 20:21:33 +02:00 committed by GitHub
parent e0af75f265
commit 7375d09887
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 77 deletions

View file

@ -376,19 +376,6 @@ impl From<NonReplacedContents> for Contents {
} }
} }
impl std::convert::TryFrom<Contents> for NonReplacedContents {
type Error = &'static str;
fn try_from(contents: Contents) -> Result<Self, Self::Error> {
match contents {
Contents::NonReplaced(non_replaced_contents) => Ok(non_replaced_contents),
Contents::Replaced(_) => {
Err("Tried to covnert a `Contents::Replaced` into `NonReplacedContent`")
},
}
}
}
impl NonReplacedContents { impl NonReplacedContents {
pub(crate) fn traverse<'dom>( pub(crate) fn traverse<'dom>(
self, self,

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::TryFrom;
use rayon::iter::{IntoParallelIterator, ParallelIterator}; use rayon::iter::{IntoParallelIterator, ParallelIterator};
use servo_arc::Arc; use servo_arc::Arc;
@ -464,9 +463,12 @@ impl<'dom> BlockContainerBuilder<'dom, '_> {
contents: Contents, contents: Contents,
box_slot: BoxSlot<'dom>, box_slot: BoxSlot<'dom>,
) { ) {
let (DisplayInside::Flow { is_list_item }, false) = let (is_list_item, non_replaced_contents) = match (display_inside, contents) {
(display_inside, contents.is_replaced()) (
else { DisplayInside::Flow { is_list_item },
Contents::NonReplaced(non_replaced_contents),
) => (is_list_item, non_replaced_contents),
(_, contents) => {
// If this inline element is an atomic, handle it and return. // If this inline element is an atomic, handle it and return.
let context = self.context; let context = self.context;
let propagated_data = self.propagated_data; let propagated_data = self.propagated_data;
@ -481,6 +483,7 @@ impl<'dom> BlockContainerBuilder<'dom, '_> {
); );
box_slot.set(LayoutBox::InlineLevel(vec![atomic])); box_slot.set(LayoutBox::InlineLevel(vec![atomic]));
return; return;
},
}; };
// Otherwise, this is just a normal inline box. Whatever happened before, all we need to do // Otherwise, this is just a normal inline box. Whatever happened before, all we need to do
@ -500,9 +503,7 @@ impl<'dom> BlockContainerBuilder<'dom, '_> {
} }
// `unwrap` doesnt panic here because `is_replaced` returned `false`. // `unwrap` doesnt panic here because `is_replaced` returned `false`.
NonReplacedContents::try_from(contents) non_replaced_contents.traverse(self.context, info, self);
.unwrap()
.traverse(self.context, info, self);
self.finish_anonymous_table_if_needed(); self.finish_anonymous_table_if_needed();

View file

@ -3,7 +3,6 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::{TryFrom, TryInto};
use std::iter::repeat; use std::iter::repeat;
use atomic_refcell::AtomicRef; use atomic_refcell::AtomicRef;
@ -769,11 +768,10 @@ impl<'dom> TraversalHandler<'dom> for TableBuilderTraversal<'_, 'dom> {
let new_row_group_index = self.builder.table.row_groups.len() - 1; let new_row_group_index = self.builder.table.row_groups.len() - 1;
self.current_row_group_index = Some(new_row_group_index); self.current_row_group_index = Some(new_row_group_index);
NonReplacedContents::try_from(contents).unwrap().traverse( let Contents::NonReplaced(non_replaced_contents) = contents else {
self.context, unreachable!("Replaced should not have a LayoutInternal display type.");
info, };
self, non_replaced_contents.traverse(self.context, info, self);
);
self.finish_anonymous_row_if_needed(); self.finish_anonymous_row_if_needed();
self.current_row_group_index = None; self.current_row_group_index = None;
@ -787,14 +785,13 @@ impl<'dom> TraversalHandler<'dom> for TableBuilderTraversal<'_, 'dom> {
self.finish_anonymous_row_if_needed(); self.finish_anonymous_row_if_needed();
let context = self.context; let context = self.context;
let mut row_builder = let mut row_builder =
TableRowBuilder::new(self, info, self.current_propagated_data); TableRowBuilder::new(self, info, self.current_propagated_data);
NonReplacedContents::try_from(contents).unwrap().traverse(
context, let Contents::NonReplaced(non_replaced_contents) = contents else {
info, unreachable!("Replaced should not have a LayoutInternal display type.");
&mut row_builder, };
); non_replaced_contents.traverse(context, info, &mut row_builder);
row_builder.finish(); row_builder.finish();
let row = ArcRefCell::new(TableTrack { let row = ArcRefCell::new(TableTrack {
@ -822,11 +819,10 @@ impl<'dom> TraversalHandler<'dom> for TableBuilderTraversal<'_, 'dom> {
columns: Vec::new(), columns: Vec::new(),
}; };
NonReplacedContents::try_from(contents).unwrap().traverse( let Contents::NonReplaced(non_replaced_contents) = contents else {
self.context, unreachable!("Replaced should not have a LayoutInternal display type.");
info, };
&mut column_group_builder, non_replaced_contents.traverse(self.context, info, &mut column_group_builder);
);
let first_column = self.builder.table.columns.len(); let first_column = self.builder.table.columns.len();
if column_group_builder.columns.is_empty() { if column_group_builder.columns.is_empty() {
@ -855,20 +851,17 @@ impl<'dom> TraversalHandler<'dom> for TableBuilderTraversal<'_, 'dom> {
))); )));
}, },
DisplayLayoutInternal::TableCaption => { DisplayLayoutInternal::TableCaption => {
let contents = match contents.try_into() { let Contents::NonReplaced(non_replaced_contents) = contents else {
Ok(non_replaced_contents) => { unreachable!("Replaced should not have a LayoutInternal display type.");
};
let contents =
IndependentNonReplacedContents::Flow(BlockFormattingContext::construct( IndependentNonReplacedContents::Flow(BlockFormattingContext::construct(
self.context, self.context,
info, info,
non_replaced_contents, non_replaced_contents,
self.current_propagated_data, self.current_propagated_data,
false, /* is_list_item */ false, /* is_list_item */
)) ));
},
Err(_replaced) => {
unreachable!("Replaced should not have a LayoutInternal display type.");
},
};
let caption = ArcRefCell::new(TableCaption { let caption = ArcRefCell::new(TableCaption {
context: IndependentFormattingContext { context: IndependentFormattingContext {
@ -1016,20 +1009,17 @@ impl<'dom> TraversalHandler<'dom> for TableRowBuilder<'_, '_, 'dom, '_> {
let propagated_data = let propagated_data =
self.propagated_data.disallowing_percentage_table_columns(); self.propagated_data.disallowing_percentage_table_columns();
let contents = match contents.try_into() { let Contents::NonReplaced(non_replaced_contents) = contents else {
Ok(non_replaced_contents) => { unreachable!("Replaced should not have a LayoutInternal display type.");
BlockFormattingContext::construct( };
let contents = BlockFormattingContext::construct(
self.table_traversal.context, self.table_traversal.context,
info, info,
non_replaced_contents, non_replaced_contents,
propagated_data, propagated_data,
false, /* is_list_item */ false, /* is_list_item */
) );
},
Err(_replaced) => {
unreachable!("Replaced should not have a LayoutInternal display type.");
},
};
self.finish_current_anonymous_cell_if_needed(); self.finish_current_anonymous_cell_if_needed();