我想使用Arc
多次对Vec<u8>
进行操作:
use futures::{
executor::{block_on, ThreadPool},
task::SpawnExt,
}; // 0.3.4
use std::{pin::*, sync::Arc};
fn foo(b: Arc<Vec<u8>>) {
println!("{:?}", b);
}
#[test]
fn pin_test() {
let v = Arc::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let mut pool = ThreadPool::new().unwrap();
for _ in 0..10 {
let v1 = v.clone();
let handle = pool
.spawn_with_handle(async {
foo(v1);
})
.unwrap();
block_on(handle);
}
}
我希望能够Pin
Vec<u8>
use futures::{
executor::{block_on, ThreadPool},
task::SpawnExt,
}; // 0.3.4
use std::{pin::*, sync::Arc};
fn foo(b: &[u8]) {
println!("{:?}", b);
}
#[test]
fn pin_test() {
let v = Pin::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let mut pool = ThreadPool::new().unwrap();
for _ in 0..10 {
let v1 = v.clone();
let handle = pool
.spawn_with_handle(async {
foo(&*v1);
})
.unwrap();
block_on(handle);
}
}
这给出了错误:
error[E0597]: `v1` does not live long enough
--> src/lib.rs:19:23
|
18 | .spawn_with_handle(async {
| ________________________________-_____-
| |________________________________|
| ||
19 | || foo(&*v1);
| || ^^ borrowed value does not live long enough
20 | || })
| || -
| ||_____________|
| |______________value captured here by generator
| argument requires that `v1` is borrowed for `'static`
...
23 | }
| - `v1` dropped here while still borrowed
我理解Pin
应该将Vec
数据固定到特定点,以便所有调用都可以引用相同的数据。使用Pin
的正确方法是什么,以便我可以传递对foo()
的引用?
我正在使用 Rust 1.39。
您缺少move
.
改变
let handle = pool
.spawn_with_handle(async {
foo(&*v1);
})
.unwrap();
自
let handle = pool
.spawn_with_handle(async move {
// ^^^^
foo(&*v1);
})
.unwrap();