【发布时间】:2015-03-23 21:33:05
【问题描述】:
为了学习 Rust 语言,我使用了一个旧的 C++ 库并尝试将其转换为 Rust。它使用了很多 C++11 闭包,我在翻译这些概念时遇到了一些困难。
在 C++ 中我有这样的东西:
// library.h
struct Event {
// just some data
};
class Object {
public:
// ...
std::function<void(Event&)>& makeFunc(std::string& s) {
return m_funcs[s];
}
// ...
private:
// ...
std::map<std::string, std::function<void(Event&)>> m_funcs;
// ...
};
// main.cpp using the library
int main()
{
Object foo;
foo.makeFunc("func1") = [&]{
// do stuff
};
return 0;
}
我遇到问题的部分是将函数正确存储在 Rust HashMap 集合中。我试过这个:
struct Event;
struct Object {
m_funcs : HashMap<String, FnMut(&Event)>
}
impl Object {
// send f as another parameter rather than try and return borrow
// compiler was complaining
fn makeFunc(&mut self, s : &str,f: FnMut(&Event)) {
self.m_funcs.insert(String::from_str(s), f);
}
}
但上面写着the trait core::marker::Sized is not implemented for the type 'for('r) core::ops::FnMut(&'r CreateEvent)'
这是有道理的,因为FnMut 是一个特征,因此在编译时没有已知的 HashMap 大小。所以我认为 hashmap 需要一个实际的指针而不是抽象类型。所以我把它改成这个
struct Object {
m_funcs : HashMap<String, Box<FnMut(&Event)>>
}
impl Object {
fn makeFunc(&mut self, s : &str, f: &FnMut(&Event)) {
self.m_funcs.insert(String::from_str(s), Box::new(f));
}
}
现在它在插入处显示the trait 'for('r) core::ops::Fn<(&'r CreateEvent,)>' is not implemented for the type '&for('r) core::ops::FnMut(&'r CreateEvent)' [E0277]。这个错误对我来说毫无意义。有人可以向我解释在 HashMap 中存储对非转义闭包的引用的正确方法吗?
【问题讨论】:
标签: rust