Skip to content

Commit e1957e2

Browse files
committed
Renaming AutoCancel to CancelOnDrop, also reverting most changes
1 parent 196c2d9 commit e1957e2

File tree

7 files changed

+42
-42
lines changed

7 files changed

+42
-42
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub use webcore::number::Number;
166166
pub use webcore::object::Object;
167167
pub use webcore::array::Array;
168168
pub use webcore::symbol::Symbol;
169-
pub use webcore::cancel::{Cancel, AutoCancel};
169+
pub use webcore::cancel::{Cancel, CancelOnDrop};
170170

171171
pub use webcore::unsafe_typed_array::UnsafeTypedArray;
172172
pub use webcore::once::Once;
@@ -208,7 +208,7 @@ pub mod web {
208208
};
209209
pub use webapi::cross_origin_setting::CrossOriginSetting;
210210
pub use webapi::date::Date;
211-
pub use webapi::event_target::{IEventTarget, EventTarget, EventListener};
211+
pub use webapi::event_target::{IEventTarget, EventTarget, EventListenerHandle};
212212
pub use webapi::window::RequestAnimationFrameHandle;
213213
pub use webapi::node::{INode, Node, CloneKind};
214214
pub use webapi::element::{IElement, Element};

src/webapi/event_target.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@ use webcore::value::Reference;
44
use webcore::try_from::TryInto;
55
use webcore::reference_type::ReferenceType;
66
use webapi::event::{ConcreteEvent, IEvent};
7-
use webcore::cancel::{Cancel, AutoCancel};
87
use private::TODO;
98

