Closed
Description
Short summary:
This code holds a HashMap that contains "jobs", pushes them to be executed in tokio::spawn
and sends the return value into mspc::channel (Tokio), finally it produces more jobs, and returns them from the JoinHandle
to be appended back into the original HashMap mentioned.
I expected rust to let me know if this proceedure is legal (Appears so) by running cargo check
. It returns a compiler Error
EDIT: Corrected summary
use std::{
time::{Instant, Duration},
collections::HashMap,
};
use tokio::{
task::JoinHandle,
sync::mpsc
};
use async_trait::async_trait;
// Command run on (CRON)
#[async_trait]
pub trait CRON<R>: Sized {
/// Run function, and then append to parent if more jobs are needed
async fn exec(self) -> (R, Vec<Self>);
/// check if command should be ran
fn check(&self) -> bool;
}
pub struct Scheduler<T, R>
{
val_tx: mpsc::Sender<R>,
commands: HashMap<uuid::Uuid, T>,
handles: Vec<JoinHandle<Vec<T>>>
}
impl<T, R> Scheduler<T, R>
where
R: Sync + Send + Copy + 'static,
T: CRON<R> + Sync + Send + 'static,
{
// COMPILER ERROR - Start
fn run_tasks(&mut self) {
let commands = self.commands.clone();
for (id, command) in commands {
if command.check() {
let mut vtx = self.val_tx.clone();
match self.commands.remove(&id) {
Some(job) => self.handles.push(
tokio::spawn(async move {
let (v, jobs) = job.exec().await;
vtx.send(v).await;
jobs
})),
None => {} // Add error handle?
}
}
}
}
// COMPILER ERROR - End
fn new() -> (Self, mpsc::Receiver<R>) {
unimplemented!()
}
}
struct A {}
#[async_trait]
impl CRON<()> for A {
async fn exec(self) -> ((), Vec<Self>) {
((), vec![])
}
fn check(&self) -> bool {
true
}
}
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', src/librustc_typeck/check/method/suggest.rs:550:46
stack backtrace:
0: backtrace::backtrace::libunwind::trace
at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/libunwind.rs:86
1: backtrace::backtrace::trace_unsynchronized
at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/mod.rs:66
2: std::sys_common::backtrace::_print_fmt
at src/libstd/sys_common/backtrace.rs:78
3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
at src/libstd/sys_common/backtrace.rs:59
4: core::fmt::write
at src/libcore/fmt/mod.rs:1053
5: std::io::Write::write_fmt
at src/libstd/io/mod.rs:1428
6: std::sys_common::backtrace::_print
at src/libstd/sys_common/backtrace.rs:62
7: std::sys_common::backtrace::print
at src/libstd/sys_common/backtrace.rs:49
8: std::panicking::default_hook::{{closure}}
at src/libstd/panicking.rs:204
9: std::panicking::default_hook
at src/libstd/panicking.rs:224
10: rustc_driver::report_ice
11: <alloc::boxed::Box<F> as core::ops::function::Fn<A>>::call
at /rustc/75cf41afb468152611212271bae026948cd3ba46/src/liballoc/boxed.rs:1031
12: proc_macro::bridge::client::<impl proc_macro::bridge::Bridge>::enter::{{closure}}::{{closure}}
at /rustc/75cf41afb468152611212271bae026948cd3ba46/src/libproc_macro/bridge/client.rs:305
13: std::panicking::rust_panic_with_hook
at src/libstd/panicking.rs:474
14: rust_begin_unwind
at src/libstd/panicking.rs:378
15: core::panicking::panic_fmt
at src/libcore/panicking.rs:85
16: core::panicking::panic
at src/libcore/panicking.rs:52
17: core::ops::function::impls::<impl core::ops::function::FnMut<A> for &mut F>::call_mut
18: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T,I>>::from_iter
19: rustc_typeck::check::method::suggest::<impl rustc_typeck::check::FnCtxt>::report_method_error
20: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
21: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation_and_needs
22: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_coercable_to_type
23: rustc_typeck::check::FnCtxt::check_decl_local
24: rustc_typeck::check::FnCtxt::check_stmt
25: rustc_typeck::check::FnCtxt::check_block_with_expected
26: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
27: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation_and_needs
28: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_return_expr
29: rustc_typeck::check::check_fn
30: rustc::ty::context::GlobalCtxt::enter_local
31: rustc_typeck::check::typeck_tables_of
32: rustc::ty::query::__query_compute::typeck_tables_of
33: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::typeck_tables_of>::compute
34: rustc::dep_graph::graph::DepGraph::with_task_impl
35: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
36: rustc_typeck::check::typeck_tables_of
37: rustc::ty::query::__query_compute::typeck_tables_of
38: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::typeck_tables_of>::compute
39: rustc::dep_graph::graph::DepGraph::with_task_impl
40: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
41: rustc_typeck::collect::type_of::type_of
42: rustc::ty::query::__query_compute::type_of
43: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::type_of>::compute
44: rustc::dep_graph::graph::DepGraph::with_task_impl
45: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
46: rustc_hir::intravisit::walk_expr
47: rustc_hir::intravisit::walk_expr
48: rustc_hir::intravisit::walk_expr
49: rustc_hir::intravisit::walk_expr
50: rustc_hir::intravisit::walk_expr
51: rustc_hir::intravisit::walk_block
52: rustc_hir::intravisit::walk_expr
53: <rustc_typeck::collect::CollectItemTypesVisitor as rustc_hir::intravisit::Visitor>::visit_impl_item
54: rustc::hir::map::Map::visit_item_likes_in_module
55: rustc_typeck::collect::collect_mod_item_types
56: rustc::ty::query::__query_compute::collect_mod_item_types
57: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors for rustc::ty::query::queries::collect_mod_item_types>::compute
58: rustc::dep_graph::graph::DepGraph::with_task_impl
59: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
60: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::ensure_query
61: rustc_typeck::check_crate
62: rustc_interface::passes::analysis
63: rustc::ty::query::__query_compute::analysis
64: rustc::dep_graph::graph::DepGraph::with_task_impl
65: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt>::get_query
66: rustc::ty::context::tls::enter_global
67: rustc_interface::interface::run_compiler_in_existing_thread_pool
68: rustc_ast::attr::with_globals
query stack during panic:
#0 [typeck_tables_of] type-checking `scheduler::manager::Scheduler::<T, R>::run_tasks`
#1 [typeck_item_bodies] type-checking all item bodies
#2 [analysis] running analysis passes on this crate
end of query stack
Meta
rustc 1.43.0-nightly (75cf41a 2020-03-04)
binary: rustc
commit-hash: 75cf41a
commit-date: 2020-03-04
host: x86_64-unknown-linux-gnu
release: 1.43.0-nightly
LLVM version: 9.0