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 from_static to HeaderValue #1085

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 33 additions & 9 deletions async-nats/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

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

use std::{collections::HashMap, fmt, slice::Iter, str::FromStr};
use std::{
collections::HashMap,
fmt,
slice::Iter,
str::{self, FromStr},
};

use bytes::Bytes;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -200,7 +205,7 @@ impl HeaderMap {
for v in vs.iter() {
buf.extend_from_slice(k.as_str().as_bytes());
buf.extend_from_slice(b": ");
buf.extend_from_slice(v.inner.as_bytes());
buf.extend_from_slice(v.inner.as_ref());
buf.extend_from_slice(b"\r\n");
}
}
Expand All @@ -222,12 +227,12 @@ impl HeaderMap {
/// ```
#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
pub struct HeaderValue {
inner: String,
inner: Bytes,
}

impl ToString for HeaderValue {
fn to_string(&self) -> String {
self.inner.to_string()
self.as_str().to_owned()
}
}

Expand All @@ -245,7 +250,7 @@ impl From<&HeaderValue> for String {

impl<'a> From<&'a HeaderValue> for &'a str {
fn from(header: &'a HeaderValue) -> Self {
header.inner.as_str()
unsafe { str::from_utf8_unchecked(header.inner.as_ref()) }
}
}

Expand All @@ -258,23 +263,23 @@ impl FromStr for HeaderValue {
}

Ok(HeaderValue {
inner: s.to_string(),
inner: Bytes::copy_from_slice(s.as_bytes()),
})
}
}

impl From<u64> for HeaderValue {
fn from(v: u64) -> Self {
Self {
inner: v.to_string(),
inner: Bytes::from(v.to_string()),
}
}
}

impl From<&str> for HeaderValue {
fn from(v: &str) -> Self {
Self {
inner: v.to_string(),
inner: Bytes::from(v.to_string()),
}
}
}
Expand All @@ -284,6 +289,17 @@ impl HeaderValue {
HeaderValue::default()
}

/// Convert a static string to a `HeaderValue`.
///
/// This function will not perform any copying, however the string is
/// checked to ensure that no invalid characters are present.
pub const fn from_static(s: &'static str) -> HeaderValue {
// TODO(caspervonb) do a static range check
HeaderValue {
inner: Bytes::from_static(s.as_bytes()),
}
}

pub fn as_str(&self) -> &str {
self.into()
}
Expand Down Expand Up @@ -328,7 +344,7 @@ pub trait IntoHeaderValue {
impl IntoHeaderValue for &str {
fn into_header_value(self) -> HeaderValue {
HeaderValue {
inner: self.to_string(),
inner: Bytes::from(self.to_string()),
}
}
}
Expand Down Expand Up @@ -710,4 +726,12 @@ mod tests {

assert_eq!(a, b);
}

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

assert_eq!(a, b);
}
}
Loading