Skip to content

Commit

Permalink
Use a Cow in HeaderName::Custom
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Sep 20, 2023
1 parent fbd77fb commit 2c0205e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 16 additions & 3 deletions lib/proto/http/header/header_name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt::{self, Display};
use std::{
borrow::Cow,
fmt::{self, Display},
};

use crate::internal::misc::filter_crlf;

Expand All @@ -22,17 +25,27 @@ macro_rules! headers {
),*,
/// Custom header type.
/// Only used when the header type is unknown to afire.
Custom(String),
Custom(Cow<'static, str>),
}

impl HeaderName {
fn from_str(s: &str) -> Self {
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<Cow<'static, str>>) -> 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 {
Expand Down

0 comments on commit 2c0205e

Please sign in to comment.