mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Use our copy of RefCell for style data.
This allows removing `#![feature(as_unsafe_cell)]` in geckolib and make progress towards #11815.
This commit is contained in:
parent
0fb5d634a0
commit
f7f81e0ed0
12 changed files with 481 additions and 11 deletions
|
@ -2,7 +2,6 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
#![feature(as_unsafe_cell)]
|
|
||||||
#![feature(box_patterns)]
|
#![feature(box_patterns)]
|
||||||
#![feature(box_syntax)]
|
#![feature(box_syntax)]
|
||||||
#![feature(custom_derive)]
|
#![feature(custom_derive)]
|
||||||
|
|
|
@ -34,9 +34,9 @@ use core::nonzero::NonZero;
|
||||||
use data::{LayoutDataFlags, PrivateLayoutData};
|
use data::{LayoutDataFlags, PrivateLayoutData};
|
||||||
use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode};
|
use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode};
|
||||||
use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutData};
|
use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutData};
|
||||||
use std::cell::{Ref, RefCell, RefMut};
|
|
||||||
use style::computed_values::content::{self, ContentItem};
|
use style::computed_values::content::{self, ContentItem};
|
||||||
use style::properties::ComputedValues;
|
use style::properties::ComputedValues;
|
||||||
|
use style::refcell::{Ref, RefCell, RefMut};
|
||||||
|
|
||||||
pub type NonOpaqueStyleAndLayoutData = *mut RefCell<PrivateLayoutData>;
|
pub type NonOpaqueStyleAndLayoutData = *mut RefCell<PrivateLayoutData>;
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,6 @@ use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutDat
|
||||||
use script_traits::{ConstellationControlMsg, LayoutControlMsg, LayoutMsg as ConstellationMsg};
|
use script_traits::{ConstellationControlMsg, LayoutControlMsg, LayoutMsg as ConstellationMsg};
|
||||||
use script_traits::{StackingContextScrollState, UntrustedNodeAddress};
|
use script_traits::{StackingContextScrollState, UntrustedNodeAddress};
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::hash::BuildHasherDefault;
|
use std::hash::BuildHasherDefault;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
@ -108,6 +107,7 @@ use style::logical_geometry::LogicalPoint;
|
||||||
use style::media_queries::{Device, MediaType};
|
use style::media_queries::{Device, MediaType};
|
||||||
use style::parallel::WorkQueueData;
|
use style::parallel::WorkQueueData;
|
||||||
use style::properties::ComputedValues;
|
use style::properties::ComputedValues;
|
||||||
|
use style::refcell::RefCell;
|
||||||
use style::selector_matching::USER_OR_USER_AGENT_STYLESHEETS;
|
use style::selector_matching::USER_OR_USER_AGENT_STYLESHEETS;
|
||||||
use style::servo::{SharedStyleContext, Stylesheet, Stylist};
|
use style::servo::{SharedStyleContext, Stylesheet, Stylist};
|
||||||
use style::stylesheets::CSSRuleIteratorExt;
|
use style::stylesheets::CSSRuleIteratorExt;
|
||||||
|
|
|
@ -50,7 +50,6 @@ use script_layout_interface::{OpaqueStyleAndLayoutData, PartialStyleAndLayoutDat
|
||||||
use selectors::matching::{DeclarationBlock, ElementFlags};
|
use selectors::matching::{DeclarationBlock, ElementFlags};
|
||||||
use selectors::parser::{AttrSelector, NamespaceConstraint};
|
use selectors::parser::{AttrSelector, NamespaceConstraint};
|
||||||
use smallvec::VecLike;
|
use smallvec::VecLike;
|
||||||
use std::cell::{Ref, RefCell, RefMut};
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::mem::{transmute, transmute_copy};
|
use std::mem::{transmute, transmute_copy};
|
||||||
use string_cache::{Atom, BorrowedAtom, BorrowedNamespace, Namespace};
|
use string_cache::{Atom, BorrowedAtom, BorrowedNamespace, Namespace};
|
||||||
|
@ -60,6 +59,7 @@ use style::dom::{PresentationalHintsSynthetizer, OpaqueNode, TDocument, TElement
|
||||||
use style::element_state::*;
|
use style::element_state::*;
|
||||||
use style::properties::{ComputedValues, ServoComputedValues};
|
use style::properties::{ComputedValues, ServoComputedValues};
|
||||||
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock};
|
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock};
|
||||||
|
use style::refcell::{Ref, RefCell, RefMut};
|
||||||
use style::restyle_hints::ElementSnapshot;
|
use style::restyle_hints::ElementSnapshot;
|
||||||
use style::selector_impl::{NonTSPseudoClass, ServoSelectorImpl};
|
use style::selector_impl::{NonTSPseudoClass, ServoSelectorImpl};
|
||||||
use style::servo::{PrivateStyleData, SharedStyleContext};
|
use style::servo::{PrivateStyleData, SharedStyleContext};
|
||||||
|
|
|
@ -52,7 +52,7 @@ use core::nonzero::NonZero;
|
||||||
use ipc_channel::ipc::IpcSender;
|
use ipc_channel::ipc::IpcSender;
|
||||||
use libc::c_void;
|
use libc::c_void;
|
||||||
use restyle_damage::RestyleDamage;
|
use restyle_damage::RestyleDamage;
|
||||||
use std::cell::RefCell;
|
use style::refcell::RefCell;
|
||||||
use style::servo::PrivateStyleData;
|
use style::servo::PrivateStyleData;
|
||||||
|
|
||||||
pub struct PartialStyleAndLayoutData {
|
pub struct PartialStyleAndLayoutData {
|
||||||
|
|
|
@ -10,13 +10,13 @@ use gfx_traits::{ByteIndex, LayerId, LayerType};
|
||||||
use msg::constellation_msg::PipelineId;
|
use msg::constellation_msg::PipelineId;
|
||||||
use range::Range;
|
use range::Range;
|
||||||
use restyle_damage::RestyleDamage;
|
use restyle_damage::RestyleDamage;
|
||||||
use std::cell::{Ref, RefCell};
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use string_cache::{Atom, BorrowedAtom, BorrowedNamespace, Namespace};
|
use string_cache::{Atom, BorrowedAtom, BorrowedNamespace, Namespace};
|
||||||
use style::computed_values::display;
|
use style::computed_values::display;
|
||||||
use style::dom::OpaqueNode;
|
use style::dom::OpaqueNode;
|
||||||
use style::dom::{PresentationalHintsSynthetizer, TNode};
|
use style::dom::{PresentationalHintsSynthetizer, TNode};
|
||||||
use style::properties::ServoComputedValues;
|
use style::properties::ServoComputedValues;
|
||||||
|
use style::refcell::{Ref, RefCell};
|
||||||
use style::selector_impl::{PseudoElement, PseudoElementCascadeType, ServoSelectorImpl};
|
use style::selector_impl::{PseudoElement, PseudoElementCascadeType, ServoSelectorImpl};
|
||||||
use style::servo::SharedStyleContext;
|
use style::servo::SharedStyleContext;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
|
@ -8,12 +8,12 @@ use context::SharedStyleContext;
|
||||||
use data::PrivateStyleData;
|
use data::PrivateStyleData;
|
||||||
use element_state::ElementState;
|
use element_state::ElementState;
|
||||||
use properties::{ComputedValues, PropertyDeclaration, PropertyDeclarationBlock};
|
use properties::{ComputedValues, PropertyDeclaration, PropertyDeclarationBlock};
|
||||||
|
use refcell::{Ref, RefMut};
|
||||||
use restyle_hints::{ElementSnapshot, RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint};
|
use restyle_hints::{ElementSnapshot, RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint};
|
||||||
use selector_impl::{ElementExt, SelectorImplExt};
|
use selector_impl::{ElementExt, SelectorImplExt};
|
||||||
use selectors::Element;
|
use selectors::Element;
|
||||||
use selectors::matching::DeclarationBlock;
|
use selectors::matching::DeclarationBlock;
|
||||||
use smallvec::VecLike;
|
use smallvec::VecLike;
|
||||||
use std::cell::{Ref, RefMut};
|
|
||||||
use std::ops::BitOr;
|
use std::ops::BitOr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use string_cache::{Atom, Namespace};
|
use string_cache::{Atom, Namespace};
|
||||||
|
|
|
@ -63,6 +63,7 @@ pub mod matching;
|
||||||
pub mod media_queries;
|
pub mod media_queries;
|
||||||
pub mod parallel;
|
pub mod parallel;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
|
pub mod refcell;
|
||||||
pub mod restyle_hints;
|
pub mod restyle_hints;
|
||||||
pub mod selector_impl;
|
pub mod selector_impl;
|
||||||
pub mod selector_matching;
|
pub mod selector_matching;
|
||||||
|
|
461
components/style/refcell.rs
Normal file
461
components/style/refcell.rs
Normal file
|
@ -0,0 +1,461 @@
|
||||||
|
// Copyright 2012-2014 The Rust Project Developers.
|
||||||
|
// See http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
//! A fork of std::cell::RefCell that makes `as_unsafe_cell` usable on stable Rust.
|
||||||
|
//!
|
||||||
|
//! FIXME(https://github.com/rust-lang/rust/issues/27708): Remove this when
|
||||||
|
//! `std::cell::RefCell::as_unsafe_cell` is in Rust’s stable channel.
|
||||||
|
|
||||||
|
#![allow(unsafe_code)]
|
||||||
|
|
||||||
|
use std::cell::{UnsafeCell, Cell};
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
/// A fork of std::cell::RefCell that makes `as_unsafe_cell` usable on stable Rust.
|
||||||
|
///
|
||||||
|
/// FIXME(https://github.com/rust-lang/rust/issues/27708): Remove this when
|
||||||
|
/// `std::cell::RefCell::as_unsafe_cell` is in Rust’s stable channel.
|
||||||
|
pub struct RefCell<T: ?Sized> {
|
||||||
|
borrow: Cell<BorrowFlag>,
|
||||||
|
value: UnsafeCell<T>,
|
||||||
|
}
|
||||||
|
type BorrowFlag = usize;
|
||||||
|
|
||||||
|
/// An enumeration of values returned from the `state` method on a `RefCell<T>`.
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||||
|
pub enum BorrowState {
|
||||||
|
/// The cell is currently being read, there is at least one active `borrow`.
|
||||||
|
Reading,
|
||||||
|
/// The cell is currently being written to, there is an active `borrow_mut`.
|
||||||
|
Writing,
|
||||||
|
/// There are no outstanding borrows on this cell.
|
||||||
|
Unused,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values [1, MAX-1] represent the number of `Ref` active
|
||||||
|
// (will not outgrow its range since `usize` is the size of the address space)
|
||||||
|
const UNUSED: BorrowFlag = 0;
|
||||||
|
const WRITING: BorrowFlag = !0;
|
||||||
|
|
||||||
|
impl<T> RefCell<T> {
|
||||||
|
/// Creates a new `RefCell` containing `value`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::RefCell;
|
||||||
|
///
|
||||||
|
/// let c = RefCell::new(5);
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
pub fn new(value: T) -> RefCell<T> {
|
||||||
|
RefCell {
|
||||||
|
value: UnsafeCell::new(value),
|
||||||
|
borrow: Cell::new(UNUSED),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consumes the `RefCell`, returning the wrapped value.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::RefCell;
|
||||||
|
///
|
||||||
|
/// let c = RefCell::new(5);
|
||||||
|
///
|
||||||
|
/// let five = c.into_inner();
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
pub fn into_inner(self) -> T {
|
||||||
|
// Since this function takes `self` (the `RefCell`) by value, the
|
||||||
|
// compiler statically verifies that it is not currently borrowed.
|
||||||
|
// Therefore the following assertion is just a `debug_assert!`.
|
||||||
|
debug_assert!(self.borrow.get() == UNUSED);
|
||||||
|
unsafe { self.value.into_inner() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized> RefCell<T> {
|
||||||
|
/// Query the current state of this `RefCell`
|
||||||
|
///
|
||||||
|
/// The returned value can be dispatched on to determine if a call to
|
||||||
|
/// `borrow` or `borrow_mut` would succeed.
|
||||||
|
#[inline]
|
||||||
|
pub fn borrow_state(&self) -> BorrowState {
|
||||||
|
match self.borrow.get() {
|
||||||
|
WRITING => BorrowState::Writing,
|
||||||
|
UNUSED => BorrowState::Unused,
|
||||||
|
_ => BorrowState::Reading,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Immutably borrows the wrapped value.
|
||||||
|
///
|
||||||
|
/// The borrow lasts until the returned `Ref` exits scope. Multiple
|
||||||
|
/// immutable borrows can be taken out at the same time.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the value is currently mutably borrowed.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::RefCell;
|
||||||
|
///
|
||||||
|
/// let c = RefCell::new(5);
|
||||||
|
///
|
||||||
|
/// let borrowed_five = c.borrow();
|
||||||
|
/// let borrowed_five2 = c.borrow();
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// An example of panic:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::RefCell;
|
||||||
|
/// use std::thread;
|
||||||
|
///
|
||||||
|
/// let result = thread::spawn(move || {
|
||||||
|
/// let c = RefCell::new(5);
|
||||||
|
/// let m = c.borrow_mut();
|
||||||
|
///
|
||||||
|
/// let b = c.borrow(); // this causes a panic
|
||||||
|
/// }).join();
|
||||||
|
///
|
||||||
|
/// assert!(result.is_err());
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
pub fn borrow(&self) -> Ref<T> {
|
||||||
|
match BorrowRef::new(&self.borrow) {
|
||||||
|
Some(b) => Ref {
|
||||||
|
value: unsafe { &*self.value.get() },
|
||||||
|
borrow: b,
|
||||||
|
},
|
||||||
|
None => panic!("RefCell<T> already mutably borrowed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mutably borrows the wrapped value.
|
||||||
|
///
|
||||||
|
/// The borrow lasts until the returned `RefMut` exits scope. The value
|
||||||
|
/// cannot be borrowed while this borrow is active.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the value is currently borrowed.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::RefCell;
|
||||||
|
///
|
||||||
|
/// let c = RefCell::new(5);
|
||||||
|
///
|
||||||
|
/// *c.borrow_mut() = 7;
|
||||||
|
///
|
||||||
|
/// assert_eq!(*c.borrow(), 7);
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// An example of panic:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::RefCell;
|
||||||
|
/// use std::thread;
|
||||||
|
///
|
||||||
|
/// let result = thread::spawn(move || {
|
||||||
|
/// let c = RefCell::new(5);
|
||||||
|
/// let m = c.borrow();
|
||||||
|
///
|
||||||
|
/// let b = c.borrow_mut(); // this causes a panic
|
||||||
|
/// }).join();
|
||||||
|
///
|
||||||
|
/// assert!(result.is_err());
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
pub fn borrow_mut(&self) -> RefMut<T> {
|
||||||
|
match BorrowRefMut::new(&self.borrow) {
|
||||||
|
Some(b) => RefMut {
|
||||||
|
value: unsafe { &mut *self.value.get() },
|
||||||
|
borrow: b,
|
||||||
|
},
|
||||||
|
None => panic!("RefCell<T> already borrowed"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a reference to the underlying `UnsafeCell`.
|
||||||
|
///
|
||||||
|
/// This can be used to circumvent `RefCell`'s safety checks.
|
||||||
|
///
|
||||||
|
/// This function is `unsafe` because `UnsafeCell`'s field is public.
|
||||||
|
#[inline]
|
||||||
|
pub unsafe fn as_unsafe_cell(&self) -> &UnsafeCell<T> {
|
||||||
|
&self.value
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the underlying data.
|
||||||
|
///
|
||||||
|
/// This call borrows `RefCell` mutably (at compile-time) so there is no
|
||||||
|
/// need for dynamic checks.
|
||||||
|
#[inline]
|
||||||
|
pub fn get_mut(&mut self) -> &mut T {
|
||||||
|
unsafe {
|
||||||
|
&mut *self.value.get()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl<T: ?Sized> Send for RefCell<T> where T: Send {}
|
||||||
|
|
||||||
|
impl<T: Clone> Clone for RefCell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> RefCell<T> {
|
||||||
|
RefCell::new(self.borrow().clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Default> Default for RefCell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn default() -> RefCell<T> {
|
||||||
|
RefCell::new(Default::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized + PartialEq> PartialEq for RefCell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn eq(&self, other: &RefCell<T>) -> bool {
|
||||||
|
*self.borrow() == *other.borrow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized + Eq> Eq for RefCell<T> {}
|
||||||
|
|
||||||
|
impl<T: ?Sized + PartialOrd> PartialOrd for RefCell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn partial_cmp(&self, other: &RefCell<T>) -> Option<Ordering> {
|
||||||
|
self.borrow().partial_cmp(&*other.borrow())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn lt(&self, other: &RefCell<T>) -> bool {
|
||||||
|
*self.borrow() < *other.borrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn le(&self, other: &RefCell<T>) -> bool {
|
||||||
|
*self.borrow() <= *other.borrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn gt(&self, other: &RefCell<T>) -> bool {
|
||||||
|
*self.borrow() > *other.borrow()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn ge(&self, other: &RefCell<T>) -> bool {
|
||||||
|
*self.borrow() >= *other.borrow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized + Ord> Ord for RefCell<T> {
|
||||||
|
#[inline]
|
||||||
|
fn cmp(&self, other: &RefCell<T>) -> Ordering {
|
||||||
|
self.borrow().cmp(&*other.borrow())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BorrowRef<'b> {
|
||||||
|
borrow: &'b Cell<BorrowFlag>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> BorrowRef<'b> {
|
||||||
|
#[inline]
|
||||||
|
fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRef<'b>> {
|
||||||
|
match borrow.get() {
|
||||||
|
WRITING => None,
|
||||||
|
b => {
|
||||||
|
borrow.set(b + 1);
|
||||||
|
Some(BorrowRef { borrow: borrow })
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> Drop for BorrowRef<'b> {
|
||||||
|
#[inline]
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let borrow = self.borrow.get();
|
||||||
|
debug_assert!(borrow != WRITING && borrow != UNUSED);
|
||||||
|
self.borrow.set(borrow - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> Clone for BorrowRef<'b> {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> BorrowRef<'b> {
|
||||||
|
// Since this Ref exists, we know the borrow flag
|
||||||
|
// is not set to WRITING.
|
||||||
|
let borrow = self.borrow.get();
|
||||||
|
debug_assert!(borrow != UNUSED);
|
||||||
|
// Prevent the borrow counter from overflowing.
|
||||||
|
assert!(borrow != WRITING);
|
||||||
|
self.borrow.set(borrow + 1);
|
||||||
|
BorrowRef { borrow: self.borrow }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wraps a borrowed reference to a value in a `RefCell` box.
|
||||||
|
/// A wrapper type for an immutably borrowed value from a `RefCell<T>`.
|
||||||
|
///
|
||||||
|
/// See the [module-level documentation](index.html) for more.
|
||||||
|
pub struct Ref<'b, T: ?Sized + 'b> {
|
||||||
|
value: &'b T,
|
||||||
|
borrow: BorrowRef<'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b, T: ?Sized> Deref for Ref<'b, T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
self.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b, T: ?Sized> Ref<'b, T> {
|
||||||
|
/// Copies a `Ref`.
|
||||||
|
///
|
||||||
|
/// The `RefCell` is already immutably borrowed, so this cannot fail.
|
||||||
|
///
|
||||||
|
/// This is an associated function that needs to be used as
|
||||||
|
/// `Ref::clone(...)`. A `Clone` implementation or a method would interfere
|
||||||
|
/// with the widespread use of `r.borrow().clone()` to clone the contents of
|
||||||
|
/// a `RefCell`.
|
||||||
|
#[inline]
|
||||||
|
pub fn clone(orig: &Ref<'b, T>) -> Ref<'b, T> {
|
||||||
|
Ref {
|
||||||
|
value: orig.value,
|
||||||
|
borrow: orig.borrow.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Make a new `Ref` for a component of the borrowed data.
|
||||||
|
///
|
||||||
|
/// The `RefCell` is already immutably borrowed, so this cannot fail.
|
||||||
|
///
|
||||||
|
/// This is an associated function that needs to be used as `Ref::map(...)`.
|
||||||
|
/// A method would interfere with methods of the same name on the contents
|
||||||
|
/// of a `RefCell` used through `Deref`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::{RefCell, Ref};
|
||||||
|
///
|
||||||
|
/// let c = RefCell::new((5, 'b'));
|
||||||
|
/// let b1: Ref<(u32, char)> = c.borrow();
|
||||||
|
/// let b2: Ref<u32> = Ref::map(b1, |t| &t.0);
|
||||||
|
/// assert_eq!(*b2, 5)
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
pub fn map<U: ?Sized, F>(orig: Ref<'b, T>, f: F) -> Ref<'b, U>
|
||||||
|
where F: FnOnce(&T) -> &U
|
||||||
|
{
|
||||||
|
Ref {
|
||||||
|
value: f(orig.value),
|
||||||
|
borrow: orig.borrow,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b, T: ?Sized> RefMut<'b, T> {
|
||||||
|
/// Make a new `RefMut` for a component of the borrowed data, e.g. an enum
|
||||||
|
/// variant.
|
||||||
|
///
|
||||||
|
/// The `RefCell` is already mutably borrowed, so this cannot fail.
|
||||||
|
///
|
||||||
|
/// This is an associated function that needs to be used as
|
||||||
|
/// `RefMut::map(...)`. A method would interfere with methods of the same
|
||||||
|
/// name on the contents of a `RefCell` used through `Deref`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use std::cell::{RefCell, RefMut};
|
||||||
|
///
|
||||||
|
/// let c = RefCell::new((5, 'b'));
|
||||||
|
/// {
|
||||||
|
/// let b1: RefMut<(u32, char)> = c.borrow_mut();
|
||||||
|
/// let mut b2: RefMut<u32> = RefMut::map(b1, |t| &mut t.0);
|
||||||
|
/// assert_eq!(*b2, 5);
|
||||||
|
/// *b2 = 42;
|
||||||
|
/// }
|
||||||
|
/// assert_eq!(*c.borrow(), (42, 'b'));
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
pub fn map<U: ?Sized, F>(orig: RefMut<'b, T>, f: F) -> RefMut<'b, U>
|
||||||
|
where F: FnOnce(&mut T) -> &mut U
|
||||||
|
{
|
||||||
|
RefMut {
|
||||||
|
value: f(orig.value),
|
||||||
|
borrow: orig.borrow,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct BorrowRefMut<'b> {
|
||||||
|
borrow: &'b Cell<BorrowFlag>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> Drop for BorrowRefMut<'b> {
|
||||||
|
#[inline]
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let borrow = self.borrow.get();
|
||||||
|
debug_assert!(borrow == WRITING);
|
||||||
|
self.borrow.set(UNUSED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b> BorrowRefMut<'b> {
|
||||||
|
#[inline]
|
||||||
|
fn new(borrow: &'b Cell<BorrowFlag>) -> Option<BorrowRefMut<'b>> {
|
||||||
|
match borrow.get() {
|
||||||
|
UNUSED => {
|
||||||
|
borrow.set(WRITING);
|
||||||
|
Some(BorrowRefMut { borrow: borrow })
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wrapper type for a mutably borrowed value from a `RefCell<T>`.
|
||||||
|
///
|
||||||
|
/// See the [module-level documentation](index.html) for more.
|
||||||
|
pub struct RefMut<'b, T: ?Sized + 'b> {
|
||||||
|
value: &'b mut T,
|
||||||
|
borrow: BorrowRefMut<'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b, T: ?Sized> Deref for RefMut<'b, T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
self.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'b, T: ?Sized> DerefMut for RefMut<'b, T> {
|
||||||
|
#[inline]
|
||||||
|
fn deref_mut(&mut self) -> &mut T {
|
||||||
|
self.value
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,6 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
#![feature(as_unsafe_cell)]
|
|
||||||
|
|
||||||
extern crate app_units;
|
extern crate app_units;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate cssparser;
|
extern crate cssparser;
|
||||||
|
|
|
@ -28,7 +28,6 @@ use selectors::Element;
|
||||||
use selectors::matching::DeclarationBlock;
|
use selectors::matching::DeclarationBlock;
|
||||||
use selectors::parser::{AttrSelector, NamespaceConstraint};
|
use selectors::parser::{AttrSelector, NamespaceConstraint};
|
||||||
use smallvec::VecLike;
|
use smallvec::VecLike;
|
||||||
use std::cell::{Ref, RefCell, RefMut};
|
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ops::BitOr;
|
use std::ops::BitOr;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
@ -46,6 +45,7 @@ use style::parser::ParserContextExtraData;
|
||||||
#[allow(unused_imports)] // Used in commented-out code.
|
#[allow(unused_imports)] // Used in commented-out code.
|
||||||
use style::properties::parse_style_attribute;
|
use style::properties::parse_style_attribute;
|
||||||
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock};
|
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock};
|
||||||
|
use style::refcell::{Ref, RefCell, RefMut};
|
||||||
use style::restyle_hints::ElementSnapshot;
|
use style::restyle_hints::ElementSnapshot;
|
||||||
use style::selector_impl::ElementExt;
|
use style::selector_impl::ElementExt;
|
||||||
#[allow(unused_imports)] // Used in commented-out code.
|
#[allow(unused_imports)] // Used in commented-out code.
|
||||||
|
|
|
@ -69,5 +69,16 @@ licenses = [
|
||||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
"""
|
""",
|
||||||
|
|
||||||
|
"""\
|
||||||
|
// Copyright 2012-2014 The Rust Project Developers.
|
||||||
|
// See http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
""",
|
||||||
] # noqa: Indicate to flake8 that we do not want to check indentation here
|
] # noqa: Indicate to flake8 that we do not want to check indentation here
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue