aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2017-10-14 00:03:57 +0200
committerSimon Sapin <simon.sapin@exyr.org>2017-10-14 00:03:57 +0200
commitb505c9e94817847e19f22854b2ee4258f66f18d2 (patch)
treefad6d01d0f273b36823f70bda152310fa12f7a28
parent4c5d6fd8345f301be4aafe2dba71cdf2de0ab01e (diff)
downloadservo-b505c9e94817847e19f22854b2ee4258f66f18d2.tar.gz
servo-b505c9e94817847e19f22854b2ee4258f66f18d2.zip
Introduce an unsafe HasBaseFlow trait for base()/base_mut() casts.
-rw-r--r--components/layout/block.rs4
-rw-r--r--components/layout/flex.rs4
-rw-r--r--components/layout/flow.rs14
-rw-r--r--components/layout/inline.rs4
-rw-r--r--components/layout/list_item.rs4
-rw-r--r--components/layout/multicol.rs8
-rw-r--r--components/layout/table.rs4
-rw-r--r--components/layout/table_caption.rs4
-rw-r--r--components/layout/table_cell.rs4
-rw-r--r--components/layout/table_colgroup.rs4
-rw-r--r--components/layout/table_row.rs4
-rw-r--r--components/layout/table_rowgroup.rs4
-rw-r--r--components/layout/table_wrapper.rs4
13 files changed, 63 insertions, 3 deletions
diff --git a/components/layout/block.rs b/components/layout/block.rs
index c7f675293d3..bffcbef0e67 100644
--- a/components/layout/block.rs
+++ b/components/layout/block.rs
@@ -492,8 +492,12 @@ pub enum FormattingContextType {
Other,
}
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for BlockFlow {}
+
// A block formatting context.
#[derive(Serialize)]
+#[repr(C)]
pub struct BlockFlow {
/// Data common to all flows.
pub base: BaseFlow,
diff --git a/components/layout/flex.rs b/components/layout/flex.rs
index 063add9e1d5..6d969ef8643 100644
--- a/components/layout/flex.rs
+++ b/components/layout/flex.rs
@@ -328,8 +328,12 @@ impl FlexLine {
}
}
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for FlexFlow {}
+
/// A block with the CSS `display` property equal to `flex`.
#[derive(Debug, Serialize)]
+#[repr(C)]
pub struct FlexFlow {
/// Data common to all block flows.
block_flow: BlockFlow,
diff --git a/components/layout/flow.rs b/components/layout/flow.rs
index 8dad2104523..990e29e0238 100644
--- a/components/layout/flow.rs
+++ b/components/layout/flow.rs
@@ -65,11 +65,19 @@ use table_rowgroup::TableRowGroupFlow;
use table_wrapper::TableWrapperFlow;
use webrender_api::ClipAndScrollInfo;
+/// This marker trait indicates that a type is a struct with `#[repr(C)]` whose first field
+/// is of type `BaseFlow` or some type that also implements this trait.
+///
+/// In other words, the memory representation of `BaseFlow` must be a prefix
+/// of the memory representation of types implementing `HasBaseFlow`.
+#[allow(unsafe_code)]
+pub unsafe trait HasBaseFlow {}
+
/// Virtual methods that make up a float context.
///
/// Note that virtual methods have a cost; we should not overuse them in Servo. Consider adding
/// methods to `ImmutableFlowUtils` or `MutableFlowUtils` before adding more methods here.
-pub trait Flow: fmt::Debug + Sync + Send + 'static {
+pub trait Flow: HasBaseFlow + fmt::Debug + Sync + Send + 'static {
// RTTI
//
// TODO(pcwalton): Use Rust's RTTI, once that works.
@@ -451,7 +459,7 @@ pub trait Flow: fmt::Debug + Sync + Send + 'static {
#[inline(always)]
#[allow(unsafe_code)]
-pub fn base<T: ?Sized + Flow>(this: &T) -> &BaseFlow {
+pub fn base<T: ?Sized + HasBaseFlow>(this: &T) -> &BaseFlow {
let ptr: *const T = this;
let ptr = ptr as *const BaseFlow;
unsafe { &*ptr }
@@ -464,7 +472,7 @@ pub fn child_iter<'a>(flow: &'a Flow) -> FlowListIterator {
#[inline(always)]
#[allow(unsafe_code)]
-pub fn mut_base<T: ?Sized + Flow>(this: &mut T) -> &mut BaseFlow {
+pub fn mut_base<T: ?Sized + HasBaseFlow>(this: &mut T) -> &mut BaseFlow {
let ptr: *mut T = this;
let ptr = ptr as *mut BaseFlow;
unsafe { &mut *ptr }
diff --git a/components/layout/inline.rs b/components/layout/inline.rs
index e04ede22470..e6f23afae8f 100644
--- a/components/layout/inline.rs
+++ b/components/layout/inline.rs
@@ -861,8 +861,12 @@ impl InlineFragments {
}
}
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for InlineFlow {}
+
/// Flows for inline layout.
#[derive(Serialize)]
+#[repr(C)]
pub struct InlineFlow {
/// Data common to all flows.
pub base: BaseFlow,
diff --git a/components/layout/list_item.rs b/components/layout/list_item.rs
index 969d82b7fd7..270d15b3771 100644
--- a/components/layout/list_item.rs
+++ b/components/layout/list_item.rs
@@ -24,8 +24,12 @@ use style::logical_geometry::LogicalSize;
use style::properties::ComputedValues;
use style::servo::restyle_damage::RESOLVE_GENERATED_CONTENT;
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for ListItemFlow {}
+
/// A block with the CSS `display` property equal to `list-item`.
#[derive(Debug)]
+#[repr(C)]
pub struct ListItemFlow {
/// Data common to all block flows.
pub block_flow: BlockFlow,
diff --git a/components/layout/multicol.rs b/components/layout/multicol.rs
index 2cb181f1f97..7b811e38772 100644
--- a/components/layout/multicol.rs
+++ b/components/layout/multicol.rs
@@ -24,6 +24,10 @@ use style::properties::ComputedValues;
use style::values::Either;
use style::values::computed::{LengthOrPercentageOrAuto, LengthOrPercentageOrNone};
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for MulticolFlow {}
+
+#[repr(C)]
pub struct MulticolFlow {
pub block_flow: BlockFlow,
@@ -32,6 +36,10 @@ pub struct MulticolFlow {
pub column_pitch: Au,
}
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for MulticolColumnFlow {}
+
+#[repr(C)]
pub struct MulticolColumnFlow {
pub block_flow: BlockFlow,
}
diff --git a/components/layout/table.rs b/components/layout/table.rs
index a0c7c69ce85..7009e4ec60b 100644
--- a/components/layout/table.rs
+++ b/components/layout/table.rs
@@ -34,10 +34,14 @@ use table_row::{self, CellIntrinsicInlineSize, CollapsedBorder, CollapsedBorderP
use table_row::TableRowFlow;
use table_wrapper::TableLayout;
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for TableFlow {}
+
/// A table flow corresponded to the table's internal table fragment under a table wrapper flow.
/// The properties `position`, `float`, and `margin-*` are used on the table wrapper fragment,
/// not table fragment per CSS 2.1 § 10.5.
#[derive(Serialize)]
+#[repr(C)]
pub struct TableFlow {
pub block_flow: BlockFlow,
diff --git a/components/layout/table_caption.rs b/components/layout/table_caption.rs
index ecdd52784ee..c6425e93b4e 100644
--- a/components/layout/table_caption.rs
+++ b/components/layout/table_caption.rs
@@ -19,7 +19,11 @@ use std::fmt;
use style::logical_geometry::LogicalSize;
use style::properties::ComputedValues;
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for TableCaptionFlow {}
+
/// A table formatting context.
+#[repr(C)]
pub struct TableCaptionFlow {
pub block_flow: BlockFlow,
}
diff --git a/components/layout/table_cell.rs b/components/layout/table_cell.rs
index 1c04b327904..c6c438204cf 100644
--- a/components/layout/table_cell.rs
+++ b/components/layout/table_cell.rs
@@ -28,8 +28,12 @@ use style::values::generics::box_::VerticalAlign;
use table::InternalTable;
use table_row::{CollapsedBorder, CollapsedBorderProvenance};
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for TableCellFlow {}
+
/// A table formatting context.
#[derive(Serialize)]
+#[repr(C)]
pub struct TableCellFlow {
/// Data common to all block flows.
pub block_flow: BlockFlow,
diff --git a/components/layout/table_colgroup.rs b/components/layout/table_colgroup.rs
index 4d54afbe386..4b6af989e25 100644
--- a/components/layout/table_colgroup.rs
+++ b/components/layout/table_colgroup.rs
@@ -19,7 +19,11 @@ use style::logical_geometry::LogicalSize;
use style::properties::ComputedValues;
use style::values::computed::LengthOrPercentageOrAuto;
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for TableColGroupFlow {}
+
/// A table formatting context.
+#[repr(C)]
pub struct TableColGroupFlow {
/// Data common to all flows.
pub base: BaseFlow,
diff --git a/components/layout/table_row.rs b/components/layout/table_row.rs
index 5a835d0ded2..761a363881a 100644
--- a/components/layout/table_row.rs
+++ b/components/layout/table_row.rs
@@ -31,7 +31,11 @@ use style::values::computed::{Color, LengthOrPercentageOrAuto};
use table::{ColumnComputedInlineSize, ColumnIntrinsicInlineSize, InternalTable, VecExt};
use table_cell::{CollapsedBordersForCell, TableCellFlow};
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for TableRowFlow {}
+
/// A single row of a table.
+#[repr(C)]
pub struct TableRowFlow {
/// Fields common to all block flows.
pub block_flow: BlockFlow,
diff --git a/components/layout/table_rowgroup.rs b/components/layout/table_rowgroup.rs
index 6877ba8b781..0639f02dbf5 100644
--- a/components/layout/table_rowgroup.rs
+++ b/components/layout/table_rowgroup.rs
@@ -24,7 +24,11 @@ use style::logical_geometry::LogicalSize;
use style::properties::ComputedValues;
use table::{ColumnIntrinsicInlineSize, InternalTable, TableLikeFlow};
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for TableRowGroupFlow {}
+
/// A table formatting context.
+#[repr(C)]
pub struct TableRowGroupFlow {
/// Fields common to all block flows.
pub block_flow: BlockFlow,
diff --git a/components/layout/table_wrapper.rs b/components/layout/table_wrapper.rs
index 972f5299094..866916b9ad5 100644
--- a/components/layout/table_wrapper.rs
+++ b/components/layout/table_wrapper.rs
@@ -43,8 +43,12 @@ pub enum TableLayout {
Auto
}
+#[allow(unsafe_code)]
+unsafe impl ::flow::HasBaseFlow for TableWrapperFlow {}
+
/// A table wrapper flow based on a block formatting context.
#[derive(Serialize)]
+#[repr(C)]
pub struct TableWrapperFlow {
pub block_flow: BlockFlow,