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;

use {
    WriteBuffer,
    WriteOptions,
};

use super::Path;

#[derive(Debug)]
pub struct RcPath(pub Rc<RefCell<Path>>);

impl RcPath {
    pub fn new(p: Path) -> RcPath {
        RcPath(Rc::new(RefCell::new(p)))
    }

    pub fn borrow(&self) -> Ref<Path> {
        (*self.0).borrow()
    }

    pub fn borrow_mut(&self) -> RefMut<Path> {
        (*self.0).borrow_mut()
    }
}

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

impl PartialEq for RcPath {
    fn eq(&self, other: &RcPath) -> 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
}

impl WriteBuffer for RcPath {
    fn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>) {
        (*self.0).borrow().write_buf_opt(opt, buf)
    }
}