Skip to main content

rocketmq_protocol/protocol/remoting_command/
legacy_api.rs

1// Copyright 2026 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use bytes::Bytes;
16use bytes::BytesMut;
17
18use super::RemotingCommand;
19use crate::protocol::command_custom_header::CommandCustomHeader;
20
21impl RemotingCommand {
22    /// Legacy ambiguous-success response factory.
23    ///
24    /// New code should call [`Self::create_success_response_command`] so the
25    /// response intent is visible during review. Call
26    /// [`Self::create_java_default_error_response_command`] when matching
27    /// Java's unset-response behavior instead.
28    #[deprecated(
29        note = "use create_success_response_command for SUCCESS or create_java_default_error_response_command for Java-compatible unset errors"
30    )]
31    pub fn create_response_command() -> Self {
32        Self::create_success_response_command()
33    }
34
35    /// Legacy ambiguous-success typed-header response factory.
36    ///
37    /// New code should call [`Self::create_success_response_command_with_header`].
38    /// Call [`Self::create_java_default_error_response_command_with_header`]
39    /// when matching Java's unset-response behavior instead.
40    #[deprecated(
41        note = "use create_success_response_command_with_header for SUCCESS or create_java_default_error_response_command_with_header for Java-compatible unset errors"
42    )]
43    pub fn create_response_command_with_header(header: impl CommandCustomHeader + Sync + Send + 'static) -> Self {
44        Self::create_success_response_command_with_header(header)
45    }
46
47    /// Convert custom header to network format (merge into ext_fields)
48    #[inline]
49    pub fn make_custom_header_to_net(&mut self) {
50        let _ = self.try_make_custom_header_to_net();
51    }
52
53    #[inline]
54    pub fn materialize_custom_header_to_ext_fields(&mut self) {
55        let _ = self.try_make_custom_header_to_net();
56    }
57
58    #[deprecated(
59        since = "1.0.0",
60        note = "use try_read_custom_header_ref; this compatibility alias is fallible despite its historical unchecked name"
61    )]
62    pub fn read_custom_header_ref_unchecked<T>(&self) -> rocketmq_error::Result<&T>
63    where
64        T: CommandCustomHeader + Sync + Send + 'static,
65    {
66        self.try_read_custom_header_ref::<T>()
67    }
68
69    /// Compatibility name for the former shared-reference mutation escape.
70    ///
71    /// Mutation now requires exclusive access to this command and succeeds only
72    /// when the safely shared header is uniquely owned.
73    #[deprecated(note = "use read_custom_header_mut; shared-reference mutation is no longer supported")]
74    pub fn read_custom_header_mut_from_ref<T>(&mut self) -> Option<&mut T>
75    where
76        T: CommandCustomHeader + Sync + Send + 'static,
77    {
78        self.try_read_custom_header_mut::<T>().ok()
79    }
80
81    #[deprecated(
82        since = "1.0.0",
83        note = "use try_read_custom_header_mut; this compatibility alias is fallible despite its historical unchecked name"
84    )]
85    pub fn read_custom_header_mut_unchecked<T>(&mut self) -> rocketmq_error::Result<&mut T>
86    where
87        T: CommandCustomHeader + Sync + Send + 'static,
88    {
89        self.try_read_custom_header_mut::<T>()
90    }
91
92    pub fn read_custom_header_ref<T>(&self) -> Option<&T>
93    where
94        T: CommandCustomHeader + Sync + Send + 'static,
95    {
96        self.try_read_custom_header_ref::<T>().ok()
97    }
98
99    pub fn read_custom_header_mut<T>(&mut self) -> Option<&mut T>
100    where
101        T: CommandCustomHeader + Sync + Send + 'static,
102    {
103        self.try_read_custom_header_mut::<T>().ok()
104    }
105
106    /// Encode header with optimized path selection
107    #[inline]
108    pub fn header_encode(&mut self) -> Option<Bytes> {
109        self.try_header_encode().ok()
110    }
111
112    #[inline]
113    pub fn fast_header_encode(&mut self, dst: &mut BytesMut) {
114        let _ = self.try_fast_header_encode(dst);
115    }
116
117    /// Encode header with body length information
118    #[inline]
119    pub fn encode_header(&mut self) -> Option<Bytes> {
120        let body_length = self.body.as_ref().map_or(0, |b| b.len());
121        self.try_encode_header_with_body_length(body_length).ok()
122    }
123
124    /// Optimized header encoding with pre-calculated capacity
125    #[inline]
126    pub fn encode_header_with_body_length(&mut self, body_length: usize) -> Option<Bytes> {
127        self.try_encode_header_with_body_length(body_length).ok()
128    }
129}