fbthrift_git/
context_stack.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::marker::PhantomData;
18
19use anyhow::Error;
20
21use crate::bufext::BufExt;
22use crate::framing::Framing;
23use crate::framing::FramingDecoded;
24use crate::framing::FramingEncodedFinal;
25use crate::thrift_protocol::ProtocolID;
26
27pub struct SerializedMessage<'a, Name: ?Sized, Buffer> {
28    pub protocol: ProtocolID,
29    pub buffer: Buffer,
30    pub method_name: &'a Name,
31}
32
33pub trait ContextStack {
34    /// Type for method names
35    type Name: ?Sized;
36    /// Type for serialized read and write buffers
37    type Frame: Framing;
38
39    /// Called before the request is read.
40    fn pre_read(&mut self) -> Result<(), Error>;
41
42    /// Called before post_read after reading arguments (server) / after reading
43    /// reply (client), with the actual (unparsed, serialized) data.
44    fn on_read_data(
45        &mut self,
46        msg: SerializedMessage<Self::Name, FramingDecoded<Self::Frame>>,
47    ) -> Result<(), Error>;
48
49    /// Called after the request is read.
50    fn post_read(&mut self, bytes: u32) -> Result<(), Error>;
51
52    /// Called before a response is written.
53    fn pre_write(&mut self) -> Result<(), Error>;
54
55    /// Called before post_write, after serializing response (server) / after
56    /// serializing request (client), with the actual (serialized) data.
57    fn on_write_data(
58        &mut self,
59        msg: SerializedMessage<Self::Name, FramingEncodedFinal<Self::Frame>>,
60    ) -> Result<(), Error>;
61
62    /// Called after a response a written.
63    fn post_write(&mut self, bytes: u32) -> Result<(), Error>;
64}
65
66pub struct DummyContextStack<Name: ?Sized, Frame> {
67    _phantom: PhantomData<(Frame, Name)>,
68}
69
70impl<Name: ?Sized, Frame> DummyContextStack<Name, Frame> {
71    pub fn new() -> Self {
72        Self {
73            _phantom: PhantomData,
74        }
75    }
76}
77
78impl<Name: ?Sized, Frame: Framing> ContextStack for DummyContextStack<Name, Frame>
79where
80    FramingEncodedFinal<Frame>: BufExt,
81{
82    type Name = Name;
83    type Frame = Frame;
84
85    fn pre_read(&mut self) -> Result<(), Error> {
86        Ok(())
87    }
88
89    fn on_read_data(
90        &mut self,
91        _msg: SerializedMessage<Self::Name, FramingDecoded<Self::Frame>>,
92    ) -> Result<(), Error> {
93        Ok(())
94    }
95
96    fn post_read(&mut self, _bytes: u32) -> Result<(), Error> {
97        Ok(())
98    }
99
100    fn pre_write(&mut self) -> Result<(), Error> {
101        Ok(())
102    }
103
104    fn on_write_data(
105        &mut self,
106        _msg: SerializedMessage<Self::Name, FramingEncodedFinal<Self::Frame>>,
107    ) -> Result<(), Error> {
108        Ok(())
109    }
110
111    fn post_write(&mut self, _bytes: u32) -> Result<(), Error> {
112        Ok(())
113    }
114}
115
116#[cfg(test)]
117mod test {
118    use bytes::Bytes;
119
120    use super::*;
121
122    fn assert_context_stack(_: &impl ContextStack) {}
123
124    #[test]
125    fn check_unsized() {
126        assert_context_stack(&DummyContextStack::<std::ffi::CStr, Bytes>::new());
127    }
128}