fbthrift_git/framing.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::io::Cursor;
18
19use bytes::Bytes;
20use bytes::BytesMut;
21
22use crate::bufext::BufExt;
23use crate::bufext::BufMutExt;
24
25/// Helper type alias to get encoding buffer type
26pub type FramingEncoded<F> = <F as Framing>::EncBuf;
27
28/// Helper type alias to get the type of the finalized encoded buffer
29pub type FramingEncodedFinal<F> = <<F as Framing>::EncBuf as BufMutExt>::Final;
30
31/// Helper type alias to get the buffer to use as input to decoding
32pub type FramingDecoded<F> = <F as Framing>::DecBuf;
33
34/// Trait describing the in-memory frames the transport uses for Protocol messages.
35pub trait Framing {
36 /// Buffer type we encode into
37 type EncBuf: BufMutExt + Send + 'static;
38
39 /// Buffer type we decode from
40 type DecBuf: BufExt + Send + 'static;
41
42 /// Allocate a new encoding buffer with a given capacity
43 /// FIXME: need &self?
44 fn enc_with_capacity(cap: usize) -> Self::EncBuf;
45}
46
47impl Framing for Bytes {
48 type EncBuf = BytesMut;
49 type DecBuf = Cursor<Bytes>;
50
51 fn enc_with_capacity(cap: usize) -> Self::EncBuf {
52 BytesMut::with_capacity(cap)
53 }
54}