servo: Merge #10066 - implement and use From<bool> for enum and back (from apopiak:fromBool); r=asajeffrey

as discussed in the #servo channel on IRC:

implement and use  `From<bool>` for `EventBubbles` (and back direction)
implement and use `From<bool>` for `EventCancelable` (and back direction)

Source-Repo: https://github.com/servo/servo
Source-Revision: 4df7975ed3e74f1d6c491b0435bb3e751b5386a8
This commit is contained in:
Alexander Popiak 2016-03-19 22:29:21 +05:01
parent 2fb0f49f37
commit 9d55144633
10 changed files with 68 additions and 78 deletions

View File

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

View File

@ -60,8 +60,8 @@ impl ErrorEvent {
let ev = ErrorEvent::new_uninitialized(global);
{
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.message.borrow_mut() = message;
*ev.filename.borrow_mut() = filename;
ev.lineno.set(lineno);
@ -88,13 +88,9 @@ impl ErrorEvent {
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 {
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
let cancelable = EventCancelable::from(init.parent.cancelable);
// Dictionaries need to be rooted
// https://github.com/servo/servo/issues/6381

View File

@ -32,12 +32,48 @@ pub enum EventBubbles {
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)]
pub enum EventCancelable {
Cancelable,
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]
pub struct Event {
reflector_: Reflector,
@ -87,15 +123,15 @@ impl Event {
bubbles: EventBubbles,
cancelable: EventCancelable) -> Root<Event> {
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
}
pub fn Constructor(global: GlobalRef,
type_: DOMString,
init: &EventBinding::EventInit) -> Fallible<Root<Event>> {
let bubbles = if init.bubbles { EventBubbles::Bubbles } else { EventBubbles::DoesNotBubble };
let cancelable = if init.cancelable { EventCancelable::Cancelable } else { EventCancelable::NotCancelable };
let bubbles = EventBubbles::from(init.bubbles);
let cancelable = EventCancelable::from(init.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 ev = reflect_dom_object(event, GlobalRef::Window(window), FocusEventBinding::Wrap);
ev.upcast::<UIEvent>().InitUIEvent(type_,
can_bubble == EventBubbles::Bubbles,
cancelable == EventCancelable::Cancelable,
bool::from(can_bubble),
bool::from(cancelable),
view, detail);
ev.related_target.set(related_target);
ev
@ -51,16 +51,8 @@ impl FocusEvent {
pub fn Constructor(global: GlobalRef,
type_: DOMString,
init: &FocusEventBinding::FocusEventInit) -> Fallible<Root<FocusEvent>> {
let bubbles = if init.parent.parent.bubbles {
EventBubbles::Bubbles
} else {
EventBubbles::DoesNotBubble
};
let cancelable = if init.parent.parent.cancelable {
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
let bubbles = EventBubbles::from(init.parent.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.parent.cancelable);
let event = FocusEvent::new(global.as_window(), type_,
bubbles,
cancelable,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -53,7 +53,7 @@ impl WebGLContextEvent {
{
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
@ -67,17 +67,9 @@ impl WebGLContextEvent {
None => DOMString::new(),
};
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
} else {
EventCancelable::NotCancelable
};
let cancelable = EventCancelable::from(init.parent.cancelable);
Ok(WebGLContextEvent::new(global, Atom::from(type_),
bubbles,