Hello,
When I migrated to the 2024 edition, this code that worked before no longer compiles now!
Code :
...
pub(crate) fn create_html(document: &str, title: &str) -> String {
SWAGGER_UI_TEMPLATE
.replace("{:title}", title)
.replace("{:style}", SWAGGER_UI_CSS)
.replace("{:script}", SWAGGER_UI_JS)
.replace("{:spec}", document)
}
pub(crate) fn create_endpoint(document: &str, title: &str) -> impl Endpoint {
let ui_html = create_html(document, title);
poem::Route::new()
.at("/", make_sync(move |_| Html(ui_html.clone())))
.at("/oauth-receiver.html", make_sync(move |_| Html(OAUTH_RECEIVER_HTML.to_string())))
}
pub fn modern_swagger_ui<T: poem_openapi::OpenApi, W: poem_openapi::Webhook>(oas: &OpenApiService<T, W>, title: &str) -> impl Endpoint {
create_endpoint(&oas.spec(), title)
}
#[allow(dead_code)]
pub trait ModernSwagger {
fn modern_swagger_ui(&self) -> impl Endpoint;
}
impl<T: poem_openapi::OpenApi, W: poem_openapi::Webhook> ModernSwagger for OpenApiService<T, W> {
fn modern_swagger_ui(&self) -> impl Endpoint {
create_endpoint(&self.spec(), "Swagger-ui")
}
}
...
The errors :
|
63 | create_endpoint(&oas.spec(), title)
| -----------------^^^^^^^^^^--------
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'static`
72 | fn modern_swagger_ui(&self) -> impl Endpoint {
| - let's call the lifetime of this reference `'1`
73 | create_endpoint(&self.spec(), "Swagger-ui")
| -----------------^^^^^^^^^^^---------------
| | | |
| | | temporary value is freed at the end of this statement
| | creates a temporary value which is freed while still in use
| argument requires that borrow lasts for `'1`
107 | let mut api_service = OpenApiService::new((rest::AuthorizationRestCtl, rest::CityRestCtl, rest::AuthRestCtl, rest::SystemRestCtl, rest::MarketRestCtl), "APIs", "1.0")
| --------------- binding `api_service` declared here
138 | let ui = swagger_ui::modern_swagger_ui(&api_service, "API DOC");
| ------------------------------^^^^^^^^^^^^------------
| | |
| | borrowed value does not live long enough
| argument requires that `api_service` is borrowed for `'static`
I fixed the issue, of course, but I'm wondering what changed so much that it no longer accepts this reference passing? Or was the compiler previously blind and failed to detect this error?