Alex Crichton 3efe51eb8b Reorganize and rewrite examples
This commit is a large-ish scale reorganization of our examples. The
main goal here is to have a dedicated section of the guide for example,
and all examples will be listed there. Each example's `README` is now
just boilerplate pointing at the guide along with a blurb about how to
run it.

Some examples like `math` and `smorgasboard` have been deleted as they
didn't really serve much purpose, and others like `closures` have been
rewritten with `web-sys` instead of hand-bound bindings.

Overall it's hoped that this puts us in a good and consistent state for
our examples, with all of them being described in the guide, excerpts
are in the guide, and they're all relatively idiomatically using
`web-sys`.
2018-09-20 16:45:30 -07:00

40 lines
1.2 KiB
Rust

extern crate humantime;
extern crate wasm_bindgen;
extern crate web_sys;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(a: &str);
}
macro_rules! console_log {
($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}
// Called by our JS entry point to run the example
#[wasm_bindgen]
pub fn run() {
let window = web_sys::window().expect("should have a window in this context");
let performance = window.performance().expect("performance should be available");
console_log!("the current time (in ms) is {}", performance.now());
let start = perf_to_system(performance.timing().request_start());
let end = perf_to_system(performance.timing().response_end());
console_log!("request started at {}", humantime::format_rfc3339(start));
console_log!("request ended at {}", humantime::format_rfc3339(end));
}
fn perf_to_system(amt: f64) -> SystemTime {
let secs = (amt as u64) / 1_000;
let nanos = ((amt as u32) % 1_000) * 1_000_000;
UNIX_EPOCH + Duration::new(secs, nanos)
}