Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit 9e0b309

Browse files
authored
Use bitflags module for bitflags (#184)
1 parent 762489f commit 9e0b309

File tree

7 files changed

+58
-169
lines changed

7 files changed

+58
-169
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ keywords = ["mongo", "mongodb", "database", "bson", "nosql"]
1313
license = "Apache-2.0"
1414

1515
[dependencies]
16+
bitflags = "0.7.0"
1617
bson = "0.3.2"
1718
byteorder = "0.5.3"
1819
chrono = "0.2.25"

src/cursor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use bson::{self, Bson};
3030
use common::{ReadMode, ReadPreference};
3131
use pool::PooledStream;
3232
use time;
33-
use wire_protocol::flags::OpQueryFlags;
33+
use wire_protocol::flags::{self, OpQueryFlags};
3434
use wire_protocol::operations::Message;
3535

3636
use std::collections::vec_deque::VecDeque;
@@ -110,7 +110,7 @@ impl Cursor {
110110
Cursor::query(client.clone(),
111111
format!("{}.$cmd", db),
112112
1,
113-
OpQueryFlags::no_flags(),
113+
OpQueryFlags::empty(),
114114
0,
115115
0,
116116
doc,
@@ -234,7 +234,7 @@ impl Cursor {
234234
let new_flags = if !slave_ok {
235235
flags
236236
} else {
237-
OpQueryFlags { slave_ok: true, ..flags }
237+
flags | flags::SLAVE_OK
238238
};
239239

240240
// Send read_preference to the server based on the result from server selection.

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
//! pool. By default, each pool has a maximum of 5 concurrent open connections.
8989
9090
#[doc(html_root_url = "https://docs.rs/mongodb")]
91+
#[macro_use(bitflags)]
92+
extern crate bitflags;
9193
#[macro_use(bson, doc)]
9294
extern crate bson;
9395
extern crate byteorder;

src/wire_protocol/flags.rs

Lines changed: 38 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,44 @@
11
//! Operation flags.
22
use 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

11241
impl 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
}

src/wire_protocol/operations.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Message {
114114
-> Message {
115115
Message::OpReply {
116116
header: header,
117-
flags: OpReplyFlags::from_i32(flags),
117+
flags: OpReplyFlags::from_bits_truncate(flags),
118118
cursor_id: cursor_id,
119119
starting_from: starting_from,
120120
number_returned: number_returned,
@@ -305,7 +305,7 @@ impl Message {
305305
// Writes the null terminator for the collection name string.
306306
try!(buffer.write_u8(0));
307307

308-
try!(buffer.write_i32::<LittleEndian>(flags.to_i32()));
308+
try!(buffer.write_i32::<LittleEndian>(flags.bits()));
309309

310310
try!(Message::write_bson_document(buffer, selector));
311311
try!(Message::write_bson_document(buffer, update));
@@ -336,7 +336,7 @@ impl Message {
336336
-> Result<()> {
337337

338338
try!(header.write(buffer));
339-
try!(buffer.write_i32::<LittleEndian>(flags.to_i32()));
339+
try!(buffer.write_i32::<LittleEndian>(flags.bits()));
340340

341341
for byte in namespace.bytes() {
342342
try!(buffer.write_u8(byte));
@@ -385,7 +385,7 @@ impl Message {
385385
-> Result<()> {
386386

387387
try!(header.write(buffer));
388-
try!(buffer.write_i32::<LittleEndian>(flags.to_i32()));
388+
try!(buffer.write_i32::<LittleEndian>(flags.bits()));
389389

390390
for byte in namespace.bytes() {
391391
try!(buffer.write_u8(byte));

tests/client/cursor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn cursor_features() {
2323
assert!(coll.insert_many(docs, None).is_ok());
2424

2525
let doc = Document::new();
26-
let flags = OpQueryFlags::no_flags();
26+
let flags = OpQueryFlags::empty();
2727

2828
let result = Cursor::query(client.clone(),
2929
"test-client-cursor.cursor_test".to_owned(),

tests/client/wire_protocol.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn insert_single_key_doc() {
1616
let doc = doc! { "foo" => 42.0 };
1717

1818
let docs = vec![doc];
19-
let flags = OpInsertFlags::no_flags();
19+
let flags = OpInsertFlags::empty();
2020
let name = "test-client-wire_protocol-insert_single_key_doc.single_key".to_owned();
2121
let res = Message::new_insert(1, flags, name, docs);
2222

@@ -31,7 +31,7 @@ fn insert_single_key_doc() {
3131
};
3232

3333
let doc = Document::new();
34-
let flags = OpQueryFlags::no_flags();
34+
let flags = OpQueryFlags::empty();
3535
let name = "test-client-wire_protocol-insert_single_key_doc.single_key".to_owned();
3636
let res = Message::new_query(1, flags, name, 0, 0, doc, None);
3737

@@ -80,7 +80,7 @@ fn insert_multi_key_doc() {
8080
};
8181

8282
let docs = vec![doc];
83-
let flags = OpInsertFlags::no_flags();
83+
let flags = OpInsertFlags::empty();
8484
let name = "test-client-wire_protocol-insert_multi_key_doc.multi_key".to_owned();
8585
let res = Message::new_insert(1, flags, name, docs);
8686

@@ -95,7 +95,7 @@ fn insert_multi_key_doc() {
9595
};
9696

9797
let doc = Document::new();
98-
let flags = OpQueryFlags::no_flags();
98+
let flags = OpQueryFlags::empty();
9999
let name = "test-client-wire_protocol-insert_multi_key_doc.multi_key".to_owned();
100100
let res = Message::new_query(1, flags, name, 0, 0, doc, None);
101101

@@ -153,7 +153,7 @@ fn insert_docs() {
153153
};
154154

155155
let docs = vec![doc1, doc2];
156-
let flags = OpInsertFlags::no_flags();
156+
let flags = OpInsertFlags::empty();
157157
let name = "test-client-wire_protocol-insert_docs.multi_doc".to_owned();
158158
let res = Message::new_insert(1, flags, name, docs);
159159

@@ -168,7 +168,7 @@ fn insert_docs() {
168168
};
169169

170170
let doc = Document::new();
171-
let flags = OpQueryFlags::no_flags();
171+
let flags = OpQueryFlags::empty();
172172
let name = "test-client-wire_protocol-insert_docs.multi_doc".to_owned();
173173
let res = Message::new_query(1, flags, name, 0, 0, doc, None);
174174

@@ -226,7 +226,7 @@ fn insert_update_then_query() {
226226
let doc = doc! { "foo" => 42.0 };
227227

228228
let docs = vec![doc];
229-
let flags = OpInsertFlags::no_flags();
229+
let flags = OpInsertFlags::empty();
230230
let name = "test-client-wire_protocol-insert_update_then_query.update".to_owned();
231231
let res = Message::new_insert(1, flags, name, docs);
232232

@@ -244,7 +244,7 @@ fn insert_update_then_query() {
244244

245245
let update = doc! { "foo" => "bar" };
246246

247-
let flags = OpUpdateFlags::no_flags();
247+
let flags = OpUpdateFlags::empty();
248248
let name = "test-client-wire_protocol-insert_update_then_query.update".to_owned();
249249
let res = Message::new_update(2, name, flags, selector, update);
250250

@@ -259,7 +259,7 @@ fn insert_update_then_query() {
259259
};
260260

261261
let doc = Document::new();
262-
let flags = OpQueryFlags::no_flags();
262+
let flags = OpQueryFlags::empty();
263263
let name = "test-client-wire_protocol-insert_update_then_query.update".to_owned();
264264
let res = Message::new_query(3, flags, name, 0, 0, doc, None);
265265

0 commit comments

Comments
 (0)