optimize for optional static response headers zero-copy zero-alloc

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-12 22:59:57 +00:00
parent a6e340e24d
commit fd11fee88d
9 changed files with 40 additions and 30 deletions

View File

@ -7,7 +7,7 @@ pub mod v1 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1mediadownloadservernamemediaid
use std::time::Duration;
use std::{borrow::Cow, time::Duration};
use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
use ruma_common::{
@ -58,7 +58,7 @@ pub mod v1 {
/// The content type of the file that was previously uploaded.
#[ruma_api(header = CONTENT_TYPE)]
pub content_type: Option<String>,
pub content_type: Option<Cow<'static, str>>,
/// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
/// file that was previously uploaded.
@ -67,7 +67,7 @@ pub mod v1 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax
#[ruma_api(header = CONTENT_DISPOSITION)]
pub content_disposition: Option<String>,
pub content_disposition: Option<Cow<'static, str>>,
}
impl Request {

View File

@ -7,7 +7,7 @@ pub mod v1 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1mediadownloadservernamemediaidfilename
use std::time::Duration;
use std::{borrow::Cow, time::Duration};
use http::header::{CONTENT_DISPOSITION, CONTENT_TYPE};
use ruma_common::{
@ -62,7 +62,7 @@ pub mod v1 {
/// The content type of the file that was previously uploaded.
#[ruma_api(header = CONTENT_TYPE)]
pub content_type: Option<String>,
pub content_type: Option<Cow<'static, str>>,
/// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
/// file that was previously uploaded.
@ -71,7 +71,7 @@ pub mod v1 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax
#[ruma_api(header = CONTENT_DISPOSITION)]
pub content_disposition: Option<String>,
pub content_disposition: Option<Cow<'static, str>>,
}
impl Request {

View File

@ -7,7 +7,7 @@ pub mod v1 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1mediathumbnailservernamemediaid
use std::time::Duration;
use std::{borrow::Cow, time::Duration};
use http::header::CONTENT_TYPE;
use js_int::UInt;
@ -87,7 +87,7 @@ pub mod v1 {
/// The content type of the thumbnail.
#[ruma_api(header = CONTENT_TYPE)]
pub content_type: Option<String>,
pub content_type: Option<Cow<'static, str>>,
}
impl Request {

View File

@ -7,7 +7,7 @@ pub mod v3 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3downloadservernamemediaid
use std::time::Duration;
use std::{borrow::Cow, time::Duration};
use http::header::{CACHE_CONTROL, CONTENT_DISPOSITION, CONTENT_TYPE};
use ruma_common::{
@ -83,7 +83,7 @@ pub mod v3 {
/// The content type of the file that was previously uploaded.
#[ruma_api(header = CONTENT_TYPE)]
pub content_type: Option<String>,
pub content_type: Option<Cow<'static, str>>,
/// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
/// file that was previously uploaded.
@ -92,7 +92,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax
#[ruma_api(header = CONTENT_DISPOSITION)]
pub content_disposition: Option<String>,
pub content_disposition: Option<Cow<'static, str>>,
/// The value of the `Cross-Origin-Resource-Policy` HTTP header.
///
@ -100,7 +100,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy#syntax
#[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
pub cross_origin_resource_policy: Option<String>,
pub cross_origin_resource_policy: Option<Cow<'static, str>>,
/// The value of the `Cache-Control` HTTP header.
///
@ -108,7 +108,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#syntax
#[ruma_api(header = CACHE_CONTROL)]
pub cache_control: Option<String>,
pub cache_control: Option<Cow<'static, str>>,
}
#[allow(deprecated)]
@ -141,7 +141,7 @@ pub mod v3 {
file,
content_type: None,
content_disposition: None,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
cross_origin_resource_policy: Some("cross-origin".into()),
cache_control: None,
}
}

View File

@ -7,7 +7,7 @@ pub mod v3 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3downloadservernamemediaidfilename
use std::time::Duration;
use std::{borrow::Cow, time::Duration};
use http::header::{CACHE_CONTROL, CONTENT_DISPOSITION, CONTENT_TYPE};
use ruma_common::{
@ -87,7 +87,7 @@ pub mod v3 {
/// The content type of the file that was previously uploaded.
#[ruma_api(header = CONTENT_TYPE)]
pub content_type: Option<String>,
pub content_type: Option<Cow<'static, str>>,
/// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
/// file that was previously uploaded.
@ -96,7 +96,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax
#[ruma_api(header = CONTENT_DISPOSITION)]
pub content_disposition: Option<String>,
pub content_disposition: Option<Cow<'static, str>>,
/// The value of the `Cross-Origin-Resource-Policy` HTTP header.
///
@ -104,7 +104,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy#syntax
#[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
pub cross_origin_resource_policy: Option<String>,
pub cross_origin_resource_policy: Option<Cow<'static, str>>,
/// The value of the `Cache-Control` HTTP header.
///
@ -112,7 +112,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#syntax
#[ruma_api(header = CACHE_CONTROL)]
pub cache_control: Option<String>,
pub cache_control: Option<Cow<'static, str>>,
}
#[allow(deprecated)]
@ -146,7 +146,7 @@ pub mod v3 {
file,
content_type: None,
content_disposition: None,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
cross_origin_resource_policy: Some("cross-origin".into()),
cache_control: None,
}
}

View File

@ -7,7 +7,7 @@ pub mod v3 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixmediav3thumbnailservernamemediaid
use std::time::Duration;
use std::{borrow::Cow, time::Duration};
use http::header::{CACHE_CONTROL, CONTENT_DISPOSITION, CONTENT_TYPE};
use js_int::UInt;
@ -112,7 +112,7 @@ pub mod v3 {
/// The content type of the thumbnail.
#[ruma_api(header = CONTENT_TYPE)]
pub content_type: Option<String>,
pub content_type: Option<Cow<'static, str>>,
/// The value of the `Cross-Origin-Resource-Policy` HTTP header.
///
@ -120,7 +120,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Resource-Policy#syntax
#[ruma_api(header = CROSS_ORIGIN_RESOURCE_POLICY)]
pub cross_origin_resource_policy: Option<String>,
pub cross_origin_resource_policy: Option<Cow<'static, str>>,
/// The value of the `Cache-Control` HTTP header.
///
@ -128,7 +128,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#syntax
#[ruma_api(header = CACHE_CONTROL)]
pub cache_control: Option<String>,
pub cache_control: Option<Cow<'static, str>>,
/// The value of the `Content-Disposition` HTTP header, possibly containing the name of the
/// file that was previously uploaded.
@ -137,7 +137,7 @@ pub mod v3 {
///
/// [MDN]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax
#[ruma_api(header = CONTENT_DISPOSITION)]
pub content_disposition: Option<String>,
pub content_disposition: Option<Cow<'static, str>>,
}
#[allow(deprecated)]
@ -180,7 +180,7 @@ pub mod v3 {
Self {
file,
content_type: None,
cross_origin_resource_policy: Some("cross-origin".to_owned()),
cross_origin_resource_policy: Some("cross-origin".into()),
cache_control: None,
content_disposition: None,
}

View File

@ -5,6 +5,8 @@ pub mod v3 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3loginssoredirect
use std::borrow::Cow;
use http::header::{LOCATION, SET_COOKIE};
use ruma_common::{
api::{request, response, Metadata},
@ -40,7 +42,7 @@ pub mod v3 {
/// Cookie storing state to secure the SSO process.
#[ruma_api(header = SET_COOKIE)]
pub cookie: Option<String>,
pub cookie: Option<Cow<'static, str>>,
}
impl Request {

View File

@ -7,6 +7,8 @@ pub mod v3 {
//!
//! [spec]: https://spec.matrix.org/latest/client-server-api/#get_matrixclientv3loginssoredirectidpid
use std::borrow::Cow;
use http::header::{LOCATION, SET_COOKIE};
use ruma_common::{
api::{request, response, Metadata},
@ -46,7 +48,7 @@ pub mod v3 {
/// Cookie storing state to secure the SSO process.
#[ruma_api(header = SET_COOKIE)]
pub cookie: Option<String>,
pub cookie: Option<Cow<'static, str>>,
}
impl Request {

View File

@ -23,10 +23,16 @@ impl Response {
if segments.last().unwrap().ident == "Option" =>
{
quote! {
if let Some(header) = self.#field_name {
if let Some(ref header) = self.#field_name {
let header = match header {
::std::borrow::Cow::Borrowed(ref header) =>
#http::header::HeaderValue::from_static(header),
::std::borrow::Cow::Owned(ref header) =>
#http::header::HeaderValue::from_str(&header)?,
};
headers.insert(
#header_name,
header.parse()?,
header,
);
}
}