Make node references more Rusty
Currently we have this:
-
NodeTrait
is the simple trait that node implementations must provide. It hasimpl Downcast
so we can have... -
... a
Node.with_impl()
method. When code needs to call a node's concrete implementation, they pass a closure towith_impl()
. This is rather awkward. -
We define a type alias
type RsvgNode = Rc<Node>
, and pass around&RsvgNode
everywhere. The C code obtains*const RsvgNode
, and the corresponding reference counting is explained here. -
Functions in
NodeTrait
get passed anode: &RsvgNode
argument in addition to&self
; this feels a bit strange. For example, this is howNode.draw()
callsNodeTrait.draw()
.
These things are inconvenient/weird:
-
Having to carry around
&RsvgNode
in addition to&self
. -
Having to call
with_impl()
just to access the node implementation's concrete type. -
We clone and box on every call to
rsvg_node_ref()
from the C code. I think this was written beforeRc.into_raw()
was available? We could do something similar torsvg_paint_server_ref()
. However, I'd like to keep the C prototypes forrsvg_node_ref/unref
as they are; they make the C code more robust against using a node after unreffing it.
Could we have something closer to Kuchiki's implementation of tree nodes? What it does with a Node
and a NodeRef(Rc<Node>)
is interesting. I don't quite like its NodeData
enum with variants for every concrete node type. Maybe we should still have boxed trait objects for those; I'm not sure.