11//! Operation flags.
22use coll:: options:: { CursorType , FindOptions } ;
33
4- /// Represents the bit vector of options for an OP_REPLY message.
5- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
6- pub struct OpReplyFlags {
7- pub cursor_not_found : bool , // Bit 0
8- pub query_failure : bool , // Bit 1
9- pub await_capable : bool , /* Bit 3
10- *
11- * All bits remaining must be 0 */
12- }
13-
14- impl OpReplyFlags {
15- /// Constructs a new struct from a bit vector of options.
16- ///
17- /// # Return value
18- ///
19- /// Returns the newly-created struct.
20- pub fn from_i32 ( i : i32 ) -> OpReplyFlags {
21- let cursor_not_found = ( i & 1 ) != 0 ;
22- let query_failure = ( i & ( 1 << 1 ) ) != 0 ;
23- let await_capable = ( i & ( 1 << 3 ) ) != 0 ;
24-
25- OpReplyFlags {
26- cursor_not_found : cursor_not_found,
27- query_failure : query_failure,
28- await_capable : await_capable,
29- }
4+ bitflags ! {
5+ /// Represents the bit vector of options for an OP_REPLY message.
6+ pub flags OpReplyFlags : i32 {
7+ const CURSOR_NOT_FOUND = 0b00000001 ,
8+ const QUERY_FAILURE = 0b00000010 ,
9+ const AWAIT_CAPABLE = 0b00001000 ,
3010 }
3111}
3212
33- /// Represents the bit vector of options for an OP_UPDATE message.
34- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
35- pub struct OpUpdateFlags {
36- pub upsert : bool , // Bit 0
37- pub multi_update : bool , /* Bit 1
38- *
39- * All bits remaining must be 0 */
40- }
41-
42- /// Represents the bit vector of flags for an OP_INSERT message.
43- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
44- pub struct OpInsertFlags {
45- pub continue_on_error : bool , /* Bit 0
46- *
47- * All bits remaining must be 0 */
48- }
49-
50- /// Represents the bit vector of flags for an OP_QUERY message.
51- #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
52- pub struct OpQueryFlags {
53- pub tailable_cursor : bool , // Bit 1
54- pub slave_ok : bool , // Bit 2
55- pub oplog_relay : bool , // Bit 3
56- pub no_cursor_timeout : bool , // Bit 4
57- pub await_data : bool , // Bit 5
58- pub exhaust : bool , // Bit 6
59- pub partial : bool , /* Bit 7
60- * All bits remaining must be 0 */
61- }
62-
63- impl OpUpdateFlags {
64- /// Constructs a new struct with all flags set to false.
65- ///
66- /// # Return value
67- ///
68- /// Returns the newly-created struct.
69- pub fn no_flags ( ) -> OpUpdateFlags {
70- OpUpdateFlags {
71- upsert : false ,
72- multi_update : false ,
73- }
74- }
75-
76- /// Gets the actual bit vector that the struct represents.
77- ///
78- /// # Return value
79- ///
80- /// Returns the bit vector as an i32.
81- pub fn to_i32 ( & self ) -> i32 {
82- let mut i: i32 = if self . upsert { 1 } else { 0 } ;
83-
84- if self . multi_update {
85- i |= 1 << 1 ;
86- }
87-
88- i
13+ bitflags ! {
14+ /// Represents the bit vector of options for an OP_UPDATE message.
15+ pub flags OpUpdateFlags : i32 {
16+ const UPSERT = 0b00000001 ,
17+ const MULTI_UPDATE = 0b00000010 ,
8918 }
9019}
9120
92- impl OpInsertFlags {
93- /// Constructs a new struct with all flags set to false.
94- ///
95- /// # Return value
96- ///
97- /// Returns the newly-created struct.
98- pub fn no_flags ( ) -> OpInsertFlags {
99- OpInsertFlags { continue_on_error : false }
21+ bitflags ! {
22+ /// Represents the bit vector of flags for an OP_INSERT message.
23+ pub flags OpInsertFlags : i32 {
24+ const CONTINUE_ON_ERROR = 0b00000001 ,
10025 }
26+ }
10127
102- /// Gets the actual bit vector that the struct represents.
103- ///
104- /// # Return value
105- ///
106- /// Returns the bit vector as an i32.
107- pub fn to_i32 ( & self ) -> i32 {
108- if self . continue_on_error { 1 } else { 0 }
28+ bitflags ! {
29+ /// Represents the bit vector of flags for an OP_QUERY message.
30+ pub flags OpQueryFlags : i32 {
31+ const TAILABLE_CURSOR = 0b00000010 ,
32+ const SLAVE_OK = 0b00000100 ,
33+ const OPLOG_RELAY = 0b00001000 ,
34+ const NO_CURSOR_TIMEOUT = 0b00010000 ,
35+ const AWAIT_DATA = 0b00100000 ,
36+ const EXHAUST = 0b01000000 ,
37+ const PARTIAL = 0b10000000 ,
10938 }
11039}
11140
11241impl OpQueryFlags {
113- /// Constructs a new struct with all flags set to false.
114- ///
115- /// # Return value
116- ///
117- /// Returns the newly-created struct.
118- pub fn no_flags ( ) -> OpQueryFlags {
119- OpQueryFlags {
120- tailable_cursor : false ,
121- slave_ok : false ,
122- oplog_relay : false ,
123- no_cursor_timeout : false ,
124- await_data : false ,
125- exhaust : false ,
126- partial : false ,
127- }
128- }
129-
13042 /// Constructs a new struct with flags based on a FindOptions struct.
13143 ///
13244 /// # Arguments
@@ -138,53 +50,27 @@ impl OpQueryFlags {
13850 ///
13951 /// Returns the newly created OpQueryFlags struct.
14052 pub fn with_find_options ( options : & FindOptions ) -> OpQueryFlags {
141- OpQueryFlags {
142- tailable_cursor : options. cursor_type != CursorType :: NonTailable ,
143- slave_ok : false ,
144- oplog_relay : options. op_log_replay ,
145- no_cursor_timeout : options. no_cursor_timeout ,
146- await_data : options. cursor_type == CursorType :: TailableAwait ,
147- exhaust : false ,
148- partial : options. allow_partial_results ,
149- }
150- }
151-
152- /// Gets the actual bit vector that the struct represents.
153- ///
154- /// # Return value
155- ///
156- /// Returns the bit vector as an i32.
157- pub fn to_i32 ( & self ) -> i32 {
158- let mut i = 0 as i32 ;
159-
160- if self . tailable_cursor {
161- i |= 1 << 1 ;
162- }
163-
164- if self . slave_ok {
165- i |= 1 << 2 ;
166- }
167-
168- if self . oplog_relay {
169- i |= 1 << 3 ;
53+ let mut flags = OpQueryFlags :: empty ( ) ;
54+ if options. cursor_type != CursorType :: NonTailable {
55+ flags. insert ( TAILABLE_CURSOR ) ;
17056 }
17157
172- if self . no_cursor_timeout {
173- i |= 1 << 4 ;
58+ if options . op_log_replay {
59+ flags . insert ( OPLOG_RELAY ) ;
17460 }
17561
176- if self . await_data {
177- i |= 1 << 5 ;
62+ if options . no_cursor_timeout {
63+ flags . insert ( NO_CURSOR_TIMEOUT ) ;
17864 }
17965
180- if self . exhaust {
181- i |= 1 << 6 ;
66+ if options . cursor_type == CursorType :: TailableAwait {
67+ flags . insert ( AWAIT_DATA ) ;
18268 }
18369
184- if self . partial {
185- i |= 1 << 7 ;
70+ if options . allow_partial_results {
71+ flags . insert ( PARTIAL ) ;
18672 }
18773
188- i
74+ flags
18975 }
19076}
0 commit comments