rust 找出闭包注释/签名
rust find out closure annotation / signature
问题:
如何找到闭包的签名?具体来说,我如何确定闭包是否在引用我的局部变量?
背景:
我认为 pool.install
中使用的闭包应该对我的局部变量 book
进行不可变引用,但编译器没有抱怨。所以我试图弄清楚 pool.install
中使用的闭包签名,认为这些信息可能会帮助我理解正在发生的事情。
let pool = rayon::ThreadPoolBuilder::new().build().unwrap();
loop {
let book = get_book.select_next_some().await;
while mut_tasks.is_empty() {
select! {
task = rx_grammar_req.select_next_some() => {
match task.req {
Req::M {..} => mut_tasks.push(ready(task)),
Req::R {..} => {
// Q: how do I figure out the closure's signature?
pool.install(|| run_ref_task(&book, task));
}
};
},
}
}
get_book.push(ready(book));
let mut queries = FuturesUnordered::new();
loop {
select! {
book = queries.select_next_some() => {
get_book.push(ready(book));
if mut_tasks.is_empty() && queries.is_empty() { break; }
},
book = get_book.select_next_some() => {
if let Some(task) = mut_tasks.next().await {
queries.push(run_mut_task(book, task));
} else {
get_book.push(ready(book));
if queries.is_empty() { break; }
}
}
}
}
}
闭包的签名由 ThreadPool::install
:
的函数签名决定
pub fn install<OP, R>(&self, op: OP) -> R
where
OP: FnOnce() -> R + Send,
R: Send { ... }
因此,闭包的签名是 () -> R
,其中 R
是 run_ref_task(&book, task)
的输出。
我假设您实际上对闭包的签名不感兴趣,而是对闭包捕获的内容感兴趣。在这种情况下,book
通过引用捕获,而 task
通过值捕获或复制(如果其类型实现了 Copy
.
)
有关闭包捕获模式和 FnOnce
/ FnMut
/ Fn
的进一步阅读可在此处找到:
https://doc.rust-lang.org/reference/types/closure.html#capture-modes
https://doc.rust-lang.org/reference/types/closure.html#call-traits-and-coercions
问题:
如何找到闭包的签名?具体来说,我如何确定闭包是否在引用我的局部变量?
背景:
我认为 pool.install
中使用的闭包应该对我的局部变量 book
进行不可变引用,但编译器没有抱怨。所以我试图弄清楚 pool.install
中使用的闭包签名,认为这些信息可能会帮助我理解正在发生的事情。
let pool = rayon::ThreadPoolBuilder::new().build().unwrap();
loop {
let book = get_book.select_next_some().await;
while mut_tasks.is_empty() {
select! {
task = rx_grammar_req.select_next_some() => {
match task.req {
Req::M {..} => mut_tasks.push(ready(task)),
Req::R {..} => {
// Q: how do I figure out the closure's signature?
pool.install(|| run_ref_task(&book, task));
}
};
},
}
}
get_book.push(ready(book));
let mut queries = FuturesUnordered::new();
loop {
select! {
book = queries.select_next_some() => {
get_book.push(ready(book));
if mut_tasks.is_empty() && queries.is_empty() { break; }
},
book = get_book.select_next_some() => {
if let Some(task) = mut_tasks.next().await {
queries.push(run_mut_task(book, task));
} else {
get_book.push(ready(book));
if queries.is_empty() { break; }
}
}
}
}
}
闭包的签名由 ThreadPool::install
:
pub fn install<OP, R>(&self, op: OP) -> R
where
OP: FnOnce() -> R + Send,
R: Send { ... }
因此,闭包的签名是 () -> R
,其中 R
是 run_ref_task(&book, task)
的输出。
我假设您实际上对闭包的签名不感兴趣,而是对闭包捕获的内容感兴趣。在这种情况下,book
通过引用捕获,而 task
通过值捕获或复制(如果其类型实现了 Copy
.
有关闭包捕获模式和 FnOnce
/ FnMut
/ Fn
的进一步阅读可在此处找到:
https://doc.rust-lang.org/reference/types/closure.html#capture-modes https://doc.rust-lang.org/reference/types/closure.html#call-traits-and-coercions