Skip to content

Commit d8c12f7

Browse files
authored
RUST-942 Generate 5 random bytes instead of 3 for ObjectIds (#284)
1 parent 2295e19 commit d8c12f7

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

serde-tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ fn u2i() {
902902
"u_32": 1234_i64,
903903
"u_32_max": u32::MAX as i64,
904904
"u_64": 12345_i64,
905-
"i_64_max": i64::MAX as u64,
905+
"i_64_max": i64::MAX,
906906
};
907907

908908
run_test(&v, &expected, "u2i - valid");

src/oid.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ impl ObjectId {
172172
hex::encode(self.id)
173173
}
174174

175-
// Generates a new timestamp representing the current seconds since epoch.
176-
// Represented in Big Endian.
175+
/// Generates a new timestamp representing the current seconds since epoch.
176+
/// Represented in Big Endian.
177177
fn gen_timestamp() -> [u8; 4] {
178178
let timestamp: u32 = SystemTime::now()
179179
.duration_since(SystemTime::UNIX_EPOCH)
@@ -184,22 +184,17 @@ impl ObjectId {
184184
timestamp.to_be_bytes()
185185
}
186186

187-
// Generate a random 5-byte array.
187+
/// Generate a random 5-byte array.
188188
fn gen_process_id() -> [u8; 5] {
189189
lazy_static! {
190-
static ref BUF: [u8; 5] = {
191-
let rng = thread_rng().gen_range(0, MAX_U24) as u32;
192-
let mut buf: [u8; 5] = [0; 5];
193-
buf[0..4].copy_from_slice(&rng.to_be_bytes());
194-
buf
195-
};
190+
static ref BUF: [u8; 5] = thread_rng().gen();
196191
}
197192

198193
*BUF
199194
}
200195

201-
// Gets an incremental 3-byte count.
202-
// Represented in Big Endian.
196+
/// Gets an incremental 3-byte count.
197+
/// Represented in Big Endian.
203198
fn gen_count() -> [u8; 3] {
204199
let u_counter = OID_COUNTER.fetch_add(1, Ordering::SeqCst);
205200

0 commit comments

Comments
 (0)