Auto merge of #18523 - asajeffrey:pipeline-id-nonzero, r=mbrubeck

Make pipeline ids non-zero so that optional ids take no extra space

<!-- Please describe your changes on the following line: -->

Use the NonZero trait to reduce space usage of `Option<PipelineId>`.

This needs a bump of serde to get https://github.com/serde-rs/serde/pull/1003.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] There are tests for these changes OR

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18523)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-09-18 15:50:57 -05:00 committed by GitHub
commit 7256a05d74
11 changed files with 128 additions and 82 deletions

View file

@ -6,6 +6,7 @@ use CompositionPipeline;
use SendableFrameTree;
use compositor_thread::{CompositorProxy, CompositorReceiver};
use compositor_thread::{InitialCompositorState, Msg, RenderListener};
use core::nonzero::NonZero;
use euclid::{Point2D, TypedPoint2D, TypedVector2D, ScaleFactor};
use gfx_traits::Epoch;
use gleam::gl;
@ -61,7 +62,7 @@ impl ConvertPipelineIdFromWebRender for webrender_api::PipelineId {
fn from_webrender(&self) -> PipelineId {
PipelineId {
namespace_id: PipelineNamespaceId(self.0),
index: PipelineIndex(self.1),
index: PipelineIndex(NonZero::new(self.1).expect("Webrender pipeline zero?")),
}
}
}

View file

@ -4,7 +4,9 @@
#![deny(unsafe_code)]
#![feature(box_syntax)]
#![feature(nonzero)]
extern crate core;
extern crate euclid;
extern crate gfx_traits;
extern crate gleam;

View file

@ -13,5 +13,5 @@ path = "lib.rs"
bitflags = "0.7"
heapsize = "0.4"
heapsize_derive = "0.1"
serde = "1.0"
serde = { version = "1.0.14", features = [ "unstable" ] }
webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}

View file

@ -5,6 +5,7 @@
//! The high-level interface from script to constellation. Using this abstract interface helps
//! reduce coupling between these two components.
use core::nonzero::NonZero;
use std::cell::Cell;
use std::fmt;
use webrender_api;
@ -194,10 +195,9 @@ impl PipelineNamespace {
});
}
fn next_index(&mut self) -> u32 {
let result = self.index;
self.index = result + 1;
result
fn next_index(&mut self) -> NonZero<u32> {
self.index += 1;
NonZero::new(self.index).expect("pipeline id index wrapped!")
}
fn next_pipeline_id(&mut self) -> PipelineId {
@ -220,8 +220,9 @@ thread_local!(pub static PIPELINE_NAMESPACE: Cell<Option<PipelineNamespace>> = C
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PipelineNamespaceId(pub u32);
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PipelineIndex(pub u32);
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PipelineIndex(pub NonZero<u32>);
known_heap_size!(0, PipelineIndex);
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub struct PipelineId {
@ -242,7 +243,7 @@ impl PipelineId {
pub fn to_webrender(&self) -> webrender_api::PipelineId {
let PipelineNamespaceId(namespace_id) = self.namespace_id;
let PipelineIndex(index) = self.index;
webrender_api::PipelineId(namespace_id, index)
webrender_api::PipelineId(namespace_id, index.get())
}
pub fn root_scroll_node(&self) -> webrender_api::ClipId {
@ -258,17 +259,18 @@ impl fmt::Display for PipelineId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let PipelineNamespaceId(namespace_id) = self.namespace_id;
let PipelineIndex(index) = self.index;
write!(fmt, "({},{})", namespace_id, index)
write!(fmt, "({},{})", namespace_id, index.get())
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub struct BrowsingContextIndex(pub u32);
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct BrowsingContextIndex(pub NonZero<u32>);
known_heap_size!(0, BrowsingContextIndex);
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub struct BrowsingContextId {
pub namespace_id: PipelineNamespaceId,
pub index: BrowsingContextIndex
pub index: BrowsingContextIndex,
}
impl BrowsingContextId {
@ -286,7 +288,7 @@ impl fmt::Display for BrowsingContextId {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let PipelineNamespaceId(namespace_id) = self.namespace_id;
let BrowsingContextIndex(index) = self.index;
write!(fmt, "({},{})", namespace_id, index)
write!(fmt, "({},{})", namespace_id, index.get())
}
}
@ -336,9 +338,12 @@ impl PartialEq<BrowsingContextId> for TopLevelBrowsingContextId {
// We provide ids just for unit testing.
pub const TEST_NAMESPACE: PipelineNamespaceId = PipelineNamespaceId(1234);
pub const TEST_PIPELINE_INDEX: PipelineIndex = PipelineIndex(5678);
#[allow(unsafe_code)]
pub const TEST_PIPELINE_INDEX: PipelineIndex = unsafe { PipelineIndex(NonZero::new_unchecked(5678)) };
pub const TEST_PIPELINE_ID: PipelineId = PipelineId { namespace_id: TEST_NAMESPACE, index: TEST_PIPELINE_INDEX };
pub const TEST_BROWSING_CONTEXT_INDEX: BrowsingContextIndex = BrowsingContextIndex(8765);
#[allow(unsafe_code)]
pub const TEST_BROWSING_CONTEXT_INDEX: BrowsingContextIndex =
unsafe { BrowsingContextIndex(NonZero::new_unchecked(8765)) };
pub const TEST_BROWSING_CONTEXT_ID: BrowsingContextId =
BrowsingContextId { namespace_id: TEST_NAMESPACE, index: TEST_BROWSING_CONTEXT_INDEX };

View file

@ -2,11 +2,15 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#![feature(const_fn)]
#![feature(const_nonzero_new)]
#![feature(nonzero)]
#![deny(unsafe_code)]
#[macro_use]
extern crate bitflags;
extern crate heapsize;
extern crate core;
#[macro_use] extern crate heapsize;
#[macro_use] extern crate heapsize_derive;
#[macro_use] extern crate serde;
extern crate webrender_api;