[librsvg] node.rs: Add a Node::with_impl() method, to access the NodeTrait impl out of band
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] node.rs: Add a Node::with_impl() method, to access the NodeTrait impl out of band
- Date: Mon, 27 Feb 2017 22:52:16 +0000 (UTC)
commit 1112a20c355043cb962b0f3d4c89ee4c0e3978ae
Author: Federico Mena Quintero <federico gnome org>
Date: Mon Feb 27 16:35:46 2017 -0600
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.
rust/Cargo.lock | 7 +++++++
rust/Cargo.toml | 4 ++++
rust/src/lib.rs | 7 +++++++
rust/src/node.rs | 14 +++++++++++++-
4 files changed, 31 insertions(+), 1 deletions(-)
---
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 848b400..ef615c8 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -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)",
@@ -53,6 +54,11 @@ dependencies = [
]
[[package]]
+name = "downcast-rs"
+version = "1.0.0"
+source = "git+https://github.com/marcianx/downcast-rs#5ace99daf704b91f2eeab366b67b78b368d51884"
+
+[[package]]
name = "glib"
version = "0.1.1"
source = "git+https://github.com/gtk-rs/glib#91f2ceb47cdc2d3b209ce3b6c96e3de928223842"
@@ -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>"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 9f9b6f6..25eb419 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -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"]
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index ae2e5b2..8b7fcd4 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -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,
diff --git a/rust/src/node.rs b/rust/src/node.rs
index 6cab60a..f845871 100644
--- a/rust/src/node.rs
+++ b/rust/src/node.rs
@@ -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" {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]