Hi guys:
I'm a newbie, and this is probably the hardest code I have ever written. But it has a bug in it
fn main() {
let v = vec![Json::True, Json::Obj(vec![])];
let mut tree = Dag::new(&v);
let e = Editor::new(&mut tree);
let t = (e.tree.node)('t');
println!("{:?}", t.unwrap());
}
trait Ast<'a>: Sized {
fn node_function() -> Box<dyn Fn(char) -> Option<Self> + 'a>;
}
struct Dag<'a, Node: Ast<'a>> {
x: &'a Vec<Node>,
node: Box<dyn Fn(char) -> Option<Node> + 'a>,
}
impl<'a, Node: Ast<'a>> Dag<'a, Node> {
fn new(x: &'a Vec<Node>) -> Self {
Dag {
x,
node: Node::node_function(),
}
}
}
#[derive(Debug)]
enum Json<'a> {
True,
False,
Obj(Vec<&'a Json<'a>>),
}
impl<'a> Ast<'a> for Json<'a> {
fn node_function() -> Box<dyn Fn(char) -> Option<Self> + 'a> {
Box::new(move |x| match x {
't' => Some(Json::True),
'f' => Some(Json::False),
_ => None,
})
}
}
struct Editor<'a, Node: Ast<'a>> {
tree: &'a mut Dag<'a, Node>,
}
impl<'a, Node: Ast<'a> + 'a> Editor<'a, Node> {
fn new(tree: &'a mut Dag<'a, Node>) -> Editor<'a, Node> {
Editor { tree }
}
}
I'm working on this code, the general idea is: I want Struct Dag
to store a closure
, and for different data type, closure
will be different.
The problem happens when I mutable borrow Dag
. Compiler doesn't provide much useful information.