109
/// A handle to a particular event listener.
11-
pub struct EventListener {
10+
pub struct EventListenerHandle {
1211
event_type: &'static str,
1312
reference: Reference,
1413
listener_reference: Reference
1514
}
1615

17-
impl fmt::Debug for EventListener {
16+
impl fmt::Debug for EventListenerHandle {
1817
fn fmt( &self, formatter: &mut fmt::Formatter ) -> fmt::Result {
19-
write!( formatter, "EventListener {{ event_type: {}, reference: {:?} }}", self.event_type, self.reference )
18+
write!( formatter, "EventListenerHandle {{ event_type: {}, reference: {:?} }}", self.event_type, self.reference )
2019
}
2120
}
2221

23-
impl Cancel for EventListener {
22+
impl EventListenerHandle {
2423
/// Removes the listener from the [IEventTarget](trait.IEventTarget.html) on
2524
/// which it was previously registered.
2625
///
2726
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener)
2827
// https://dom.spec.whatwg.org/#ref-for-dom-eventtarget-removeeventlistener%E2%91%A0
29-
fn cancel( &mut self ) {
28+
pub fn remove( self ) {
3029
js! { @(no_return)
3130
var listener = @{&self.listener_reference};
3231
@{&self.reference}.removeEventListener( @{self.event_type}, listener );
@@ -46,7 +45,7 @@ pub trait IEventTarget: ReferenceType {
4645
///
4746
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)
4847
// https://dom.spec.whatwg.org/#ref-for-dom-eventtarget-addeventlistener%E2%91%A0
49-
fn add_event_listener< T, F >( &self, listener: F ) -> AutoCancel< EventListener >
48+
fn add_event_listener< T, F >( &self, listener: F ) -> EventListenerHandle
5049
where T: ConcreteEvent, F: FnMut( T ) + 'static
5150
{
5251
let reference = self.as_ref();
@@ -57,11 +56,11 @@ pub trait IEventTarget: ReferenceType {
5756
return listener;
5857
}.try_into().unwrap();
5958

60-
AutoCancel::new( EventListener {
59+
EventListenerHandle {
6160
event_type: T::EVENT_TYPE,
6261
reference: reference.clone(),
6362
listener_reference: listener_reference
64-
} )
63+
}
6564
}
6665

6766
/// Dispatches an `Event` at this `EventTarget`, invoking the affected event listeners in the

src/webapi/mutation_observer.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use webcore::value::{Reference, Value, ConversionError};
33
use webapi::node_list::NodeList;
44
use webcore::try_from::{TryFrom, TryInto};
55
use webapi::node::{INode, Node};
6-
use webcore::cancel::{Cancel, AutoCancel};
76
use private::TODO;
87

98
/// Provides a way to receive notifications about changes to the DOM.
@@ -63,17 +62,17 @@ impl MutationObserver {
6362
///
6463
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#Constructor)
6564
// https://dom.spec.whatwg.org/#ref-for-dom-mutationobserver-mutationobserver
66-
pub fn new< F >( callback: F ) -> AutoCancel< MutationObserverHandle >
65+
pub fn new< F >( callback: F ) -> MutationObserverHandle
6766
where F: FnMut( Vec< MutationRecord >, Self ) + 'static {
6867
let callback_reference: Reference = js! ( return @{callback}; ).try_into().unwrap();
6968

70-
AutoCancel::new( MutationObserverHandle {
69+
MutationObserverHandle {
7170
callback_reference: callback_reference.clone(),
7271

7372
mutation_observer: js! (
7473
return new MutationObserver( @{callback_reference} );
7574
).try_into().unwrap(),
76-
} )
75+
}
7776
}
7877

7978
/// Starts observing changes to the `target`.
@@ -160,7 +159,7 @@ impl MutationObserver {
160159
/// This is created by the [`MutationObserver::new`](struct.MutationObserver.html#method.new) method, and
161160
/// it can use the same methods as [`MutationObserver`](struct.MutationObserver.html).
162161
///
163-
/// When the `MutationObserverHandle` is cancelled, the [`disconnect`](#method.disconnect)
162+
/// When the `MutationObserverHandle` is dropped, the [`disconnect`](#method.disconnect)
164163
/// method will automatically be called.
165164
#[ derive( Debug ) ]
166165
pub struct MutationObserverHandle {
@@ -177,9 +176,9 @@ impl std::ops::Deref for MutationObserverHandle {
177176
}
178177
}
179178

180-
impl Cancel for MutationObserverHandle {
179+
impl Drop for MutationObserverHandle {
181180
#[inline]
182-
fn cancel( &mut self ) {
181+
fn drop( &mut self ) {
183182
self.disconnect();
184183

185184
js! { @(no_return)

src/webapi/window.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ use webapi::location::Location;
77
use webapi::history::History;
88
use webcore::once::Once;
99
use webcore::value::Value;
10-
use webcore::cancel::{Cancel, AutoCancel};
1110

1211
/// A handle to a pending animation frame request.
1312
#[derive(Debug)]
1413
pub struct RequestAnimationFrameHandle(Value);
1514

16-
impl Cancel for RequestAnimationFrameHandle {
15+
impl RequestAnimationFrameHandle {
1716
/// Cancels an animation frame request.
1817
///
1918
/// [(Javascript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame)
20-
fn cancel( &mut self ) {
19+
pub fn cancel( self ) {
2120
js! { @(no_return)
2221
var val = @{&self.0};
2322
val.window.cancelAnimationFrame(val.request);
@@ -124,13 +123,13 @@ impl Window {
124123
///
125124
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)
126125
// https://html.spec.whatwg.org/#the-window-object:dom-window-requestanimationframe
127-
pub fn request_animation_frame< F: FnOnce(f64) + 'static>( &self, callback: F) -> AutoCancel< RequestAnimationFrameHandle > {
126+
pub fn request_animation_frame< F: FnOnce(f64) + 'static>( &self, callback: F) -> RequestAnimationFrameHandle {
128127
let values: Value = js!{
129128
var callback = @{Once(callback)};
130129
var request = @{self}.requestAnimationFrame(callback);
131130
return { request: request, callback: callback, window: @{self} };
132131
};
133-
AutoCancel::new( RequestAnimationFrameHandle(values) )
132+
RequestAnimationFrameHandle(values)
134133
}
135134

136135
/// Returns the global [History](struct.History.html) object, which provides methods to

src/webcore/cancel.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,36 @@ pub trait Cancel {
1111
///
1212
#[must_use = "
1313
14-
The AutoCancel is unused, which causes it to be immediately cancelled.
14+
The CancelOnDrop is unused, which causes it to be immediately cancelled.
1515
You probably don't want that to happen.
1616
1717
How to fix this:
18-
1) Store the AutoCancel in a variable or data structure
19-
2) Use .leak() which will cause it to not be cancelled (this *will* leak memory!)
18+
1) Store the CancelOnDrop in a variable or data structure.
19+
2) Use .leak() which will cause it to not be cancelled (this *will* leak memory!).
2020
2121
See the documentation for more details.
2222
"]
2323
#[derive(Debug)]
24-
pub struct AutoCancel< A: Cancel >( Option< A > );
24+
pub struct CancelOnDrop< A: Cancel >( Option< A > );
2525

26-
impl< A: Cancel > AutoCancel< A > {
26+
impl< A: Cancel > CancelOnDrop< A > {
2727
///
2828
#[inline]
2929
pub fn new( canceler: A ) -> Self {
30-
AutoCancel( Some( canceler ) )
30+
CancelOnDrop( Some( canceler ) )
3131
}
3232

3333
///
3434
#[inline]
3535
pub fn leak( mut self ) -> A {
36-
self.0.take().unwrap()
36+
match self.0.take() {
37+
Some(value) => value,
38+
None => unreachable!(),
39+
}
3740
}
3841
}
3942

40-
impl< A: Cancel > Drop for AutoCancel< A > {
43+
impl< A: Cancel > Drop for CancelOnDrop< A > {
4144
#[inline]
4245
fn drop( &mut self ) {
4346
match self.0 {
@@ -47,7 +50,7 @@ impl< A: Cancel > Drop for AutoCancel< A > {
4750
}
4851
}
4952

50-
impl< A: Cancel > Deref for AutoCancel< A > {
53+
impl< A: Cancel > Deref for CancelOnDrop< A > {
5154
type Target = A;
5255

5356
#[inline]
@@ -59,7 +62,7 @@ impl< A: Cancel > Deref for AutoCancel< A > {
5962
}
6063
}
6164

62-
impl< A: Cancel > DerefMut for AutoCancel< A > {
65+
impl< A: Cancel > DerefMut for CancelOnDrop< A > {
6366
#[inline]
6467
fn deref_mut( &mut self ) -> &mut Self::Target {
6568
match self.0 {
@@ -72,13 +75,13 @@ impl< A: Cancel > DerefMut for AutoCancel< A > {
7275

7376
#[cfg(test)]
7477
mod tests {
75-
use super::{Cancel, AutoCancel};
78+
use super::{Cancel, CancelOnDrop};
7679

7780
struct Foo( bool );
7881

7982
impl Foo {
80-
fn new() -> AutoCancel< Foo > {
81-
AutoCancel::new( Foo( false ) )
83+
fn new() -> CancelOnDrop< Foo > {
84+
CancelOnDrop::new( Foo( false ) )
8285
}
8386
}
8487

src/webcore/promise.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std;
22
use webcore::once::Once;
33
use webcore::value::{Value, Reference};
44
use webcore::try_from::{TryInto, TryFrom};
5-
use webcore::cancel::{Cancel, AutoCancel};
5+
use webcore::cancel::{Cancel, CancelOnDrop};
66

77
#[cfg(feature = "futures")]
88
use futures::unsync::oneshot::channel;
@@ -105,7 +105,7 @@ impl Promise {
105105
/// no longer interested in the `Promise`'s result.
106106
///
107107
/// But if you *are* interested in the `Promise`'s result, then you either need to make sure to keep the handle
108-
/// alive until after the callback is called, or you need to use the [`leak`](struct.AutoCancel.html#method.leak) method.
108+
/// alive until after the callback is called, or you need to use the [`leak`](struct.CancelOnDrop.html#method.leak) method.
109109
///
110110
/// Cancelling the [`DoneHandle`](struct.DoneHandle.html) does ***not*** cancel the `Promise`, because promises
111111
/// do not support cancellation.
@@ -123,7 +123,7 @@ impl Promise {
123123
///
124124
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then)
125125
// https://www.ecma-international.org/ecma-262/6.0/#sec-performpromisethen
126-
pub fn done< A, B, F >( &self, callback: F ) -> AutoCancel< DoneHandle >
126+
pub fn done< A, B, F >( &self, callback: F ) -> CancelOnDrop< DoneHandle >
127127
where A: TryFrom< Value >,
128128
B: TryFrom< Value >,
129129
// TODO these Debug constraints are only needed because of unwrap
@@ -166,7 +166,7 @@ impl Promise {
166166
return done;
167167
);
168168

169-
AutoCancel::new( DoneHandle {
169+
CancelOnDrop::new( DoneHandle {
170170
callback,
171171
done,
172172
} )

src/webcore/promise_future.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use webapi::error;
55
use futures::{Future, Poll, Async};
66
use futures::unsync::oneshot::Receiver;
77
use webcore::promise_executor::spawn;
8-
use webcore::cancel::AutoCancel;
8+
use webcore::cancel::CancelOnDrop;
99
use super::promise::{Promise, DoneHandle};
1010

1111

@@ -22,7 +22,7 @@ use super::promise::{Promise, DoneHandle};
2222
/// ```
2323
pub struct PromiseFuture< Value, Error = error::Error > {
2424
pub(crate) future: Receiver< Result< Value, Error > >,
25-
pub(crate) _done_handle: AutoCancel< DoneHandle >,
25+
pub(crate) _done_handle: CancelOnDrop< DoneHandle >,
2626
}
2727

2828
impl PromiseFuture< (), () > {

0 commit comments

Comments
 (0)