Skip to main content

rocketmq_common/common/message/
message_flag.rs

1// Copyright 2023 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
15/// Message flags as a type-safe bitflag.
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17pub struct MessageFlag(i32);
18
19impl MessageFlag {
20    pub const NONE: Self = Self(0);
21    pub const COMPRESSED: Self = Self(1 << 0);
22    pub const MULTI_TAGS: Self = Self(1 << 1);
23    pub const TRANSACTION_PREPARED: Self = Self(1 << 2);
24    pub const TRANSACTION_COMMIT: Self = Self(1 << 3);
25    pub const TRANSACTION_ROLLBACK: Self = Self(1 << 4);
26
27    /// Creates an empty flag (no bits set).
28    #[inline]
29    pub const fn empty() -> Self {
30        Self(0)
31    }
32
33    /// Creates a new flag from raw value.
34    #[inline]
35    pub const fn from_bits(bits: i32) -> Self {
36        Self(bits)
37    }
38
39    /// Returns the raw flag value.
40    #[inline]
41    pub const fn bits(&self) -> i32 {
42        self.0
43    }
44
45    /// Checks if a flag is set.
46    #[inline]
47    pub const fn contains(&self, other: Self) -> bool {
48        (self.0 & other.0) == other.0
49    }
50
51    /// Sets a flag.
52    #[inline]
53    pub fn insert(&mut self, other: Self) {
54        self.0 |= other.0;
55    }
56
57    /// Removes a flag.
58    #[inline]
59    pub fn remove(&mut self, other: Self) {
60        self.0 &= !other.0;
61    }
62
63    /// Returns true if the compressed flag is set.
64    #[inline]
65    pub fn is_compressed(&self) -> bool {
66        self.contains(Self::COMPRESSED)
67    }
68
69    /// Returns true if the multi-tags flag is set.
70    #[inline]
71    pub fn is_multi_tags(&self) -> bool {
72        self.contains(Self::MULTI_TAGS)
73    }
74
75    /// Returns true if the transaction prepared flag is set.
76    #[inline]
77    pub fn is_transaction_prepared(&self) -> bool {
78        self.contains(Self::TRANSACTION_PREPARED)
79    }
80
81    /// Returns true if the transaction commit flag is set.
82    #[inline]
83    pub fn is_transaction_commit(&self) -> bool {
84        self.contains(Self::TRANSACTION_COMMIT)
85    }
86
87    /// Returns true if the transaction rollback flag is set.
88    #[inline]
89    pub fn is_transaction_rollback(&self) -> bool {
90        self.contains(Self::TRANSACTION_ROLLBACK)
91    }
92}
93
94impl std::ops::BitOr for MessageFlag {
95    type Output = Self;
96
97    fn bitor(self, rhs: Self) -> Self::Output {
98        Self(self.0 | rhs.0)
99    }
100}
101
102impl std::ops::BitOrAssign for MessageFlag {
103    fn bitor_assign(&mut self, rhs: Self) {
104        self.0 |= rhs.0;
105    }
106}
107
108impl std::ops::BitAnd for MessageFlag {
109    type Output = Self;
110
111    fn bitand(self, rhs: Self) -> Self::Output {
112        Self(self.0 & rhs.0)
113    }
114}
115
116impl std::ops::BitAndAssign for MessageFlag {
117    fn bitand_assign(&mut self, rhs: Self) {
118        self.0 &= rhs.0;
119    }
120}
121
122impl From<i32> for MessageFlag {
123    fn from(value: i32) -> Self {
124        Self(value)
125    }
126}
127
128impl From<MessageFlag> for i32 {
129    fn from(flag: MessageFlag) -> Self {
130        flag.0
131    }
132}