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
use std::borrow::Cow;
use std::fmt::{Debug, Formatter};
use std::net::SocketAddr;
use bytecheck::CheckBytes;
use rkyv::de::deserializers::SharedDeserializeMap;
use rkyv::validation::validators::DefaultValidator;
use rkyv::{Archive, Deserialize, Serialize};
use crate::view::{DataView, InvalidView};
#[repr(C)]
#[derive(Serialize, Deserialize, Archive, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[archive_attr(derive(CheckBytes))]
pub struct MessageMetadata {
#[with(rkyv::with::AsOwned)]
pub(crate) service_name: Cow<'static, str>,
#[with(rkyv::with::AsOwned)]
pub(crate) path: Cow<'static, str>,
}
pub struct Request<Msg>
where
Msg: Archive,
Msg::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
{
pub(crate) remote_addr: SocketAddr,
#[cfg(debug_assertions)]
pub(crate) view: Box<DataView<Msg>>,
#[cfg(not(debug_assertions))]
pub(crate) view: DataView<Msg>,
}
impl<Msg> Debug for Request<Msg>
where
Msg: Archive,
Msg::Archived: CheckBytes<DefaultValidator<'static>> + Debug + 'static,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Request")
.field("view", &self.view)
.field("remote_addr", &self.remote_addr)
.finish()
}
}
impl<Msg> Request<Msg>
where
Msg: Archive,
Msg::Archived: CheckBytes<DefaultValidator<'static>> + 'static,
{
pub(crate) fn new(remote_addr: SocketAddr, view: DataView<Msg>) -> Self {
Self {
remote_addr,
#[cfg(debug_assertions)]
view: Box::new(view),
#[cfg(not(debug_assertions))]
view,
}
}
#[cfg(debug_assertions)]
pub fn into_view(self) -> DataView<Msg> {
*self.view
}
#[cfg(not(debug_assertions))]
pub fn into_view(self) -> DataView<Msg> {
self.view
}
pub fn remote_addr(&self) -> SocketAddr {
self.remote_addr
}
}
impl<Msg> Request<Msg>
where
Msg: Archive,
Msg::Archived: CheckBytes<DefaultValidator<'static>>
+ Deserialize<Msg, SharedDeserializeMap>
+ 'static,
{
pub fn to_owned(&self) -> Result<Msg, InvalidView> {
self.view.to_owned()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_metadata() {
let meta = MessageMetadata {
service_name: Cow::Borrowed("test"),
path: Cow::Borrowed("demo"),
};
let bytes = rkyv::to_bytes::<_, 1024>(&meta).expect("Serialize");
let copy: MessageMetadata = rkyv::from_bytes(&bytes).expect("Deserialize");
assert_eq!(meta, copy, "Deserialized value should match");
}
#[test]
fn test_request() {
let msg = MessageMetadata {
service_name: Cow::Borrowed("test"),
path: Cow::Borrowed("demo"),
};
let addr = "127.0.0.1:8000".parse().unwrap();
let bytes = rkyv::to_bytes::<_, 1024>(&msg).expect("Serialize");
let view: DataView<MessageMetadata, _> =
DataView::using(bytes).expect("Create view");
let req = Request::new(addr, view);
assert_eq!(req.remote_addr(), addr, "Remote addr should match.");
assert_eq!(
req.to_owned().unwrap(),
msg,
"Deserialized value should match."
);
}
}