node.rs: Add a Node::with_impl() method, to access the NodeTrait impl out of band

This is a bit sketchy, because we use the downcast-rs crate to actually
be able to downcast a our "node_impl: Box<NodeTrait>" field into the
concrete implementation type.  At first we'll use this in the marker.rs
implementation, as markers are not rendered from the normal ::draw()
method.  Maybe we'll find a way to make this cleaner at some point.
parent 70f390d8
......@@ -5,6 +5,7 @@ dependencies = [
"bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cairo-rs 0.1.1 (git+https://github.com/gtk-rs/cairo.git)",
"cairo-sys-rs 0.3.2 (git+https://github.com/gtk-rs/cairo.git)",
"downcast-rs 1.0.0 (git+https://github.com/marcianx/downcast-rs)",
"glib 0.1.1 (git+https://github.com/gtk-rs/glib)",
"libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"nom 2.1.0 (git+https://github.com/federicomenaquintero/nom.git)",
......@@ -52,6 +53,11 @@ dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "downcast-rs"
version = "1.0.0"
source = "git+https://github.com/marcianx/downcast-rs#5ace99daf704b91f2eeab366b67b78b368d51884"
[[package]]
name = "glib"
version = "0.1.1"
......@@ -111,6 +117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum c_vec 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0059f5a658f62a4bd3937a7addc52ccfda144b75cce7a92b187e528629cdc507"
"checksum cairo-rs 0.1.1 (git+https://github.com/gtk-rs/cairo.git)" = "<none>"
"checksum cairo-sys-rs 0.3.2 (git+https://github.com/gtk-rs/cairo.git)" = "<none>"
"checksum downcast-rs 1.0.0 (git+https://github.com/marcianx/downcast-rs)" = "<none>"
"checksum glib 0.1.1 (git+https://github.com/gtk-rs/glib)" = "<none>"
"checksum glib-sys 0.3.2 (git+https://github.com/gtk-rs/sys)" = "<none>"
"checksum gobject-sys 0.3.2 (git+https://github.com/gtk-rs/sys)" = "<none>"
......
......@@ -28,6 +28,10 @@ version = "0.1.1"
git = "https://github.com/federicomenaquintero/nom.git"
features = ["verbose-errors"]
[dependencies.downcast-rs]
git = "https://github.com/marcianx/downcast-rs"
version = "1.0.0"
[lib]
name = "rsvg_internals"
crate-type = ["staticlib"]
......@@ -4,6 +4,9 @@ extern crate bitflags;
#[macro_use]
extern crate nom;
#[macro_use]
extern crate downcast_rs;
pub use aspect_ratio::{
FitMode,
AlignMode,
......@@ -43,6 +46,10 @@ pub use length::{
rsvg_length_hand_normalize,
};
pub use marker::{
rsvg_node_marker_new,
};
pub use node::{
rsvg_node_get_type,
rsvg_node_get_parent,
......
......@@ -5,6 +5,8 @@ use std::rc::Weak;
use std::cell::RefCell;
use std::ptr;
use downcast_rs::*;
use drawing_ctx::RsvgDrawingCtx;
use drawing_ctx;
......@@ -25,12 +27,14 @@ pub type RsvgNode = Rc<Node>;
*/
pub enum RsvgCNodeImpl {}
pub trait NodeTrait {
pub trait NodeTrait: Downcast {
fn set_atts (&self, node: &RsvgNode, handle: *const RsvgHandle, pbag: *const RsvgPropertyBag);
fn draw (&self, node: &RsvgNode, draw_ctx: *const RsvgDrawingCtx, dominate: i32);
fn get_c_impl (&self) -> *const RsvgCNodeImpl;
}
impl_downcast! (NodeTrait);
pub struct Node {
node_type: NodeType,
parent: Option<Weak<Node>>, // optional; weak ref to parent
......@@ -133,6 +137,14 @@ impl Node {
pub fn get_c_impl (&self) -> *const RsvgCNodeImpl {
self.node_impl.get_c_impl ()
}
pub fn with_impl<T: NodeTrait, F: FnOnce (&T)> (&self, f: F) {
if let Some (t) = (&self.node_impl).downcast_ref::<T> () {
f (t);
} else {
panic! ("could not downcast");
}
}
}
extern "C" {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment