Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save federicomenaquintero/b479eafe88ba43b427269c6d8d1ffa91 to your computer and use it in GitHub Desktop.
Save federicomenaquintero/b479eafe88ba43b427269c6d8d1ffa91 to your computer and use it in GitHub Desktop.
How to cast a &trait to a &ConcreteType
// Trait for node implementations (bezier paths, circles, etc.)
pub trait NodeTrait {
fn set_atts (&self, ...);
fn draw (&self, ...);
}
// Node in the tree of SVG elements
pub struct Node {
node_type: NodeType,
parent: Option<Weak<Node>>,
children: RefCell<Vec<Rc<Node>>>,
state: *mut RsvgState,
node_impl: Box<NodeTrait>
}
// Wrapper for node types that are implemented in C; c_node_impl is the C-side struct
struct CNode {
c_node_impl: *const libc::c_void,
... pointers to C functions ...
}
impl NodeTrait for CNode {
... call the corresponding C functions from our NodeTrait methods ...
}
// This is the exported function that the C code calls to implement a node type
#[no_mangle]
pub extern fn rsvg_rust_cnode_new (...,
c_node_impl: *const libc::c_void,
...) -> *const Rc<Node> {
...
}
// The C code sometimes needs to get the c_node_impl *outside* of a NodeTrait method call
#[no_mangle]
pub extern fn rsvg_rust_cnode_get_impl (raw_node: *const Rc<Node>) -> *const libc::c_void {
assert! (!raw_node.is_null ());
let node: &Rc<Node> = unsafe { & *raw_node };
(&*node as &CNode).c_node_impl // <---- can't cast to the specific type like that!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment