Best practice for struct field inheritance in the current rust version


#1

So to be short this is how my current solution for some sort of inheritance goes in rust for a problem I need to solve

pub struct NodeBase{
	id: usize,
	dims: [SymPolynomial; 2]
	name: String,
	children: Vec<usize>,
	gradient_level: u8,
	inline: bool,
}

pub struct NumberNode{
	base: NodeBase,
	value: f64
}

pub struct ConstInputNode{
	base: NodeBase,
}

pub struct ParameterNode {
    base: NodeBase,
    grad_child: usize,
}

pub struct ConstDerivedNode{
	base: NodeBase,
	op: Operator
}

pub struct ParameterDerivedNode{
	base: NodeBase,
	op: Operator,
	grad_child: usize,
	grad_parent: usize
}

enum ComputeNode{
	Number(NumberNode),
	ConstInput(ConstInputNode),
	Parameter(ParameterNode),
	ConstDerived(ConstDerivedNode),
	ParameterDerived(ParameterDerivedNode)
}

Now I’m well aware there is a significant discussion about resolving this and the “Efficient code reuse” challenge with several RFCs already. Just wondering what is the “best practice” current suggestion for such situation? Is my code okay or is that really bad? Just want to educate myself on the topic.


What the best strategy to deals with C Union FFI?
#2

I generally embed the substructs into the enum itself and put the enum on the inside:

pub struct ComputeNode {
    id: usize,
    dims: [SymPolynomial; 2],
    name: String,
    children: Vec<usize>,
    gradient_level: u8,
    inline: bool,
    data: NodeData,
}

enum NodeData {
    Number {
        value: f64
    },
    ConstInput,
    Parameter {
        grad_child: usize
    },
    ConstDerived {
        op: Operator
    },
    ParameterDerived {
        op: Operator,
        grad_child: usize,
        grad_parent: usize
    },
}