gecko-dev/servo/components/layout/flow_ref.rs
Shing Lyu b988a636be servo: Merge #13740 - Migrated -Z trace-layout to serde_json (from shinglyu:layout_serde); r=jdm
<!-- Please describe your changes on the following line: -->

Migrated the trace-layout code from old `rustc-serialize` to `serde_json`. This will help us iterate faster on the layout viewer (#13432), #13436, #12675 and fix #12936.
---

<!-- 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] These changes fix #12936 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because it's a relatively low risk debug tool

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

Source-Repo: https://github.com/servo/servo
Source-Revision: 1c26f44cbb23e7da3b9d63f0223c8d5d43eec958
2016-11-07 02:15:03 -06:00

67 lines
2.2 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! Reference-counted pointers to flows.
//!
//! Eventually, with dynamically sized types in Rust, much of this code will
//! be superfluous. This design is largely duplicating logic of Arc<T> and
//! Weak<T>; please see comments there for details.
use flow::Flow;
use std::ops::Deref;
use std::sync::{Arc, Weak};
#[derive(Clone,Debug)]
pub struct FlowRef(Arc<Flow>);
impl Deref for FlowRef {
type Target = Flow;
fn deref(&self) -> &Flow {
self.0.deref()
}
}
impl FlowRef {
/// `FlowRef`s can only be made available to the traversal code.
/// See https://github.com/servo/servo/issues/14014 for more details.
pub fn new(mut r: Arc<Flow>) -> Self {
// This assertion checks that this `FlowRef` does not alias normal `Arc`s.
// If that happens, we're in trouble.
assert!(Arc::get_mut(&mut r).is_some());
FlowRef(r)
}
pub fn get_mut(this: &mut FlowRef) -> Option<&mut Flow> {
Arc::get_mut(&mut this.0)
}
pub fn downgrade(this: &FlowRef) -> WeakFlowRef {
WeakFlowRef(Arc::downgrade(&this.0))
}
pub fn into_arc(mut this: FlowRef) -> Arc<Flow> {
// This assertion checks that this `FlowRef` does not alias normal `Arc`s.
// If that happens, we're in trouble.
assert!(FlowRef::get_mut(&mut this).is_some());
this.0
}
/// WARNING: This should only be used when there is no aliasing:
/// when the traversal ensures that no other threads accesses the same flow at the same time.
/// See https://github.com/servo/servo/issues/6503
/// Use Arc::get_mut instead when possible (e.g. on an Arc that was just created).
#[allow(unsafe_code)]
pub fn deref_mut(this: &mut FlowRef) -> &mut Flow {
let ptr: *const Flow = &*this.0;
unsafe { &mut *(ptr as *mut Flow) }
}
}
#[derive(Clone,Debug)]
pub struct WeakFlowRef(Weak<Flow>);
impl WeakFlowRef {
pub fn upgrade(&self) -> Option<FlowRef> {
self.0.upgrade().map(FlowRef)
}
}