Skip to main content

fbthrift/
context_stack.rs

1/*
2 * Copyright (c) Facebook, Inc. and its 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::thrift_protocol::ProtocolID;
22
23pub struct SerializedMessage<'a, Name: ?Sized, Buffer> {
24    pub protocol: ProtocolID,
25    pub buffer: PhantomData<Buffer>, // Buffer,
26    pub method_name: &'a Name,
27}
28
29pub trait ContextStack {
30    /// Type for method names
31    type Name: ?Sized;
32    /// Type for buffers
33    type Buffer;
34
35    /// Called before the request is read.
36    fn pre_read(&mut self) -> Result<(), Error>;
37
38    /// Called before post_read after reading arguments (server) / after reading
39    /// reply (client), with the actual (unparsed, serialized) data.
40    fn on_read_data(
41        &mut self,
42        msg: &SerializedMessage<Self::Name, Self::Buffer>,
43    ) -> Result<(), Error>;
44
45    /// Called after the request is read.
46    fn post_read(&mut self, bytes: u32) -> Result<(), Error>;
47
48    /// Called before a response is written.
49    fn pre_write(&mut self) -> Result<(), Error>;
50
51    /// Called before post_write, after serializing response (server) / after
52    /// serializing request (client), with the actual (serialized) data.
53    fn on_write_data(
54        &mut self,
55        msg: &SerializedMessage<Self::Name, Self::Buffer>,
56    ) -> Result<(), Error>;
57
58    /// Called after a response a written.
59    fn post_write(&mut self, bytes: u32) -> Result<(), Error>;
60}
61
62pub struct DummyContextStack<Name: ?Sized, Buffer> {
63    _phantom: PhantomData<(Buffer, Name)>,
64}
65
66impl<Name: ?Sized, Buffer> DummyContextStack<Name, Buffer> {
67    pub fn new() -> Self {
68        Self {
69            _phantom: PhantomData,
70        }
71    }
72}
73
74impl<Name: ?Sized, Buffer> ContextStack for DummyContextStack<Name, Buffer> {
75    type Name = Name;
76    type Buffer = Buffer;
77
78    fn pre_read(&mut self) -> Result<(), Error> {
79        Ok(())
80    }
81
82    fn on_read_data(
83        &mut self,
84        _msg: &SerializedMessage<Self::Name, Self::Buffer>,
85    ) -> Result<(), Error> {
86        Ok(())
87    }
88
89    fn post_read(&mut self, _bytes: u32) -> Result<(), Error> {
90        Ok(())
91    }
92
93    fn pre_write(&mut self) -> Result<(), Error> {
94        Ok(())
95    }
96
97    fn on_write_data(
98        &mut self,
99        _msg: &SerializedMessage<Self::Name, Self::Buffer>,
100    ) -> Result<(), Error> {
101        Ok(())
102    }
103
104    fn post_write(&mut self, _bytes: u32) -> Result<(), Error> {
105        Ok(())
106    }
107}
108
109fn _assert_context_stack(_: &impl ContextStack) {}
110
111#[test]
112fn check_unsized() {
113    _assert_context_stack(&DummyContextStack::<std::ffi::CStr, Vec<u8>>::new());
114}