-
Notifications
You must be signed in to change notification settings - Fork 1
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
166ef4b
commit ef71c53
Showing
2 changed files
with
105 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use proc_macro2::{Ident, Span}; | ||
use syn::{punctuated::Punctuated, FnArg, Generics, ReturnType, Token}; | ||
|
||
use crate::AsyncSignature; | ||
|
||
pub struct Sync; | ||
|
||
pub struct Async(pub(super) Option<AsyncSignature>); | ||
|
||
pub trait Kind { | ||
fn asyncness() -> Option<Token![async]>; | ||
|
||
fn transform_ident(ident: Ident) -> Ident { | ||
ident | ||
} | ||
|
||
fn transform_constness(constness: Option<Token![const]>) -> Option<Token![const]> { | ||
constness | ||
} | ||
|
||
fn transform_generics(&mut self, generics: Generics) -> Generics { | ||
generics | ||
} | ||
|
||
fn transform_inputs( | ||
&mut self, | ||
inputs: Punctuated<FnArg, Token![,]>, | ||
) -> Punctuated<FnArg, Token![,]> { | ||
inputs | ||
} | ||
|
||
fn transform_output(&mut self, output: ReturnType) -> ReturnType { | ||
output | ||
} | ||
} | ||
|
||
impl Kind for Sync { | ||
fn asyncness() -> Option<Token![async]> { | ||
None | ||
} | ||
} | ||
|
||
impl Kind for Async { | ||
fn asyncness() -> Option<Token![async]> { | ||
Some(Token)) | ||
} | ||
|
||
fn transform_ident(ident: Ident) -> Ident { | ||
Ident::new(&format!("{ident}_async"), ident.span()) | ||
} | ||
|
||
fn transform_generics(&mut self, generics: Generics) -> Generics { | ||
if let Some(alt_generics) = self | ||
.0 | ||
.as_mut() | ||
.map(|AsyncSignature { generics, .. }| std::mem::take(generics)) | ||
{ | ||
alt_generics | ||
} else { | ||
generics | ||
} | ||
} | ||
|
||
fn transform_inputs( | ||
&mut self, | ||
inputs: Punctuated<FnArg, Token!(,)>, | ||
) -> Punctuated<FnArg, Token!(,)> { | ||
if let Some(alt_inputs) = self | ||
.0 | ||
.as_mut() | ||
.map(|AsyncSignature { inputs, .. }| std::mem::take(inputs)) | ||
{ | ||
alt_inputs | ||
} else { | ||
inputs | ||
} | ||
} | ||
|
||
fn transform_output(&mut self, output: ReturnType) -> ReturnType { | ||
if let Some(alt_output) = self.0.as_mut().map(|AsyncSignature { output, .. }| { | ||
let mut default = ReturnType::Default; | ||
std::mem::swap(output, &mut default); | ||
default | ||
}) { | ||
alt_output | ||
} else { | ||
output | ||
} | ||
} | ||
} |
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