-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a077cf
commit 5ec1a18
Showing
5 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
use crate::attributes::{Attribute, MethodAttribute}; | ||
use crate::config::{build_config, build_enum_doc, build_enum_name}; | ||
use crate::expand::Context; | ||
use crate::tokens::IndexedField; | ||
|
||
build_enum_name! { ConfigName, "take_{}" } | ||
build_enum_doc! { | ||
ConfigDoc, | ||
"Replaces the field `{1}` of [`{0}`] with its default value.", | ||
} | ||
|
||
build_config! { | ||
Config, | ||
(name, ConfigName, true), | ||
(doc, ConfigDoc, false), | ||
} | ||
|
||
pub fn struct_method_take( | ||
context: &Context, | ||
indexed_field: &IndexedField, | ||
attribute: &Attribute, | ||
method_attr: &MethodAttribute, | ||
) -> syn::Result<TokenStream> { | ||
let config = Config::new(context, attribute, indexed_field)?; | ||
|
||
let doc = &config.doc; | ||
let keywords = method_attr.keywords(); | ||
let ty = &indexed_field.ty; | ||
let method_ident = &config.name; | ||
let field_ident = indexed_field.as_token(); | ||
|
||
Ok(quote! { | ||
#[doc = #doc] | ||
#keywords fn #method_ident (&mut self) -> #ty where #ty: ::std::default::Default { | ||
::core::mem::take(&mut self.#field_ident) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters