Skip to content

Commit

Permalink
Ensure imports used in job bodies are considered used
Browse files Browse the repository at this point in the history
If a trait is imported, but only used in the job body (which is in its
own module and only in scope because of `use super::*;`), the compiler
warns that the import is unused. This might be a bug in the compiler,
but we can work around it by moving the generated `Job` impl out of the
generated module.

Fixes #6
  • Loading branch information
sgrif committed May 16, 2019
1 parent 0bfa81e commit 3932c5e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
22 changes: 22 additions & 0 deletions integration_tests/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,25 @@ fn env_can_have_any_name() {
runner.run_all_pending_jobs().unwrap();
runner.assert_no_failed_jobs().unwrap();
}

#[test]
#[forbid(unused_imports)]
fn test_imports_only_used_in_job_body_are_not_warned_as_unused() {
use std::io::prelude::*;

#[swirl::background_job]
fn uses_trait_import() -> Result<(), swirl::PerformError> {
let mut buf = Vec::new();
buf.write_all(b"foo")?;
let s = String::from_utf8(buf)?;
assert_eq!(s, "foo");
Ok(())
}

let runner = TestGuard::dummy_runner();
let conn = runner.connection_pool().get().unwrap();
uses_trait_import().enqueue(&conn).unwrap();

runner.run_all_pending_jobs().unwrap();
runner.assert_no_failed_jobs().unwrap();
}
20 changes: 10 additions & 10 deletions swirl_proc_macro/src/background_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ pub fn expand(item: syn::ItemFn) -> Result<TokenStream, Diagnostic> {
}
}

impl swirl::Job for Job {
type Environment = #env_type;
const JOB_TYPE: &'static str = stringify!(#name);

#fn_token perform(self, #env_pat: &Self::Environment) #return_type {
let Job { #(#arg_names),* } = self;
#(#body)*
}
}

mod #name {
use super::*;

Expand All @@ -38,16 +48,6 @@ pub fn expand(item: syn::ItemFn) -> Result<TokenStream, Diagnostic> {
#(#struct_def),*
}

impl swirl::Job for Job {
type Environment = #env_type;
const JOB_TYPE: &'static str = stringify!(#name);

#fn_token perform(self, #env_pat: &Self::Environment) #return_type {
let Job { #(#arg_names),* } = self;
#(#body)*
}
}

swirl::register_job!(Job);
}
};
Expand Down

0 comments on commit 3932c5e

Please sign in to comment.