-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat!: Implement support for generic params #17
Draft
DanikVitek
wants to merge
18
commits into
scouten:main
Choose a base branch
from
DanikVitek:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1e96c8a
WIP: Implementing support for generic params; Extendable type-safe re…
DanikVitek 8961b66
Implement support for generic type parameters. Improve error messages…
DanikVitek c6183cc
Made macros work with traits. WIP: Make macros work with trait impls.…
DanikVitek 9824a06
Remove need for `Either`. WIP: Make macros work with trait impls. TOD…
DanikVitek 641705f
Make macros work with trait impls. WIP: Make code DRYer
DanikVitek 420a69b
WIP: Make code DRYer
DanikVitek 166ef4b
Make code DRYer; rearrange the modules
DanikVitek ef71c53
rearrange the modules
DanikVitek aab0329
Update semantic version
DanikVitek 6d1b01e
fix: Update semantic version
DanikVitek a3e3e96
Add support for `cfg_attr`
DanikVitek 24115a4
Review the approach to `cfg_attr` and instead allow to specify attrib…
DanikVitek 65ade70
Use custom keyword for `async_signature`
DanikVitek dd7c739
Rework attribute args to be able to split trait into sync and async v…
DanikVitek 4c74414
Fix parser
DanikVitek e2a5bc4
revert version diff
DanikVitek 6c34135
make inapplicable to non-trait impls
DanikVitek 91b252d
Add support for separate annotations for sync functions via `sync_sig…
DanikVitek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "async-generic" | ||
version = "1.1.2" | ||
version = "2.0.0" | ||
description = "Write code that can be both async and synchronous without duplicating it." | ||
authors = ["Eric Scouten <[email protected]>"] | ||
license = "MIT OR Apache-2.0" | ||
|
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,109 @@ | ||
use proc_macro2::{Ident, Span}; | ||
use syn::{punctuated::Punctuated, Attribute, FnArg, Generics, ReturnType, Token}; | ||
|
||
use crate::AsyncSignature; | ||
|
||
pub struct Sync; | ||
|
||
pub struct Async(pub(super) Option<AsyncSignature>); | ||
|
||
pub trait Kind { | ||
fn transform_constness(constness: Option<Token![const]>) -> Option<Token![const]> { | ||
constness | ||
} | ||
|
||
fn asyncness() -> Option<Token![async]>; | ||
|
||
fn extend_attrs(&mut self, attrs: Vec<Attribute>) -> Vec<Attribute> { | ||
attrs | ||
} | ||
|
||
fn transform_ident(ident: Ident) -> Ident { | ||
ident | ||
} | ||
|
||
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 transform_constness(_constness: Option<Token!(const)>) -> Option<Token!(const)> { | ||
None // TODO: retutn `constness` when `const async fn` is stabilized | ||
} | ||
|
||
fn asyncness() -> Option<Token![async]> { | ||
Some(Token![async](Span::call_site())) | ||
} | ||
|
||
fn extend_attrs(&mut self, mut attrs: Vec<Attribute>) -> Vec<Attribute> { | ||
if let Some(alt_attrs) = self | ||
.0 | ||
.as_mut() | ||
.map(|AsyncSignature { attributes, .. }| std::mem::take(attributes)) | ||
{ | ||
attrs.extend(alt_attrs); | ||
} | ||
attrs | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this diff. This project uses
release-plz
(https://release-plz.dev/docs) to manage versioning and that infrastructure will automatically do the necessary updates at the time based on how the PR is labeled.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also questioning whether this PR meets the criteria for bumping to a 2.0. Under the rules of semantic versioning, that should only happen if there's an incompatible API change to the library. I see changes you've made to the
async_signature
parameter of the macro, but do the break compatibility? If not, let's make this a 1.2.0 release (which would involve removing the!
from the PR title as I've amended it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I've described in the description of the commit 6d1b01e, the changes are indeed breaking.
async_signature(a: B)
means, that theasync
function does not have a return type, while previously this meta tag affected only the input arguments