Skip to content

Commit f22f308

Browse files
committed
Addressing review comments
- Remove root_context_id from create_http_stream and create_http_context calls - Move EmptyHttpContext after HttpContext declaration - Roll up all Dispatcher.create_* calls into on_create_context Signed-off-by: Daniel Grimm <[email protected]>
1 parent fa8512a commit f22f308

File tree

5 files changed

+44
-64
lines changed

5 files changed

+44
-64
lines changed

examples/http_auth_random.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl RootContext for HttpAuthRandomRoot {
3131
ContextType::HttpContext
3232
}
3333

34-
fn create_http_context(&self, _root_context_id: u32, _context_id: u32) -> Box<dyn HttpContext> {
34+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
3535
Box::new(HttpAuthRandom)
3636
}
3737
}

examples/http_body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl RootContext for HttpBodyRoot {
2929
ContextType::HttpContext
3030
}
3131

32-
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
32+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
3333
Box::new(HttpBody)
3434
}
3535
}

examples/http_headers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl RootContext for HttpHeadersRoot {
3131
ContextType::HttpContext
3232
}
3333

34-
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
34+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
3535
Box::new(HttpHeaders {
3636
context_id: _context_id,
3737
})

src/dispatcher.rs

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -60,51 +60,6 @@ impl Dispatcher {
6060
self.new_root.set(Some(callback));
6161
}
6262

63-
fn create_root_context(&self, context_id: u32) {
64-
let new_context = match self.new_root.get() {
65-
Some(f) => f(context_id),
66-
None => Box::new(NoopRoot),
67-
};
68-
if self
69-
.roots
70-
.borrow_mut()
71-
.insert(context_id, new_context)
72-
.is_some()
73-
{
74-
panic!("duplicate context_id")
75-
}
76-
}
77-
78-
fn create_stream_context(&self, context_id: u32, root_context_id: u32) {
79-
let new_context = match self.roots.borrow().get(&root_context_id) {
80-
Some(root_context) => root_context.create_stream_context(context_id, root_context_id),
81-
None => panic!("invalid root_context_id"),
82-
};
83-
if self
84-
.streams
85-
.borrow_mut()
86-
.insert(context_id, new_context)
87-
.is_some()
88-
{
89-
panic!("duplicate context_id")
90-
}
91-
}
92-
93-
fn create_http_context(&self, context_id: u32, root_context_id: u32) {
94-
let new_context = match self.roots.borrow().get(&root_context_id) {
95-
Some(root_context) => root_context.create_http_context(context_id, root_context_id),
96-
None => panic!("invalid root_context_id"),
97-
};
98-
if self
99-
.http_streams
100-
.borrow_mut()
101-
.insert(context_id, new_context)
102-
.is_some()
103-
{
104-
panic!("duplicate context_id")
105-
}
106-
}
107-
10863
fn register_callout(&self, token_id: u32) {
10964
if self
11065
.callouts
@@ -118,17 +73,46 @@ impl Dispatcher {
11873

11974
fn on_create_context(&self, context_id: u32, root_context_id: u32) {
12075
if root_context_id == 0 {
121-
self.create_root_context(context_id);
76+
let root_context = match self.new_root.get() {
77+
Some(f) => f(context_id),
78+
None => Box::new(NoopRoot),
79+
};
80+
if self
81+
.roots
82+
.borrow_mut()
83+
.insert(context_id, root_context)
84+
.is_some()
85+
{
86+
panic!("duplicate context_id")
87+
}
12288
} else if let Some(root_context) = self.roots.borrow().get(&root_context_id) {
12389
match root_context.get_type() {
124-
ContextType::HttpContext => self.create_http_context(context_id, root_context_id),
90+
ContextType::HttpContext => {
91+
let http_context = root_context.create_http_context(context_id);
92+
if self
93+
.http_streams
94+
.borrow_mut()
95+
.insert(context_id, http_context)
96+
.is_some()
97+
{
98+
panic!("duplicate context_id")
99+
}
100+
}
125101
ContextType::StreamContext => {
126-
self.create_stream_context(context_id, root_context_id)
102+
let stream_context = root_context.create_stream_context(context_id);
103+
if self
104+
.streams
105+
.borrow_mut()
106+
.insert(context_id, stream_context)
107+
.is_some()
108+
{
109+
panic!("duplicate context_id")
110+
}
127111
}
128-
ContextType::RootContext => panic!("missing constructors"),
112+
ContextType::RootContext => panic!("missing ContextType on root_context"),
129113
}
130114
} else {
131-
panic!("missing constructors")
115+
panic!("invalid root_context_id");
132116
}
133117
}
134118

src/traits.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,11 @@ pub trait RootContext: Context {
122122

123123
fn on_log(&mut self) {}
124124

125-
fn create_http_context(&self, _context_id: u32, _root_context_id: u32) -> Box<dyn HttpContext> {
125+
fn create_http_context(&self, _context_id: u32) -> Box<dyn HttpContext> {
126126
Box::new(EmptyHttpContext)
127127
}
128128

129-
fn create_stream_context(
130-
&self,
131-
_context_id: u32,
132-
_root_context_id: u32,
133-
) -> Box<dyn StreamContext> {
129+
fn create_stream_context(&self, _context_id: u32) -> Box<dyn StreamContext> {
134130
Box::new(EmptyStreamContext)
135131
}
136132

@@ -175,11 +171,6 @@ pub trait StreamContext: Context {
175171
fn on_log(&mut self) {}
176172
}
177173

178-
struct EmptyHttpContext;
179-
180-
impl HttpContext for EmptyHttpContext {}
181-
impl Context for EmptyHttpContext {}
182-
183174
struct EmptyStreamContext;
184175

185176
impl StreamContext for EmptyStreamContext {}
@@ -329,3 +320,8 @@ pub trait HttpContext: Context {
329320

330321
fn on_log(&mut self) {}
331322
}
323+
324+
struct EmptyHttpContext;
325+
326+
impl HttpContext for EmptyHttpContext {}
327+
impl Context for EmptyHttpContext {}

0 commit comments

Comments
 (0)