Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static representation for custom header names #967

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions async-nats/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

//! NATS [Message][crate::Message] headers, modeled loosely after the [http::header] crate.

use bytes::Bytes;
use std::{collections::HashMap, fmt, slice, str::FromStr};

/// A struct for handling NATS headers.
Expand Down Expand Up @@ -265,7 +266,7 @@ pub trait IntoHeaderName {
impl IntoHeaderName for &str {
fn into_header_name(self) -> HeaderName {
HeaderName {
inner: HeaderRepr::Custom(self.to_string()),
inner: HeaderRepr::Custom(self.into()),
}
}
}
Expand Down Expand Up @@ -385,10 +386,47 @@ standard_headers! {
(NatsExpectedStream, NATS_EXPECTED_STREAM, b"Nats-Expected-Stream");
}

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
struct CustomHeader {
bytes: Bytes,
}

impl CustomHeader {
#[inline]
pub(crate) const fn from_static(value: &'static str) -> CustomHeader {
CustomHeader {
bytes: Bytes::from_static(value.as_bytes()),
}
}

#[inline]
pub(crate) fn as_str(&self) -> &str {
unsafe { std::str::from_utf8_unchecked(self.bytes.as_ref()) }
}
}

impl From<String> for CustomHeader {
#[inline]
fn from(value: String) -> CustomHeader {
CustomHeader {
bytes: Bytes::from(value),
}
}
}

impl<'a> From<&'a str> for CustomHeader {
#[inline]
fn from(value: &'a str) -> CustomHeader {
CustomHeader {
bytes: Bytes::copy_from_slice(value.as_bytes()),
}
}
}

#[derive(Debug, Hash, PartialEq, Eq, Clone)]
enum HeaderRepr {
Standard(StandardHeader),
Custom(String),
Custom(CustomHeader),
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
Expand All @@ -397,6 +435,20 @@ pub struct HeaderName {
}

impl HeaderName {
/// Converts a static string to a NATS header name.
#[inline]
pub const fn from_static(value: &'static str) -> HeaderName {
if let Some(standard) = StandardHeader::from_bytes(value.as_bytes()) {
return HeaderName {
inner: HeaderRepr::Standard(standard),
};
}

HeaderName {
inner: HeaderRepr::Custom(CustomHeader::from_static(value)),
}
}

/// Returns a `str` representation of the header.
#[inline]
fn as_str(&self) -> &str {
Expand All @@ -420,7 +472,7 @@ impl FromStr for HeaderName {
inner: HeaderRepr::Standard(v),
}),
None => Ok(HeaderName {
inner: HeaderRepr::Custom(s.to_string()),
inner: HeaderRepr::Custom(CustomHeader::from(s)),
}),
}
}
Expand Down Expand Up @@ -572,4 +624,12 @@ mod tests {
parsed_header.ok()
);
}

#[test]
fn from_static_eq() {
let a = HeaderName::from_static("NATS-Stream");
let b = HeaderName::from_static("NATS-Stream");

assert_eq!(a, b);
}
}