got node handlers on enum rails, making box not necessary and fixing

potential memory losses
This commit is contained in:
Rendo 2025-11-08 19:08:21 +05:00
commit 42253aa6e3
2 changed files with 16 additions and 57 deletions

View file

@ -6,17 +6,14 @@ mod node_modifier;
pub struct Node {
children: Vec<Node>,
handler: Box<dyn NodeHandler>,
handler: NodeHandler,
}
impl Node {
pub fn new<T>(children: Option<Vec<Node>>, handler: T) -> Self
where
T: NodeHandler + 'static,
{
pub fn new(children: Option<Vec<Node>>, handler: NodeHandler) -> Self {
Self {
children: children.unwrap_or(vec![]),
handler: Box::new(handler),
handler: handler,
}
}
pub fn get_value(&self) -> f64 {
@ -36,19 +33,19 @@ impl Node {
pub fn number(n: f64) -> Node {
Node {
children: vec![],
handler: Box::new(NodeNumber::new(Some(n))),
handler: NodeHandler::Number { number: n },
}
}
pub fn function(func: fn(Vec<f64>) -> f64) -> Node {
Node {
children: vec![],
handler: Box::new(NodeFunction::new(func)),
handler: NodeHandler::Function { function: func },
}
}
pub fn variable(getter: fn() -> f64) -> Node {
Node {
children: vec![],
handler: Box::new(NodeVariable::new(getter)),
handler: NodeHandler::Variable { getter },
}
}
}