Add Send bound. Fixes #50

This commit is contained in:
Jonathan Kingston 2019-05-26 13:39:01 +01:00 committed by Bodil Stokke
parent 1588f30353
commit 62dd064f79
3 changed files with 16 additions and 16 deletions

View File

@ -6,7 +6,7 @@ use std::fmt::{Display, Error, Formatter};
use std::iter; use std::iter;
/// Trait for event handlers. /// Trait for event handlers.
pub trait EventHandler<T: OutputType, E> { pub trait EventHandler<T: OutputType + Send, E: Send> {
/// Build a callback function from this event handler. /// Build a callback function from this event handler.
/// ///
/// Returns `None` is this event handler can't be used to build a callback /// Returns `None` is this event handler can't be used to build a callback
@ -26,13 +26,13 @@ pub trait EventHandler<T: OutputType, E> {
macro_rules! declare_events_struct { macro_rules! declare_events_struct {
($($name:ident,)*) => { ($($name:ident,)*) => {
pub struct Events<T> { pub struct Events<T> where T: Send {
$( $(
pub $name: Option<T>, pub $name: Option<T>,
)* )*
} }
impl<T> Events<T> { impl<T: Send> Events<T> {
pub fn iter(&self) -> impl Iterator<Item = (&'static str, &T)> { pub fn iter(&self) -> impl Iterator<Item = (&'static str, &T)> {
iter::empty() iter::empty()
$( $(
@ -54,7 +54,7 @@ macro_rules! declare_events_struct {
} }
} }
impl<T: 'static> IntoIterator for Events<T> { impl<T: 'static + Send> IntoIterator for Events<T> {
type Item = (&'static str, T); type Item = (&'static str, T);
type IntoIter = Box<dyn Iterator<Item = Self::Item>>; type IntoIter = Box<dyn Iterator<Item = Self::Item>>;
@ -72,7 +72,7 @@ macro_rules! declare_events_struct {
} }
} }
impl<T> Default for Events<T> { impl<T: Send> Default for Events<T> {
fn default() -> Self { fn default() -> Self {
Events { Events {
$( $(
@ -82,7 +82,7 @@ macro_rules! declare_events_struct {
} }
} }
impl<T: Display> Display for Events<T> { impl<T: Display + Send> Display for Events<T> {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
$( $(
if let Some(ref value) = self.$name { if let Some(ref value) = self.$name {

View File

@ -214,11 +214,11 @@ pub mod types;
/// Marker trait for outputs /// Marker trait for outputs
pub trait OutputType { pub trait OutputType {
/// The type that contains events for this output. /// The type that contains events for this output.
type Events: Default + Display; type Events: Default + Display + Send;
/// The type of event targets for this output. /// The type of event targets for this output.
type EventTarget; type EventTarget: Send;
/// The type that's returned from attaching an event listener to a target. /// The type that's returned from attaching an event listener to a target.
type EventListenerHandle; type EventListenerHandle: Send;
} }
/// String output /// String output

View File

@ -21,7 +21,7 @@ macro_rules! declare_events {
/// Container type for DOM events. /// Container type for DOM events.
pub struct Events { pub struct Events {
$( $(
pub $name: Option<Box<dyn EventHandler<Stdweb, $type>>>, pub $name: Option<Box<dyn EventHandler<Stdweb, $type> + Send>>,
)* )*
} }
@ -129,17 +129,17 @@ pub struct EFn<F, E>(Option<F>, PhantomData<E>);
impl<F, E> EFn<F, E> impl<F, E> EFn<F, E>
where where
F: FnMut(E) + 'static, F: FnMut(E) + 'static + Send,
{ {
pub fn new(f: F) -> Self { pub fn new(f: F) -> Self {
EFn(Some(f), PhantomData) EFn(Some(f), PhantomData)
} }
} }
impl<F, E> From<F> for Box<dyn EventHandler<Stdweb, E>> impl<F, E> From<F> for Box<dyn EventHandler<Stdweb, E> + Send>
where where
F: FnMut(E) + 'static, F: FnMut(E) + 'static + Send,
E: ConcreteEvent + 'static, E: ConcreteEvent + 'static + Send,
{ {
fn from(f: F) -> Self { fn from(f: F) -> Self {
Box::new(EFn::new(f)) Box::new(EFn::new(f))
@ -148,8 +148,8 @@ where
impl<F, E> EventHandler<Stdweb, E> for EFn<F, E> impl<F, E> EventHandler<Stdweb, E> for EFn<F, E>
where where
F: FnMut(E) + 'static, F: FnMut(E) + 'static + Send,
E: ConcreteEvent + 'static, E: ConcreteEvent + 'static + Send,
{ {
fn attach(&mut self, target: &mut <Stdweb as OutputType>::EventTarget) -> EventListenerHandle { fn attach(&mut self, target: &mut <Stdweb as OutputType>::EventTarget) -> EventListenerHandle {
let handler = self.0.take().unwrap(); let handler = self.0.take().unwrap();