Skip to content

Commit

Permalink
Changing to use discard crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Mar 22, 2018
1 parent 104ffc6 commit 62f8f4b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 110 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ description = "A standard library for the client-side Web"
build = "build.rs"

[dependencies]
discard = "1.0.3"
serde = { version = "1", optional = true }
serde_json = { version = "1", optional = true }
futures = { version = "0.1.18", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ extern crate futures;
#[macro_use]
extern crate stdweb_derive;

extern crate discard;

#[macro_use]
mod webcore;
mod webapi;
Expand All @@ -166,7 +168,6 @@ pub use webcore::number::Number;
pub use webcore::object::Object;
pub use webcore::array::Array;
pub use webcore::symbol::Symbol;
pub use webcore::cancel::{Cancel, CancelOnDrop};

pub use webcore::unsafe_typed_array::UnsafeTypedArray;
pub use webcore::once::Once;
Expand Down
98 changes: 0 additions & 98 deletions src/webcore/cancel.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/webcore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod once;
pub mod instance_of;
pub mod reference_type;
pub mod promise;
pub mod cancel;

#[cfg(feature = "futures")]
pub mod promise_future;
Expand Down
20 changes: 12 additions & 8 deletions src/webcore/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std;
use webcore::once::Once;
use webcore::value::{Value, Reference};
use webcore::try_from::{TryInto, TryFrom};
use webcore::cancel::{Cancel, CancelOnDrop};
use discard::{Discard, DiscardOnDrop};

#[cfg(feature = "futures")]
use webcore::serialization::JsSerialize;
Expand All @@ -25,8 +25,8 @@ pub struct DoneHandle {
state: Value,
}

impl Cancel for DoneHandle {
fn cancel( &mut self ) {
impl Discard for DoneHandle {
fn discard( self ) {
js! { @(no_return)
var state = @{&self.state};
state.cancelled = true;
Expand Down Expand Up @@ -162,13 +162,17 @@ impl Promise {
/// If the `Promise` never succeeds / fails then the `callback` will never be called.
///
/// This method returns a [`DoneHandle`](struct.DoneHandle.html). When the [`DoneHandle`](struct.DoneHandle.html)
/// is cancelled it will drop the `callback` and the `callback` will never be called. This is useful if you are
/// is dropped it will drop the `callback` and the `callback` will never be called. This is useful if you are
/// no longer interested in the `Promise`'s result.
///
/// But if you *are* interested in the `Promise`'s result, then you either need to make sure to keep the handle
/// alive until after the callback is called, or you need to use the [`leak`](struct.CancelOnDrop.html#method.leak) method.
/// alive until after the callback is called, or you need to use the [`leak`](https://docs.rs/discard/1.*/discard/struct.DiscardOnDrop.html#method.leak)
/// function.
///
/// Cancelling the [`DoneHandle`](struct.DoneHandle.html) does ***not*** cancel the `Promise`, because promises
/// If you choose to leak the [`DoneHandle`](struct.DoneHandle.html) then it ***will*** leak the memory for the
/// callback, so only do that if absolutely need to.
///
/// Discarding the [`DoneHandle`](struct.DoneHandle.html) does ***not*** cancel the `Promise`, because promises
/// do not support cancellation.
///
/// # Examples
Expand All @@ -184,7 +188,7 @@ impl Promise {
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then)
// https://www.ecma-international.org/ecma-262/6.0/#sec-performpromisethen
pub fn done< A, B, F >( &self, callback: F ) -> CancelOnDrop< DoneHandle >
pub fn done< A, B, F >( &self, callback: F ) -> DiscardOnDrop< DoneHandle >
where A: TryFrom< Value >,
B: TryFrom< Value >,
// TODO these Debug constraints are only needed because of unwrap
Expand Down Expand Up @@ -229,7 +233,7 @@ impl Promise {
return state;
);

CancelOnDrop::new( DoneHandle {
DiscardOnDrop::new( DoneHandle {
state,
} )
}
Expand Down
4 changes: 2 additions & 2 deletions src/webcore/promise_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use webapi::error;
use futures::{Future, Poll, Async};
use futures::unsync::oneshot::Receiver;
use webcore::promise_executor::spawn;
use webcore::cancel::CancelOnDrop;
use discard::DiscardOnDrop;
use super::promise::{Promise, DoneHandle};


Expand All @@ -22,7 +22,7 @@ use super::promise::{Promise, DoneHandle};
/// ```
pub struct PromiseFuture< Value, Error = error::Error > {
pub(crate) future: Receiver< Result< Value, Error > >,
pub(crate) _done_handle: CancelOnDrop< DoneHandle >,
pub(crate) _done_handle: DiscardOnDrop< DoneHandle >,
}

impl PromiseFuture< (), () > {
Expand Down

0 comments on commit 62f8f4b

Please sign in to comment.