Skip to content

Tweak more Closure docs #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2018
Merged
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
44 changes: 41 additions & 3 deletions src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
//! This module defines the `Closure` type which is used to pass "owned
//! closures" from Rust to JS. Some more details can be found on the `Closure`
//! type itself.
<<<<<<< a37fa45100baddc36695cbf01110bcd9ab638065
=======
//!
//! The `nightly` feature is required for the `Closure` type to be used.
>>>>>>> Tweak more `Closure` docs

use std::cell::UnsafeCell;
#[cfg(feature = "nightly")]
Expand Down Expand Up @@ -37,6 +42,8 @@ use throw;
///
/// # Example
///
/// Sample usage of `Closure` to invoke the `setTimeout` API.
///
/// ```rust,no_run
/// #[wasm_bindgen]
/// extern {
Expand All @@ -51,11 +58,11 @@ use throw;
///
/// #[wasm_bindgen]
/// pub fn run() -> ClosureHandle {
/// // First up we use `Closure::new` to wrap up a Rust closure and create
/// // First up we use `Closure::wrap` to wrap up a Rust closure and create
/// a JS closure.
/// let cb = Closure::new(|| {
/// let cb = Closure::wrap(Box::new(move || {
/// log("timeout elapsed!");
/// });
/// }) as Box<FnMut()>);
///
/// // Next we pass this via reference to the `setTimeout` function, and
/// // `setTimeout` gets a handle to the corresponding JS closure.
Expand All @@ -67,6 +74,37 @@ use throw;
/// ClosureHandle(cb)
/// }
/// ```
///
/// Sample usage of the same example as above except using `web_sys` instead
///
/// ```rust,no_run
/// extern crate wasm_bindgen;
/// extern crate web_sys;
///
/// use wasm_bindgen::JsCast;
///
/// #[wasm_bindgen]
/// pub struct ClosureHandle(Closure<FnMut()>);
///
/// #[wasm_bindgen]
/// pub fn run() -> ClosureHandle {
/// let cb = Closure::wrap(Box::new(move || {
/// web_sys::console::log_1(&"timeout elapsed!".into());
/// }) as Box<FnMut()>);
///
/// let window = web_sys::window().unwrap();
/// window.set_timeout_with_callback_and_timeout_and_arguments_0(
/// // Note this method call, which uses `as_ref()` to get a `JsValue`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

/// // from our `Closure` which is then converted to a `&Function`
/// // using the `JsCast::unchecked_ref` function.
/// cb.as_ref().unchecked_ref(),
/// 1_000,
/// );
///
/// // same as above
/// ClosureHandle(cb)
/// }
/// ```
pub struct Closure<T: ?Sized> {
js: ManuallyDrop<JsValue>,
_keep_this_data_alive: Rc<UnsafeCell<Box<T>>>,
Expand Down