Refactor RapiDoc
to take Cow<'static, str>
instead of borrowed str
#867
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.
Problem
As far as I can tell, it was previously impossible to dynamically construct URLs or custom HTML in certain cases. This was due to
RapiDoc
'spath
,spec_url
andhtml
fields being&'_ str
.Not bad in principle of course, but a problem arose when trying to use this in combination with
actix_web
(possibly other integrations, but I haven't tried).actix_web
'sHttpServer::new
requires that you pass a factory closure with the following conditions:Fn() -> I + Send + Clone + 'static
, and inside of that closure we need to initializeRapiDoc
. As such, the issue is that we can't dynamically construct a'static
string in these conditions (excluding quite terrible practices).For example, this would not compile beforehand (in this case due to
temporary value dropped while borrowed
):Solution
Instead of having
RapiDoc
's methods take&'_ str
, I modified them to takeInto<Cow<'static, str>>
and modified thepath
,spec_url
andhtml
fields to beCow<'static, str>
. This is also how this is handled in the adjacentutoipa_swagger_ui
crate.This example with a dynamic URL now compiles:
Breaking changes
As far as the methods go, I don't think this has any breaking changes due to the
Into<Cow<'static, str>>
generic. However, I think the removal of the three lifetime parameters onRapiDoc
can break some code.