svgdom 0.7.0

Library to represent an SVG as a DOM.
Documentation
// 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/.

use std::cell::{Ref, RefMut, RefCell};
use std::rc::Rc;

/// A reference counted vector.
#[derive(Debug)]
pub struct RcVec<T>(pub Rc<RefCell<Vec<T>>>);

impl<T> RcVec<T> {
    /// Creates a new `RcVec` from a `Vec`.
    pub fn new(p: Vec<T>) -> RcVec<T> {
        RcVec(Rc::new(RefCell::new(p)))
    }

    /// Returns a borrowed content.
    pub fn borrow(&self) -> Ref<Vec<T>> {
        (*self.0).borrow()
    }

    /// Returns a mutability borrowed content.
    pub fn borrow_mut(&self) -> RefMut<Vec<T>> {
        (*self.0).borrow_mut()
    }
}

/// Cloning a `RcVec` only increments a reference count. It does not copy the data.
impl<T> Clone for RcVec<T> {
    fn clone(&self) -> RcVec<T> {
        RcVec(self.0.clone())
    }
}

impl<T> PartialEq for RcVec<T> {
    fn eq(&self, other: &RcVec<T>) -> bool {
        same_rc(&self.0, &other.0)
    }
}

// TODO: move to Rc::ptr_eq (since 1.17) when we drop 1.13 version support
fn same_rc<T>(a: &Rc<T>, b: &Rc<T>) -> bool {
    let a: *const T = &**a;
    let b: *const T = &**b;
    a == b
}