Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename crate name customization option to 'crate' #17

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Support package name customization in #[stuck::main] and #[stuck::test]
- Support crate name customization in #[stuck::main] and #[stuck::test]

## [0.1.5] - 2022-04-09
### Added
Expand Down
20 changes: 10 additions & 10 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ use quote::quote;

#[derive(Default)]
struct Configuration {
package: Option<Ident>,
crate_name: Option<Ident>,
parallelism: Option<usize>,
}

impl Configuration {
fn set_package(&mut self, lit: syn::Lit) -> Result<(), syn::Error> {
fn set_crate_name(&mut self, lit: syn::Lit) -> Result<(), syn::Error> {
let span = lit.span();
if self.package.is_some() {
return Err(syn::Error::new(span, "package name already set"));
if self.crate_name.is_some() {
return Err(syn::Error::new(span, "crate name already set"));
}
if let syn::Lit::Str(s) = lit {
if let Ok(path) = s.parse::<syn::Path>() {
if let Some(ident) = path.get_ident() {
self.package = Some(ident.clone());
self.crate_name = Some(ident.clone());
return Ok(());
}
}
return Err(syn::Error::new(span, format!("invalid package name: {}", s.value())));
return Err(syn::Error::new(span, format!("invalid crate name: {}", s.value())));
}
Err(syn::Error::new(span, "invalid package name"))
Err(syn::Error::new(span, "invalid crate name"))
}

fn set_parallelism(&mut self, lit: syn::Lit) -> Result<(), syn::Error> {
Expand Down Expand Up @@ -56,7 +56,7 @@ fn parse_config(args: syn::AttributeArgs) -> Result<Configuration, syn::Error> {
.to_string();
match name.as_str() {
"parallelism" => config.set_parallelism(name_value.lit)?,
"package" => config.set_package(name_value.lit)?,
"crate" => config.set_crate_name(name_value.lit)?,
_ => return Err(syn::Error::new_spanned(&name_value, "unknown attribute name")),
}
},
Expand Down Expand Up @@ -99,7 +99,7 @@ fn generate(is_test: bool, attr: TokenStream, item: TokenStream) -> TokenStream
quote! {}
};

let package_name = config.package.unwrap_or_else(|| Ident::new("stuck", Span::call_site()));
let crate_name = config.crate_name.unwrap_or_else(|| Ident::new("stuck", Span::call_site()));
let parallelism = config.parallelism.unwrap_or(0);
let result = quote! {
#header
Expand All @@ -109,7 +109,7 @@ fn generate(is_test: bool, attr: TokenStream, item: TokenStream) -> TokenStream
#body
}

let mut builder = #package_name::runtime::Builder::default();
let mut builder = #crate_name::runtime::Builder::default();
if #parallelism != 0 {
builder.parallelism(#parallelism);
}
Expand Down
2 changes: 1 addition & 1 deletion src/coroutine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ mod tests {

use crate::{coroutine, task};

#[crate::test(package = "crate")]
#[crate::test(crate = "crate")]
fn yield_now() {
let five = task::spawn(|| {
let value = Rc::new(Cell::new(0));
Expand Down
2 changes: 1 addition & 1 deletion src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ mod tests {

use crate::{coroutine, task};

#[crate::test(package = "crate", parallelism = 1)]
#[crate::test(crate = "crate", parallelism = 1)]
fn yield_now() {
let five = task::spawn(|| {
let shared_value = Arc::new(Mutex::new(0));
Expand Down