-
I use a lot of type aliases in my code, for example I have attempted to use thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yeah, type aliases are a bit tricky topic and the reason being is as you said Rust does not allow derive macros (nor traits to be implemented for) to be used with type aliases. This evidently gives users three choices with how things currently work.
Both of 1. and 2. them has advantages and disadvantages. For what comes to the new type pattern Rust compiler will remove the unnecessary layer of abstraction at compile time which leads to no extra cost in compiled application. Since it is a new type it allows the regular use of macros and trait implementation making it a viable option. However it does not come without shortcomings like using new type pattern obviously enforces users to implement a lot of extra boilerplate code to expose the actual APIs of the actual type through the extra layer and would not lead to as clean APIs as one might wish. However just using the type aliases it is necessary to use the To implement it manually one could use the impl Modify for MyApi {
fn modify(&self, &mut openapi: utoipa::openapi::OpenApi) {
if let Some(components) = openapi.components {
let builder: ComponentsBuilder = components.clone().into();
builder.schema("RecordId", ObjectBuilder::new()....);
openapi.components = Some(builder.build());
}
}
} Just when I were thinking this matter, perhaps in future a macro for registering type aliases as certain type could be provided to implement relatively similar boilerplate code could to the utoipa. At the moment there is no such functionality so this needs some experimenting nonetheless I believe it could be possible. |
Beta Was this translation helpful? Give feedback.
Yeah, type aliases are a bit tricky topic and the reason being is as you said Rust does not allow derive macros (nor traits to be implemented for) to be used with type aliases. This evidently gives users three choices with how things currently work.
#[schema(value_type = ...)]
you have already used.Modify
trait or though builders.Both of 1. and 2. them has advantages and disadvantages. For what comes to the new type pattern Rust compiler will remove the unnecessary layer of abstraction at compile time which leads to no extra cost in compiled application. Since it is a new t…