How does this magic work? — Recursive ToSchema auto register. #1191
-
I tried to understand #1066 but I could not figure out how this works. I am very interested in what to me seems like keeping state inside proc macro calls. I think this could be very helpful for other libraries. I am for example thinking of a message queue where you can at definition time of the message write how the message should be handled and let the macro auto register this message in another place in the codebase without having to basically manually parse the whole codebase to find all messages. Thanks a lot for creating this library and this interesting pattern! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
In short, there is no global state. And making one would be quite hard actually and not really sustainable (in utoipa's case). In utoipa the so-called "state" tracking is a chain of definitions. All that needs to be registered needs to be linked to something that is linked to another thing that is eventually linked to an This is made possible by Rust's amazing |
Beta Was this translation helpful? Give feedback.
In short, there is no global state. And making one would be quite hard actually and not really sustainable (in utoipa's case).
In utoipa the so-called "state" tracking is a chain of definitions. All that needs to be registered needs to be linked to something that is linked to another thing that is eventually linked to an
OpenApi
. In the example shown in #1066 thePerson
type has reference to theAccount
. WhenToSchema
is executed for thePerson
struct it will create a schema reference to theAccount
. Later when thePerson
type is used inrequest_body
it will create schema references toPerson
for the pathtest_collect_schemas
. Even later when the pathtest_collect_schemas
is added to theO…