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

Enhance generic aliases with lifetimes support #458

Merged
merged 2 commits into from
Jan 25, 2023

Conversation

juhaku
Copy link
Owner

@juhaku juhaku commented Jan 25, 2023

This PR will add support for aliases with lifetimes. As demonstrated now the lifetime variable can be declared or omitted from left side of equals (=) mark. The lifetime at right side of the equals mark will be used as a lifetime for generated Rust type alias.

 #[derive(ToSchema)]
 #[aliases(Paginated1<'b> = Paginated<'b, String>, Paginated2 = Paginated<'b, Value>)]
 struct Paginated<'r, TResult> {
     pub total: usize,
     pub data: Vec<TResult>,
     pub next: Option<&'r str>,
     pub prev: Option<&'r str>,
 }

There is still one limitation with 'static lifetimes. E.g if one would define alias as Paginated3 = Paginated<'static, Value> this would cause a compile error. Normally this would be allowed syntax in Rust code.

There is a need to deprecate this alias functionality in future in favor of adding a generic solution for Rust type aliases in general.

Resolves #427

This PR will add support for aliases with lifetimes. As demonstrated now
the lifetime variable can be declared or omitted from left side of
equals (=) mark. The lifetime at right side of the equals mark will be
used as a lifetime for generated Rust type alias.

```rust
 #[aliases(Paginated1<'b> = Paginated<'b, String>, Paginated2 = Paginated<'b, Value>)]
 struct Paginated<'r, TResult> {
     pub total: usize,
     pub data: Vec<TResult>,
     pub next: Option<&'r str>,
     pub prev: Option<&'r str>,
 }
```

There is still one limitation with `'static` lifetimes. E.g if one would
define alias as `Paginated3 = Paginated<'static, Value>` this would
cause a compile error. Normally this would be allowed syntax in Rust
code.

There is a need to deprecate this alias functionality in future in favor
of adding a generic solution for Rust type aliases in general.
@juhaku juhaku merged commit 5b51eb4 into master Jan 25, 2023
@juhaku juhaku deleted the enhance-alias-support-with-lifetimes branch January 25, 2023 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Released
Development

Successfully merging this pull request may close these issues.

Generics do not work with lifetime specifiers
1 participant