我正在使用tokio-rs在Rust中构建一个服务,并且对这个技术堆栈感到满意.我现在正试图将包含写入的异步操作和借用检查器的困难时间联系起来.
我简化的最小代码示例如下:
extern crate futures; // 0.1.21
use futures::Future;
use std::{cell::RefCell, rc::Rc};
trait RequestProcessor {
fn prepare(&self) -> Box<Future<Item = (), Error = ()>>;
fn process(&mut self, request: String) -> Box<Future<Item = (), Error = ()>>;
}
struct Service {
processor: Rc<RefCell<RequestProcessor>>,
}
impl Service {
fn serve(&mut self, request: String) -> Box<Future<Item = (), Error = ()>> {
let processor_clone = self.processor.clone();
let result_fut = self
.processor
.borrow()
.prepare()
.and_then(move |_| processor_clone.borrow_mut().process(request));
Box::new(result_fut)
}
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
作为一个简短的总结,在异步准备步骤之后,我正在尝试运行另一个写入字段的异步操作 …
我认为以下简化的 C++11 代码应该是有效的。
unordered_map<string,string> test;
auto it = remove_if( test.begin(), test.end(),
[] (const decltype(test)::value_type &entry) { return true; } );
Run Code Online (Sandbox Code Playgroud)
但它无法使用 g++ 6.3 编译,抱怨 std::pair 的赋值运算符已删除,但 AFAIK 未删除该运算符。
/usr/include/c++/6/bits/stl_algo.h:868:16: error: use of deleted function ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=( ...
*__result = _GLIBCXX_MOVE(*__first);
Run Code Online (Sandbox Code Playgroud)
这是编译器/glibc 错误还是由于某些原因我看不到代码真的无效?
我有一个struct对象的集合.我想用特征对象的迭代器迭代集合,但是我不能为它创建一个合适的迭代器.我减少的测试代码是:
struct MyStruct {}
struct MyStorage(Vec<MyStruct>);
trait MyTrait {} // Dummy trait to demonstrate the problem
impl MyTrait for MyStruct {}
trait MyContainer {
fn items<'a>(&'a self) -> Box<Iterator<Item = &'a MyTrait> + 'a>;
}
impl MyContainer for MyStorage {
fn items<'a>(&'a self) -> Box<Iterator<Item = &'a MyTrait> + 'a> {
Box::new(self.0.iter())
}
}
Run Code Online (Sandbox Code Playgroud)
这会导致以下编译器错误:
error[E0271]: type mismatch resolving `<std::slice::Iter<'_, MyStruct> as std::iter::Iterator>::Item == &MyTrait`
--> src/main.rs:12:9
|
12 | Box::new(self.0.iter())
| ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `MyStruct`, found trait MyTrait
| …Run Code Online (Sandbox Code Playgroud) rust ×2
asynchronous ×1
c++11 ×1
future ×1
iterator ×1
mutable ×1
polymorphism ×1
rust-tokio ×1
stl ×1