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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use bytes::Bytes;
/// HTTP request method (RFC 9110 §9). Standard methods are unit variants — an
/// integer-discriminant compare, no allocation. A non-standard method carries
/// its wire bytes in [`Method::Other`], so inbound parsing of an unknown method
/// never copies beyond the wire buffer it already holds (use [`Method::from_wire`]).
///
/// Construction is via `From<&str>` / `From<&[u8]>` / `From<Bytes>`, so builder
/// sites that wrote `.method("GET")` keep working — a standard method parses to
/// a unit variant with zero heap, replacing the prior `Bytes::copy_from_slice`.
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub enum Method {
#[default]
Get,
Post,
Put,
Patch,
Delete,
Head,
Options,
Connect,
Trace,
/// A non-standard method, holding its wire bytes.
Other(Bytes),
}
impl Method {
/// Parse from raw bytes. Methods are case-sensitive uppercase tokens
/// (RFC 9110 §9.1), so the match is exact. Standard methods return a unit
/// variant without allocating; an unknown method copies into `Other`.
#[must_use]
pub fn from_bytes(bytes: &[u8]) -> Self {
match bytes {
b"GET" => Self::Get,
b"POST" => Self::Post,
b"PUT" => Self::Put,
b"PATCH" => Self::Patch,
b"DELETE" => Self::Delete,
b"HEAD" => Self::Head,
b"OPTIONS" => Self::Options,
b"CONNECT" => Self::Connect,
b"TRACE" => Self::Trace,
other => Self::Other(Bytes::copy_from_slice(other)),
}
}
/// Take ownership of wire `Bytes`: a standard method drops the bytes and
/// returns a unit variant; an unknown method keeps the bytes **zero-copy**
/// in `Other`. This is the inbound-decode boundary — no allocation beyond
/// the wire `Bytes` the caller already owns.
#[must_use]
pub fn from_wire(bytes: Bytes) -> Self {
match bytes.as_ref() {
b"GET" => Self::Get,
b"POST" => Self::Post,
b"PUT" => Self::Put,
b"PATCH" => Self::Patch,
b"DELETE" => Self::Delete,
b"HEAD" => Self::Head,
b"OPTIONS" => Self::Options,
b"CONNECT" => Self::Connect,
b"TRACE" => Self::Trace,
_ => Self::Other(bytes),
}
}
/// The method's wire bytes. Standard methods return a `'static` slice; an
/// `Other` returns its carried bytes. No allocation.
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
match self {
Self::Get => b"GET",
Self::Post => b"POST",
Self::Put => b"PUT",
Self::Patch => b"PATCH",
Self::Delete => b"DELETE",
Self::Head => b"HEAD",
Self::Options => b"OPTIONS",
Self::Connect => b"CONNECT",
Self::Trace => b"TRACE",
Self::Other(bytes) => bytes.as_ref(),
}
}
/// The method as `Bytes` for wire serialization. Standard methods are
/// `Bytes::from_static` (zero allocation); `Other` clones its `Bytes`
/// (a refcount bump, no copy).
#[must_use]
pub fn to_bytes(&self) -> Bytes {
match self {
Self::Get => Bytes::from_static(b"GET"),
Self::Post => Bytes::from_static(b"POST"),
Self::Put => Bytes::from_static(b"PUT"),
Self::Patch => Bytes::from_static(b"PATCH"),
Self::Delete => Bytes::from_static(b"DELETE"),
Self::Head => Bytes::from_static(b"HEAD"),
Self::Options => Bytes::from_static(b"OPTIONS"),
Self::Connect => Bytes::from_static(b"CONNECT"),
Self::Trace => Bytes::from_static(b"TRACE"),
Self::Other(bytes) => Bytes::clone(bytes),
}
}
/// The method as `&str` when its bytes are valid UTF-8 (always true for
/// standard methods).
#[must_use]
pub fn as_str(&self) -> Option<&str> {
core::str::from_utf8(self.as_bytes()).ok()
}
/// Whether this is a safe + idempotent read method (GET/HEAD).
#[must_use]
pub fn is_read(&self) -> bool {
matches!(self, Self::Get | Self::Head)
}
}
impl From<&[u8]> for Method {
fn from(bytes: &[u8]) -> Self {
Self::from_bytes(bytes)
}
}
impl From<&str> for Method {
fn from(value: &str) -> Self {
Self::from_bytes(value.as_bytes())
}
}
impl From<Bytes> for Method {
fn from(bytes: Bytes) -> Self {
Self::from_wire(bytes)
}
}
impl From<&Bytes> for Method {
fn from(bytes: &Bytes) -> Self {
Self::from_bytes(bytes.as_ref())
}
}
impl PartialEq<[u8]> for Method {
fn eq(&self, other: &[u8]) -> bool {
self.as_bytes() == other
}
}
impl PartialEq<&[u8]> for Method {
fn eq(&self, other: &&[u8]) -> bool {
self.as_bytes() == *other
}
}
impl PartialEq<str> for Method {
fn eq(&self, other: &str) -> bool {
self.as_bytes() == other.as_bytes()
}
}
impl PartialEq<&str> for Method {
fn eq(&self, other: &&str) -> bool {
self.as_bytes() == other.as_bytes()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn standard_methods_parse_to_unit_variants() {
assert_eq!(Method::from_bytes(b"GET"), Method::Get);
assert_eq!(Method::from(&b"POST"[..]), Method::Post);
assert_eq!(Method::from("DELETE"), Method::Delete);
}
#[test]
fn from_wire_keeps_unknown_method_zero_copy() {
let wire = Bytes::from_static(b"PURGE");
let method = Method::from_wire(Bytes::clone(&wire));
// the Other arm holds the SAME allocation (ptr-equal), not a copy.
match method {
Method::Other(bytes) => assert_eq!(bytes.as_ptr(), wire.as_ptr()),
other => panic!("expected Other, got {other:?}"),
}
}
#[test]
fn from_wire_standard_drops_bytes_to_unit() {
assert_eq!(Method::from_wire(Bytes::from_static(b"PUT")), Method::Put);
}
#[test]
fn round_trips_through_wire_bytes() {
for raw in [
&b"GET"[..],
b"POST",
b"PUT",
b"PATCH",
b"DELETE",
b"HEAD",
b"OPTIONS",
b"CONNECT",
b"TRACE",
b"PURGE",
] {
let method = Method::from_bytes(raw);
assert_eq!(method.as_bytes(), raw);
assert_eq!(method.to_bytes().as_ref(), raw);
}
}
#[test]
fn standard_to_bytes_is_static_no_alloc() {
// from_static carries no heap pointer the allocator owns; sanity that
// the standard path never touches Other.
assert!(matches!(Method::Get.to_bytes(), bytes if bytes.as_ref() == b"GET"));
}
}