I'm trying to use the ego_tree crate and I'm getting an error, but I'm not sure how to fix it.
Also, I seem to be getting different errors for the same problem, depending on where my code is located.
I wrote a function accepting a NodeMut as a parameter, and I want to get the non-mutable NodeRef. NodeMut seems to implement the Into trait, which has the into
function which does this, but I get compile errors when I try to call it.
I've simplified my problem to this:
extern crate ego_tree;
use std::path::PathBuf;
use ego_tree::{Tree, NodeMut, NodeRef};
enum DirEntry {
Folder(PathBuf),
File { path: PathBuf, content: String },
}
fn f() {
let mut t = Tree::new(DirEntry::Folder(PathBuf::new()));
let mut nodemut = t.root_mut();
let node : NodeRef<DirEntry> = nodemut.into();
g(&mut nodemut);
}
fn g(parent_dir: &mut NodeMut<DirEntry>) {
let node : &NodeRef<_> = parent_dir.into();
}
I get this error with the line in g():
error[E0277]: the trait bound &ego_tree::NodeRef<'_, _>: std::convert::From<&mut ego_tree::NodeMut<'_, DirEntry>> is not satisfied
So, since NodeRef doesn't implement From, is there any way to use the into method at all?
The second thing is, that if I got an entirely different error message when I tried the same thing in my original code:
A little more realistic version of my original code is:
fn create(parent_dir: &mut NodeMut<DirEntry>) {
// Next line gets me the error:
// "the type of this value must be known in this context"
let noderef = parent_dir.into() as &NodeRef<DirEntry>;
let val = noderef.value();
if let &DirEntry::Folder(ref parent_path) = val {
// Do something with parent_path
let name = "abc";
parent_dir.append(DirEntry::File { path: parent_path.join(name), content: String::from("") });
}
}
However, in this code, I get the different error "the type of this value must be known in this context" on what I think is a line doing the same thing, even though I specified what I want to convert the return type to. I'm mentioning this because I was stuck with this error for several hours.
I'm sure it's something I don't understand about the language, but what's the difference between the problematic line in g() and in create()?