rocket/request/
atomic_method.rs1use std::sync::atomic::{AtomicU8, Ordering};
2
3use crate::http::Method;
4
5pub struct AtomicMethod(AtomicU8);
6
7const METHODS: [Method; 9] = [
8 Method::Get,
9 Method::Put,
10 Method::Post,
11 Method::Delete,
12 Method::Options,
13 Method::Head,
14 Method::Trace,
15 Method::Connect,
16 Method::Patch,
17];
18
19#[inline(always)]
20const fn index(method: Method) -> u8 {
21 match method {
22 Method::Get => 0,
23 Method::Put => 1,
24 Method::Post => 2,
25 Method::Delete => 3,
26 Method::Options => 4,
27 Method::Head => 5,
28 Method::Trace => 6,
29 Method::Connect => 7,
30 Method::Patch => 8,
31 }
32}
33
34impl AtomicMethod {
35 #[inline]
36 pub fn new(value: Method) -> Self {
37 Self(AtomicU8::new(index(value)))
38 }
39
40 #[inline]
41 pub fn load(&self) -> Method {
42 METHODS[self.0.load(Ordering::Acquire) as usize]
43 }
44
45 #[inline]
46 pub fn store(&self, new: Method) {
47 self.0.store(index(new), Ordering::Release)
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn round_trip() {
57 for method in METHODS {
58 assert_eq!(METHODS[index(method) as usize], method);
59
60 let atomic = AtomicMethod::new(method);
61 assert_eq!(atomic.load(), method);
62
63 let atomic = AtomicMethod::new(Method::Get);
64 atomic.store(method);
65 assert_eq!(atomic.load(), method);
66 }
67 }
68}