diff --git a/.travis.yml b/.travis.yml index 6e6daa6c..a790b7c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Cargo.toml b/Cargo.toml index f7b4e98b..1cb18add 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index c7b672e8..f74e3438 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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}; diff --git a/src/oid.rs b/src/oid.rs index ec1fbb4c..348a7fb6 100644 --- a/src/oid.rs +++ b/src/oid.rs @@ -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; @@ -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 @@ -230,8 +234,28 @@ 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]; @@ -239,6 +263,14 @@ impl ObjectId { 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]> { @@ -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();