Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ env:
sudo: false

script:
- rustup target add wasm32-unknown-unknown
- cargo build -v
- cargo check --target wasm32-unknown-unknown
- cargo test -v --no-fail-fast
- cargo test -v --no-fail-fast --features u2i
- cd serde-tests && cargo test -v --no-fail-fast
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ serde = "1.0"
serde_json = { version = "1.0", features = ["preserve_order"] }
time = "0.1"
linked-hash-map = "0.5"
hostname = "0.1"
hex = "0.3"
md5 = "0.3"
try_from = "0.2"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
hostname = "0.1"
libc = "0.2"

[dev-dependencies]
assert_matches = "1.2"
serde_derive = "1.0"
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
extern crate byteorder;
extern crate chrono;
extern crate hex;
extern crate hostname;
extern crate libc;
extern crate linked_hash_map;
extern crate rand;
extern crate serde;
Expand All @@ -56,6 +54,11 @@ extern crate md5;
extern crate time;
extern crate try_from;

#[cfg(not(target_arch = "wasm32"))]
extern crate hostname;
#[cfg(not(target_arch = "wasm32"))]
extern crate libc;

pub use self::bson::{Array, Bson, Document, TimeStamp, UtcDateTime};
pub use self::decoder::{decode_document, decode_document_utf8_lossy, from_bson, Decoder, DecoderError, DecoderResult};
pub use self::encoder::{encode_document, to_bson, Encoder, EncoderError, EncoderResult};
Expand Down
39 changes: 36 additions & 3 deletions src/oid.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
//! ObjectId

use libc;

use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use std::{error, fmt, io, result};

use byteorder::{BigEndian, ByteOrder, LittleEndian};
use md5;

use hex::{self, FromHexError};

use rand::{thread_rng, Rng};

#[cfg(not(target_arch = "wasm32"))]
use hostname::get_hostname;
#[cfg(not(target_arch = "wasm32"))]
use libc;
#[cfg(not(target_arch = "wasm32"))]
use md5;

use time;

const TIMESTAMP_SIZE: usize = 4;
Expand Down Expand Up @@ -197,6 +200,7 @@ impl ObjectId {

// Generates a new machine id represented as an MD5-hashed 3-byte-encoded hostname string.
// Represented in Little Endian.
#[cfg(not(target_arch = "wasm32"))]
fn gen_machine_id() -> Result<[u8; 3]> {
// Short-circuit if machine id has already been calculated.
// Since the generated machine id is not variable, arising race conditions
Expand Down Expand Up @@ -230,15 +234,43 @@ impl ObjectId {
Ok(vec)
}

// In case of wasm compilation generates a random 3-byte array.
#[cfg(target_arch = "wasm32")]
fn gen_machine_id() -> Result<[u8; 3]> {
// Short-circuit if machine id has already been calculated.
// Since the generated machine id is not variable, arising race conditions
// will have the same MACHINE_BYTES result.
unsafe {
if let Some(bytes) = MACHINE_BYTES.as_ref() {
return Ok(bytes.clone());
}
}

let mut rng = rand::thread_rng();
let vec: [u8; 3] = [rng.gen(), rng.gen(), rng.gen()];

unsafe { MACHINE_BYTES = Some(vec) };
Ok(vec)
}

// Gets the process ID and returns it as a 2-byte array.
// Represented in Little Endian.
#[cfg(not(target_arch = "wasm32"))]
fn gen_process_id() -> [u8; 2] {
let pid = unsafe { libc::getpid() as u16 };
let mut buf: [u8; 2] = [0; 2];
LittleEndian::write_u16(&mut buf, pid);
buf
}

// In case of wasm return a random 2-byte array.
#[cfg(target_arch = "wasm32")]
fn gen_process_id() -> [u8; 2] {
let mut rng = rand::thread_rng();
let vec: [u8; 2] = [rng.gen(), rng.gen()];
vec
}

// Gets an incremental 3-byte count.
// Represented in Big Endian.
fn gen_count() -> Result<[u8; 3]> {
Expand Down Expand Up @@ -279,6 +311,7 @@ impl fmt::Debug for ObjectId {
}

#[test]
#[cfg(not(target_arch = "wasm33"))]
fn pid_generation() {
let pid = unsafe { libc::getpid() as u16 };
let generated = ObjectId::gen_process_id();
Expand Down