fbthrift_git/
request_context.rs1use std::marker::PhantomData;
18
19use anyhow::Error;
20
21use crate::bufext::BufExt;
22use crate::context_stack::ContextStack;
23use crate::context_stack::DummyContextStack;
24use crate::framing::Framing;
25use crate::framing::FramingEncodedFinal;
26
27pub trait RequestContext {
28 type ContextStack: ContextStack;
29 type Name: ?Sized;
30
31 fn get_context_stack(
34 &self,
35 service_name: &'static Self::Name,
36 fn_name: &'static Self::Name,
37 ) -> Result<Self::ContextStack, Error>;
38
39 fn set_user_exception_header(&self, ex_type: &str, ex_reason: &str) -> Result<(), Error>;
40}
41
42pub struct DummyRequestContext<Name: ?Sized, Frame> {
43 _phantom: PhantomData<(Frame, Name)>,
44}
45
46impl<Name: ?Sized, Frame> DummyRequestContext<Name, Frame> {
47 pub fn new() -> Self {
48 Self {
49 _phantom: PhantomData,
50 }
51 }
52}
53
54impl<Name: ?Sized, Frame: Framing> RequestContext for DummyRequestContext<Name, Frame>
55where
56 FramingEncodedFinal<Frame>: BufExt,
57{
58 type ContextStack = crate::context_stack::DummyContextStack<Name, Frame>;
59 type Name = Name;
60
61 fn get_context_stack(
62 &self,
63 _service_name: &Self::Name,
64 _fn_name: &Self::Name,
65 ) -> Result<Self::ContextStack, Error> {
66 Ok(DummyContextStack::new())
67 }
68
69 fn set_user_exception_header(&self, _ex_type: &str, _ex_reason: &str) -> Result<(), Error> {
70 Ok(())
71 }
72}
73
74#[cfg(test)]
75mod test {
76 use bytes::Bytes;
77
78 use super::*;
79
80 fn assert_context_stack(_: &impl RequestContext) {}
81
82 #[test]
83 fn check_unsized() {
84 assert_context_stack(&DummyRequestContext::<std::ffi::CStr, Bytes>::new());
85 }
86}