diff --git a/Changelog.md b/Changelog.md index b221875..579a7d9 100644 --- a/Changelog.md +++ b/Changelog.md @@ -63,6 +63,7 @@ Coming Soon - Create 'header structs' that can be converted into a `Header` and simplify working with headers in responses. - Use [loopback](https://tools.ietf.org/html/rfc1122), [private](https://tools.ietf.org/html/rfc1918), and [unique local](https://tools.ietf.org/html/rfc4193) addresses for RealIp - Add is_informational, is_success, is_redirect, is_client_error, and is_server_error methods on status codes. +- Use a Cow in HeaderName::Custom # 2.2.1 diff --git a/lib/proto/http/header/header_name.rs b/lib/proto/http/header/header_name.rs index c579b99..7837a79 100644 --- a/lib/proto/http/header/header_name.rs +++ b/lib/proto/http/header/header_name.rs @@ -1,4 +1,7 @@ -use std::fmt::{self, Display}; +use std::{ + borrow::Cow, + fmt::{self, Display}, +}; use crate::internal::misc::filter_crlf; @@ -22,7 +25,7 @@ macro_rules! headers { ),*, /// Custom header type. /// Only used when the header type is unknown to afire. - Custom(String), + Custom(Cow<'static, str>), } impl HeaderName { @@ -30,9 +33,19 @@ macro_rules! headers { use HeaderName::*; match s.to_ascii_lowercase().as_str() { $($header_lower => $name),*, - _ => HeaderName::Custom(filter_crlf(s)), + _ => HeaderName::Custom(Cow::Owned(filter_crlf(s))), } } + + /// A custom header name. + pub fn custom(s: impl Into>) -> Self { + HeaderName::Custom(s.into()) + } + + /// Create a custom header name from a static string. + pub const fn custom_str(s: &'static str) -> Self { + HeaderName::Custom(Cow::Borrowed(s)) + } } impl Display for HeaderName {