1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::HeaderValue;
use smallvec::{smallvec, SmallVec};
use smartcow::SmartCow;
use std::{
fmt::{Debug, Formatter, Result},
iter::FromIterator,
ops::{Deref, DerefMut},
};
#[derive(Clone, Eq, PartialEq)]
pub struct HeaderValues(SmallVec<[HeaderValue; 1]>);
impl Deref for HeaderValues {
type Target = [HeaderValue];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Default for HeaderValues {
fn default() -> Self {
Self(SmallVec::with_capacity(1))
}
}
impl DerefMut for HeaderValues {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Debug for HeaderValues {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self.one() {
Some(one) => Debug::fmt(one, f),
None => f.debug_list().entries(&self.0).finish(),
}
}
}
impl IntoIterator for HeaderValues {
type Item = HeaderValue;
type IntoIter = smallvec::IntoIter<[HeaderValue; 1]>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<I> FromIterator<I> for HeaderValues
where
I: Into<HeaderValue>,
{
fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
Self(iter.into_iter().map(Into::into).collect())
}
}
impl HeaderValues {
#[must_use]
pub fn new() -> Self {
Self(SmallVec::with_capacity(1))
}
pub fn as_str(&self) -> Option<&str> {
self.one().and_then(HeaderValue::as_str)
}
pub(crate) fn as_lower(&self) -> Option<SmartCow<'_>> {
self.one().and_then(HeaderValue::as_lower)
}
pub fn one(&self) -> Option<&HeaderValue> {
if self.len() == 1 {
self.0.first()
} else {
None
}
}
pub fn append(&mut self, value: impl Into<HeaderValue>) {
self.0.push(value.into());
}
pub fn extend(&mut self, values: impl Into<HeaderValues>) {
let values = values.into();
self.0.extend(values);
}
}
impl From<Vec<u8>> for HeaderValues {
fn from(v: Vec<u8>) -> Self {
Self(smallvec![v.into()])
}
}
impl From<String> for HeaderValues {
fn from(s: String) -> Self {
Self(smallvec![s.into()])
}
}
impl From<&'static str> for HeaderValues {
fn from(s: &'static str) -> Self {
Self(smallvec![s.into()])
}
}
impl From<HeaderValue> for HeaderValues {
fn from(v: HeaderValue) -> Self {
Self(smallvec![v])
}
}
impl<HV> From<Vec<HV>> for HeaderValues
where
HV: Into<HeaderValue>,
{
fn from(v: Vec<HV>) -> Self {
Self(v.into_iter().map(Into::into).collect())
}
}