implement and use From<bool> for enum and back

implement and use  From<bool> for EventBubbles (and back direction)
implement and use From<bool> for EventCancelable (and back direction)
This commit is contained in:
Alexander Popiak 2016-03-17 22:50:22 +01:00
parent 7a9dc57761
commit 0a5ac3b207
10 changed files with 68 additions and 78 deletions

View file

@ -46,8 +46,8 @@ impl CloseEvent {
{ {
let event = ev.upcast::<Event>(); let event = ev.upcast::<Event>();
event.init_event(type_, event.init_event(type_,
bubbles == EventBubbles::Bubbles, bool::from(bubbles),
cancelable == EventCancelable::Cancelable); bool::from(cancelable));
} }
ev ev
} }
@ -56,16 +56,8 @@ impl CloseEvent {
type_: DOMString, type_: DOMString,
init: &CloseEventBinding::CloseEventInit) init: &CloseEventBinding::CloseEventInit)
-> Fallible<Root<CloseEvent>> { -> Fallible<Root<CloseEvent>> {
let bubbles = if init.parent.bubbles { let bubbles = EventBubbles::from(init.parent.bubbles);
EventBubbles::Bubbles let cancelable = EventCancelable::from(init.parent.cancelable);
} else {
EventBubbles::DoesNotBubble
};
let cancelable = if init.parent.cancelable {
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
Ok(CloseEvent::new(global, Ok(CloseEvent::new(global,
Atom::from(type_), Atom::from(type_),
bubbles, bubbles,

View file

@ -60,8 +60,8 @@ impl ErrorEvent {
let ev = ErrorEvent::new_uninitialized(global); let ev = ErrorEvent::new_uninitialized(global);
{ {
let event = ev.upcast::<Event>(); let event = ev.upcast::<Event>();
event.init_event(type_, bubbles == EventBubbles::Bubbles, event.init_event(type_, bool::from(bubbles),
cancelable == EventCancelable::Cancelable); bool::from(cancelable));
*ev.message.borrow_mut() = message; *ev.message.borrow_mut() = message;
*ev.filename.borrow_mut() = filename; *ev.filename.borrow_mut() = filename;
ev.lineno.set(lineno); ev.lineno.set(lineno);
@ -88,13 +88,9 @@ impl ErrorEvent {
let col_num = init.colno.unwrap_or(0); let col_num = init.colno.unwrap_or(0);
let bubbles = if init.parent.bubbles { EventBubbles::Bubbles } else { EventBubbles::DoesNotBubble }; let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = if init.parent.cancelable { let cancelable = EventCancelable::from(init.parent.cancelable);
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
// Dictionaries need to be rooted // Dictionaries need to be rooted
// https://github.com/servo/servo/issues/6381 // https://github.com/servo/servo/issues/6381

View file

@ -32,12 +32,48 @@ pub enum EventBubbles {
DoesNotBubble DoesNotBubble
} }
impl From<EventBubbles> for bool {
fn from(bubbles: EventBubbles) -> Self {
match bubbles {
EventBubbles::Bubbles => true,
EventBubbles::DoesNotBubble => false
}
}
}
impl From<bool> for EventBubbles {
fn from(boolean: bool) -> Self {
match boolean {
true => EventBubbles::Bubbles,
false => EventBubbles::DoesNotBubble
}
}
}
#[derive(PartialEq, HeapSizeOf)] #[derive(PartialEq, HeapSizeOf)]
pub enum EventCancelable { pub enum EventCancelable {
Cancelable, Cancelable,
NotCancelable NotCancelable
} }
impl From<EventCancelable> for bool {
fn from(bubbles: EventCancelable) -> Self {
match bubbles {
EventCancelable::Cancelable => true,
EventCancelable::NotCancelable => false
}
}
}
impl From<bool> for EventCancelable {
fn from(boolean: bool) -> Self {
match boolean {
true => EventCancelable::Cancelable,
false => EventCancelable::NotCancelable
}
}
}
#[dom_struct] #[dom_struct]
pub struct Event { pub struct Event {
reflector_: Reflector, reflector_: Reflector,
@ -87,15 +123,15 @@ impl Event {
bubbles: EventBubbles, bubbles: EventBubbles,
cancelable: EventCancelable) -> Root<Event> { cancelable: EventCancelable) -> Root<Event> {
let event = Event::new_uninitialized(global); let event = Event::new_uninitialized(global);
event.init_event(type_, bubbles == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable); event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
event event
} }
pub fn Constructor(global: GlobalRef, pub fn Constructor(global: GlobalRef,
type_: DOMString, type_: DOMString,
init: &EventBinding::EventInit) -> Fallible<Root<Event>> { init: &EventBinding::EventInit) -> Fallible<Root<Event>> {
let bubbles = if init.bubbles { EventBubbles::Bubbles } else { EventBubbles::DoesNotBubble }; let bubbles = EventBubbles::from(init.bubbles);
let cancelable = if init.cancelable { EventCancelable::Cancelable } else { EventCancelable::NotCancelable }; let cancelable = EventCancelable::from(init.cancelable);
Ok(Event::new(global, Atom::from(type_), bubbles, cancelable)) Ok(Event::new(global, Atom::from(type_), bubbles, cancelable))
} }

View file

@ -41,8 +41,8 @@ impl FocusEvent {
let event = box FocusEvent::new_inherited(); let event = box FocusEvent::new_inherited();
let ev = reflect_dom_object(event, GlobalRef::Window(window), FocusEventBinding::Wrap); let ev = reflect_dom_object(event, GlobalRef::Window(window), FocusEventBinding::Wrap);
ev.upcast::<UIEvent>().InitUIEvent(type_, ev.upcast::<UIEvent>().InitUIEvent(type_,
can_bubble == EventBubbles::Bubbles, bool::from(can_bubble),
cancelable == EventCancelable::Cancelable, bool::from(cancelable),
view, detail); view, detail);
ev.related_target.set(related_target); ev.related_target.set(related_target);
ev ev
@ -51,16 +51,8 @@ impl FocusEvent {
pub fn Constructor(global: GlobalRef, pub fn Constructor(global: GlobalRef,
type_: DOMString, type_: DOMString,
init: &FocusEventBinding::FocusEventInit) -> Fallible<Root<FocusEvent>> { init: &FocusEventBinding::FocusEventInit) -> Fallible<Root<FocusEvent>> {
let bubbles = if init.parent.parent.bubbles { let bubbles = EventBubbles::from(init.parent.parent.bubbles);
EventBubbles::Bubbles let cancelable = EventCancelable::from(init.parent.parent.cancelable);
} else {
EventBubbles::DoesNotBubble
};
let cancelable = if init.parent.parent.cancelable {
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
let event = FocusEvent::new(global.as_window(), type_, let event = FocusEvent::new(global.as_window(), type_,
bubbles, bubbles,
cancelable, cancelable,

View file

@ -74,7 +74,7 @@ impl MouseEvent {
button: i16, button: i16,
relatedTarget: Option<&EventTarget>) -> Root<MouseEvent> { relatedTarget: Option<&EventTarget>) -> Root<MouseEvent> {
let ev = MouseEvent::new_uninitialized(window); let ev = MouseEvent::new_uninitialized(window);
ev.InitMouseEvent(type_, canBubble == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable, ev.InitMouseEvent(type_, bool::from(canBubble), bool::from(cancelable),
view, detail, view, detail,
screenX, screenY, clientX, clientY, screenX, screenY, clientX, clientY,
ctrlKey, altKey, shiftKey, metaKey, ctrlKey, altKey, shiftKey, metaKey,
@ -85,16 +85,8 @@ impl MouseEvent {
pub fn Constructor(global: GlobalRef, pub fn Constructor(global: GlobalRef,
type_: DOMString, type_: DOMString,
init: &MouseEventBinding::MouseEventInit) -> Fallible<Root<MouseEvent>> { init: &MouseEventBinding::MouseEventInit) -> Fallible<Root<MouseEvent>> {
let bubbles = if init.parent.parent.parent.bubbles { let bubbles = EventBubbles::from(init.parent.parent.parent.bubbles);
EventBubbles::Bubbles let cancelable = EventCancelable::from(init.parent.parent.parent.cancelable);
} else {
EventBubbles::DoesNotBubble
};
let cancelable = if init.parent.parent.parent.cancelable {
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
let event = MouseEvent::new(global.as_window(), type_, let event = MouseEvent::new(global.as_window(), type_,
bubbles, bubbles,
cancelable, cancelable,

View file

@ -39,7 +39,7 @@ impl ProgressEvent {
ProgressEventBinding::Wrap); ProgressEventBinding::Wrap);
{ {
let event = ev.upcast::<Event>(); let event = ev.upcast::<Event>();
event.init_event(type_, can_bubble == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable); event.init_event(type_, bool::from(can_bubble), bool::from(cancelable));
} }
ev ev
} }
@ -47,9 +47,8 @@ impl ProgressEvent {
type_: DOMString, type_: DOMString,
init: &ProgressEventBinding::ProgressEventInit) init: &ProgressEventBinding::ProgressEventInit)
-> Fallible<Root<ProgressEvent>> { -> Fallible<Root<ProgressEvent>> {
let bubbles = if init.parent.bubbles { EventBubbles::Bubbles } else { EventBubbles::DoesNotBubble }; let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = if init.parent.cancelable { EventCancelable::Cancelable } let cancelable = EventCancelable::from(init.parent.cancelable);
else { EventCancelable::NotCancelable };
let ev = ProgressEvent::new(global, Atom::from(type_), bubbles, cancelable, let ev = ProgressEvent::new(global, Atom::from(type_), bubbles, cancelable,
init.lengthComputable, init.loaded, init.total); init.lengthComputable, init.loaded, init.total);
Ok(ev) Ok(ev)

View file

@ -57,7 +57,7 @@ impl StorageEvent {
StorageEventBinding::Wrap); StorageEventBinding::Wrap);
{ {
let event = ev.upcast::<Event>(); let event = ev.upcast::<Event>();
event.init_event(type_, bubbles == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable); event.init_event(type_, bool::from(bubbles), bool::from(cancelable));
} }
ev ev
} }
@ -70,12 +70,8 @@ impl StorageEvent {
let newValue = init.newValue.clone(); let newValue = init.newValue.clone();
let url = init.url.clone(); let url = init.url.clone();
let storageArea = init.storageArea.r(); let storageArea = init.storageArea.r();
let bubbles = if init.parent.bubbles { EventBubbles::Bubbles } else { EventBubbles::DoesNotBubble }; let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = if init.parent.cancelable { let cancelable = EventCancelable::from(init.parent.cancelable);
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
let event = StorageEvent::new(global, Atom::from(type_), let event = StorageEvent::new(global, Atom::from(type_),
bubbles, cancelable, bubbles, cancelable,
key, oldValue, newValue, key, oldValue, newValue,

View file

@ -68,8 +68,8 @@ impl TouchEvent {
metaKey: bool) -> Root<TouchEvent> { metaKey: bool) -> Root<TouchEvent> {
let ev = TouchEvent::new_uninitialized(window, touches, changed_touches, target_touches); let ev = TouchEvent::new_uninitialized(window, touches, changed_touches, target_touches);
ev.upcast::<UIEvent>().InitUIEvent(type_, ev.upcast::<UIEvent>().InitUIEvent(type_,
canBubble == EventBubbles::Bubbles, bool::from(canBubble),
cancelable == EventCancelable::Cancelable, bool::from(cancelable),
view, detail); view, detail);
ev.ctrl_key.set(ctrlKey); ev.ctrl_key.set(ctrlKey);
ev.alt_key.set(altKey); ev.alt_key.set(altKey);

View file

@ -48,20 +48,15 @@ impl UIEvent {
view: Option<&Window>, view: Option<&Window>,
detail: i32) -> Root<UIEvent> { detail: i32) -> Root<UIEvent> {
let ev = UIEvent::new_uninitialized(window); let ev = UIEvent::new_uninitialized(window);
ev.InitUIEvent(type_, can_bubble == EventBubbles::Bubbles, ev.InitUIEvent(type_, bool::from(can_bubble), bool::from(cancelable), view, detail);
cancelable == EventCancelable::Cancelable, view, detail);
ev ev
} }
pub fn Constructor(global: GlobalRef, pub fn Constructor(global: GlobalRef,
type_: DOMString, type_: DOMString,
init: &UIEventBinding::UIEventInit) -> Fallible<Root<UIEvent>> { init: &UIEventBinding::UIEventInit) -> Fallible<Root<UIEvent>> {
let bubbles = if init.parent.bubbles { EventBubbles::Bubbles } else { EventBubbles::DoesNotBubble }; let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = if init.parent.cancelable { let cancelable = EventCancelable::from(init.parent.cancelable);
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
let event = UIEvent::new(global.as_window(), type_, let event = UIEvent::new(global.as_window(), type_,
bubbles, cancelable, bubbles, cancelable,
init.view.r(), init.detail); init.view.r(), init.detail);

View file

@ -53,7 +53,7 @@ impl WebGLContextEvent {
{ {
let parent = event.upcast::<Event>(); let parent = event.upcast::<Event>();
parent.init_event(type_, bubbles == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable); parent.init_event(type_, bool::from(bubbles), bool::from(cancelable));
} }
event event
@ -67,17 +67,9 @@ impl WebGLContextEvent {
None => DOMString::new(), None => DOMString::new(),
}; };
let bubbles = if init.parent.bubbles { let bubbles = EventBubbles::from(init.parent.bubbles);
EventBubbles::Bubbles
} else {
EventBubbles::DoesNotBubble
};
let cancelable = if init.parent.cancelable { let cancelable = EventCancelable::from(init.parent.cancelable);
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
Ok(WebGLContextEvent::new(global, Atom::from(type_), Ok(WebGLContextEvent::new(global, Atom::from(type_),
bubbles, bubbles,