ecal 0.2.0

Bindings to the eCAL library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
use anyhow::Result;
use std::{
    env, ffi,
    marker::PhantomData,
    os::raw::{c_char, c_int, c_long, c_longlong, c_void},
    ptr, slice,
    time::{Duration, Instant},
};
use thiserror::Error;

#[cfg(feature = "derive")]
pub use ecal_derive::Message;

pub mod sys;

pub trait Message {
    fn type_name() -> &'static str;
}

// TODO: ... yeah
#[derive(Debug, Error)]
pub enum CalError {
    #[error("eCAL was not initialized.")]
    InitializationFailed,
    #[error("Unable to create new publisher for `{0}`")]
    PublisherCreationFailed(String),
    #[error("Unable to create new subscriber for `{0}`")]
    SubscriberCreationFailed(String),
    #[error("The message was not sent or was only partially sent.")]
    PublishFailed,
    #[error("Unexpected message format")]
    InvalidFormat,
    #[error("Time-out waiting to receive message.")]
    Timeout,
    #[error(transparent)]
    Unknown(#[from] anyhow::Error),
}

pub mod format {
    use anyhow::Result;

    pub trait Format {
        fn topic_type() -> String;
        fn topic_description() -> Option<String>;
    }

    pub trait Serializer<T> {
        fn serialize(message: &T, buffer: &mut Vec<u8>) -> Result<()>;
    }

    pub trait Deserializer<'a, T> {
        fn deserialize(buffer: &'a [u8]) -> Result<T>;
    }

    #[cfg(feature = "use_msgpack")]
    pub mod msgpack {
        use super::{Deserializer, Format, Serializer};
        use anyhow::{Error, Result};
        use serde::{Deserialize, Serialize};
        use std::marker::PhantomData;

        pub struct MessagePack<T: crate::Message> {
            _ty: PhantomData<T>,
        }

        impl<T> Format for MessagePack<T>
        where
            T: crate::Message,
        {
            fn topic_type() -> String {
                format!("mpack:{}", T::type_name())
            }

            /// unsupported by msgpack serialization
            fn topic_description() -> Option<String> {
                None
            }
        }

        impl<T> Serializer<T> for MessagePack<T>
        where
            T: Serialize + crate::Message,
        {
            fn serialize(message: &T, buf: &mut Vec<u8>) -> Result<()> {
                rmp_serde::encode::write(buf, message).map_err(Error::from)
            }
        }

        impl<'a, T> Deserializer<'a, T> for MessagePack<T>
        where
            T: Deserialize<'a> + crate::Message,
        {
            fn deserialize(buffer: &'a [u8]) -> Result<T> {
                rmp_serde::from_slice(buffer).map_err(Error::from)
            }
        }
    }

    #[cfg(feature = "use_prost")]
    pub mod prost {
        use super::{Deserializer, Format, Serializer};
        pub use ::prost::Message as ProstMessage;
        use anyhow::{Error, Result};
        use std::marker::PhantomData;

        pub struct Prost<T: crate::Message + ::prost::Message> {
            _ty: PhantomData<T>,
        }

        impl<T> Format for Prost<T>
        where
            T: crate::Message + ::prost::Message,
        {
            fn topic_type() -> String {
                format!("proto:{}", T::type_name())
            }

            /// unsupported by prost.
            fn topic_description() -> Option<String> {
                None
            }
        }

        impl<T> Serializer<T> for Prost<T>
        where
            T: crate::Message + ::prost::Message,
        {
            fn serialize(message: &T, buf: &mut Vec<u8>) -> Result<()> {
                message.encode(buf).map_err(Error::from)
            }
        }

        impl<'a, T> Deserializer<'a, T> for Prost<T>
        where
            T: crate::Message + ::prost::Message + Default,
        {
            fn deserialize(buffer: &'a [u8]) -> Result<T> {
                T::decode(buffer).map_err(Error::from)
            }
        }
    }

    #[cfg(feature = "use_protobuf")]
    pub mod protobuf {
        use super::{Deserializer, Format, Serializer};
        use anyhow::{Error, Result};
        use std::marker::PhantomData;

        pub struct Protobuf<T: ::protobuf::Message> {
            _ty: PhantomData<T>,
        }

        impl<T> Format for Protobuf<T>
        where
            T: crate::Message + ::protobuf::Message,
        {
            fn topic_type() -> String {
                format!("proto:{}", T::type_name())
            }

            fn topic_description() -> Option<String> {
                log::warn!("Topic descriptions do not yet work.");
                let descriptor = T::descriptor_static();
                let _pset = ::protobuf::descriptor::FileDescriptorSet::default();
                let description = ::protobuf::text_format::print_to_string(descriptor.get_proto());
                Some(description)
            }
        }

        impl<T> Serializer<T> for Protobuf<T>
        where
            T: crate::Message + ::protobuf::Message,
        {
            fn serialize(message: &T, buf: &mut Vec<u8>) -> Result<()> {
                message.write_to_vec(buf).map_err(Error::from)
            }
        }

        impl<'a, T> Deserializer<'a, T> for Protobuf<T>
        where
            T: ::protobuf::Message + Default,
        {
            fn deserialize(buffer: &'a [u8]) -> Result<T> {
                T::parse_from_bytes(buffer).map_err(Error::from)
            }
        }
    }

    #[cfg(feature = "use_capnp")]
    pub mod capnp {
        use anyhow::Result;
        use std::marker::PhantomData;

        use super::{Deserializer, Format, Serializer};
        use capnp::{
            message::{ReaderOptions, TypedBuilder, TypedReader},
            serialize::{read_message_from_flat_slice, write_message_to_words, SliceSegments},
            traits::Owned,
        };

        pub struct Capnp<T>
        where
            T: crate::Message + Owned,
        {
            _t: PhantomData<T>,
        }

        impl<T> Format for Capnp<T>
        where
            T: crate::Message + Owned,
        {
            fn topic_type() -> String {
                format!("capnp:{}", T::type_name())
            }

            fn topic_description() -> Option<String> {
                None
            }
        }

        impl<T> Serializer<TypedBuilder<T>> for Capnp<T>
        where
            T: crate::Message + Owned,
        {
            fn serialize(message: &TypedBuilder<T>, buffer: &mut Vec<u8>) -> Result<()> {
                buffer.append(&mut write_message_to_words(message.borrow_inner()));
                Ok(())
            }
        }

        impl<'a, T> Deserializer<'a, TypedReader<SliceSegments<'a>, T>> for Capnp<T>
        where
            T: crate::Message + Owned,
        {
            fn deserialize(mut buffer: &'a [u8]) -> Result<TypedReader<SliceSegments<'a>, T>> {
                Ok(read_message_from_flat_slice(&mut buffer, ReaderOptions::default())?.into())
            }
        }
    }
}

#[cfg(feature = "use_msgpack")]
pub mod msgpack {
    use super::format::msgpack::MessagePack;
    pub type Publisher<T> = super::Publisher<T, MessagePack<T>>;
    pub type Subscriber<T> = super::Subscriber<T, MessagePack<T>>;
}

#[cfg(feature = "use_prost")]
pub mod prost {
    use super::format::prost::Prost;
    pub type Publisher<T> = super::Publisher<T, Prost<T>>;
    pub type Subscriber<T> = super::Subscriber<T, Prost<T>>;
}

#[cfg(feature = "use_protobuf")]
pub mod protobuf {
    use super::format::protobuf::Protobuf;
    pub type Publisher<T> = super::Publisher<T, Protobuf<T>>;
    pub type Subscriber<T> = super::Subscriber<T, Protobuf<T>>;
}

#[cfg(feature = "use_capnp")]
pub mod capnp {
    use capnp::{
        message::{TypedBuilder, TypedReader},
        serialize::SliceSegments,
    };

    use super::format::capnp::Capnp;
    pub type Publisher<T> = super::Publisher<TypedBuilder<T>, Capnp<T>>;
    pub type Subscriber<'a, T> = super::Subscriber<TypedReader<SliceSegments<'a>, T>, Capnp<T>>;
}

pub struct Publisher<T, S> {
    handle: sys::ECAL_HANDLE,
    _ty: PhantomData<T>,
    _serializer: PhantomData<S>,
}

impl<T, S> Publisher<T, S>
where
    S: format::Format + format::Serializer<T>,
{
    pub fn new(topic_name: &str) -> Result<Self> {
        let handle = unsafe { sys::eCAL_Pub_New() };
        let c_topic_name = ffi::CString::new(topic_name)?;
        let c_topic_type = ffi::CString::new(S::topic_type())?;
        let description = S::topic_description();
        let c_description = match description {
            Some(description) => ffi::CString::new(description)?,
            None => ffi::CString::default(),
        };
        let status = unsafe {
            sys::eCAL_Pub_Create(
                handle,
                c_topic_name.as_ptr(),
                c_topic_type.as_ptr(),
                c_description.as_ptr() as *const std::os::raw::c_char,
                c_description.as_bytes().len() as i32,
            )
        };
        if status == 0 {
            unsafe {
                sys::eCAL_Pub_Destroy(handle);
            }
            Err(CalError::PublisherCreationFailed(topic_name.to_string()).into())
        } else {
            Ok(Publisher {
                handle,
                _serializer: Default::default(),
                _ty: Default::default(),
            })
        }
    }

    pub fn set_id(&mut self, id: i64) -> bool {
        unsafe { sys::eCAL_Pub_SetID(self.handle, id as c_longlong) != 0 }
    }

    pub fn shm_set_buffer_count(&mut self, buffer_num: usize) -> bool {
        unsafe { sys::eCAL_Pub_ShmSetBufferCount(self.handle, buffer_num as c_long) != 0 }
    }

    pub fn is_subscribed(&self) -> bool {
        unsafe { sys::eCAL_Pub_IsSubscribed(self.handle) != 0 }
    }

    pub fn send(&self, msg: &T) -> Result<()> {
        self.send_with_time(msg, -1)
    }

    /// Same as [send](#method.send) but let the caller set the time of the message
    pub fn send_with_time(&self, msg: &T, time: i64) -> Result<()> {
        let mut buf = Vec::with_capacity(32);
        S::serialize(msg, &mut buf)?;

        let bytes_expected = buf.len();
        let bytes_sent = unsafe {
            sys::eCAL_Pub_Send(
                self.handle,
                buf.as_ptr() as *const c_void,
                bytes_expected as c_int,
                time as c_longlong,
            )
        };
        log::trace!("Published {} / {} bytes", bytes_sent, bytes_expected);
        if bytes_sent != bytes_expected as c_int {
            Err(CalError::PublishFailed.into())
        } else {
            Ok(())
        }
    }

    unsafe extern "C" fn event_wrapper<F>(
        _topic_name: *const c_char,
        _data: *const sys::SPubEventCallbackDataC,
        ctx: *mut c_void,
    ) where
        F: FnMut(),
    {
        // TODO: Support other callback types. :D
        let cb_ptr = ctx as *mut F;
        let callback = &mut *cb_ptr;
        callback();
    }

    pub fn on_subscribed<F>(&self, callback: F)
    where
        F: FnMut(),
    {
        // TODO: memory leak?
        let callback = Box::into_raw(Box::new(callback));
        unsafe {
            sys::eCAL_Pub_AddEventCallbackC(
                self.handle,
                sys::eCAL_Publisher_Event::pub_event_connected,
                Some(Self::event_wrapper::<F>),
                callback as *mut _,
            );
        }
    }
}

impl<T, S> Drop for Publisher<T, S> {
    fn drop(&mut self) {
        unsafe {
            sys::eCAL_Pub_Destroy(self.handle);
        }
    }
}

pub type RecvFn<T> = dyn Fn(Instant, T);

pub struct Subscriber<T, D> {
    handle: sys::ECAL_HANDLE,
    _ty: PhantomData<T>,
    _deserializer: PhantomData<D>,
}

impl<'a, T, D> Subscriber<T, D>
where
    D: format::Format + format::Deserializer<'a, T>,
{
    pub fn new(topic_name: &str) -> Result<Self> {
        let handle = unsafe { sys::eCAL_Sub_New() };
        let c_topic_name = ffi::CString::new(topic_name)?;
        let c_topic_type = ffi::CString::new(D::topic_type())?;
        let description = D::topic_description();
        let c_description = match description {
            Some(description) => ffi::CString::new(description)?,
            None => ffi::CString::default(),
        };
        let status = unsafe {
            sys::eCAL_Sub_Create(
                handle,
                c_topic_name.as_ptr(),
                c_topic_type.as_ptr(),
                c_description.as_ptr() as *const std::os::raw::c_char,
                c_description.as_bytes().len() as i32,
            )
        };
        if status == 0 {
            Err(CalError::SubscriberCreationFailed(topic_name.to_string()).into())
        } else {
            Ok(Subscriber {
                handle,
                _ty: Default::default(),
                _deserializer: Default::default(),
            })
        }
    }

    fn _recv(&self, timeout: c_int) -> Result<T> {
        let mut buf = ptr::null_mut::<c_void>();
        let buf_len = sys::ECAL_ALLOCATE_4ME as i32;
        let mut time = 0;

        let bytes_received =
            unsafe { sys::eCAL_Sub_Receive(self.handle, &mut buf, buf_len, &mut time, timeout) };

        if bytes_received > 0 {
            let bytes = unsafe { slice::from_raw_parts(buf as *const u8, bytes_received as usize) };

            let res = D::deserialize(bytes).map_err(|err| {
                log::error!("Failed to decode message: {}", err);
                CalError::InvalidFormat.into()
            });

            log::trace!("Freeing recv buffer");
            unsafe {
                sys::eCAL_FreeMem(buf);
            }

            res
        } else {
            log::trace!("Subscriber timeout");
            if !buf.is_null() {
                log::warn!("Non-null pointer returned from recv, but bytes_received was 0!");
            }
            Err(CalError::Timeout.into())
        }
    }

    pub fn recv(&self) -> Result<T> {
        log::trace!("Subscriber::recv");
        self._recv(-1).map_err(Into::into)
    }

    pub fn try_recv(&self, timeout: Duration) -> Option<T> {
        log::trace!("Subscriber::try_recv");
        let timeout = timeout.as_millis() as c_int;
        self._recv(timeout).ok()
    }

    unsafe extern "C" fn recv_wrapper<F>(
        _topic_name: *const c_char,
        data: *const sys::SReceiveCallbackDataC,
        ctx: *mut c_void,
    ) where
        F: FnMut(Instant, T),
    {
        let bytes = slice::from_raw_parts((*data).buf as *const u8, (*data).size as usize);

        if let Ok(msg) = D::deserialize(bytes) {
            log::trace!("Received {} bytes", bytes.len());
            let cb_ptr = ctx as *mut F;
            let callback = &mut *cb_ptr;
            // TODO: use eCAL timestamp
            let timestamp = Instant::now();
            callback(timestamp, msg);
        } else {
            log::error!("Failed to decode message.");
        }
    }

    pub fn on_recv<'b, F: FnMut(Instant, T) + 'b>(&'b self, callback: F) {
        // TODO: memory leak?
        let callback = Box::into_raw(Box::new(callback));
        unsafe {
            sys::eCAL_Sub_AddReceiveCallbackC(
                self.handle,
                Some(Self::recv_wrapper::<F>),
                callback as *mut _,
            );
        }
    }

    unsafe extern "C" fn recv_wrapper_full<F>(
        _topic_name: *const c_char,
        data: *const sys::SReceiveCallbackDataC,
        ctx: *mut c_void,
    ) where
        F: FnMut(sys::SReceiveCallbackDataC, T),
    {
        let bytes = slice::from_raw_parts((*data).buf as *const u8, (*data).size as usize);

        if let Ok(msg) = D::deserialize(bytes) {
            log::trace!("Received {} bytes", bytes.len());
            let cb_ptr = ctx as *mut F;
            let callback = &mut *cb_ptr;
            callback(*data, msg);
        } else {
            log::error!("Failed to decode message.");
        }
    }

    /// Same as [`on_recv`](#method.on_recv), but instead of pass the Instant of the message this will pass
    /// the entire content that arrives from the receive callback ([SReceiveCallbackDataC](sys::SReceiveCallbackDataC))
    pub fn on_recv_full<'b, F: FnMut(sys::SReceiveCallbackDataC, T) + 'b>(&'b self, callback: F) {
        // TODO: memory leak?
        let callback = Box::into_raw(Box::new(callback));
        unsafe {
            sys::eCAL_Sub_AddReceiveCallbackC(
                self.handle,
                Some(Self::recv_wrapper_full::<F>),
                callback as *mut _,
            );
        }
    }
}

impl<T, D> Drop for Subscriber<T, D> {
    fn drop(&mut self) {
        unsafe {
            sys::eCAL_Sub_Destroy(self.handle);
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum NodeState {
    Healthy,
    Critical,
    Failed,
    Unknown,
    Warning,
}

impl From<NodeState> for sys::eCAL_Process_eSeverity {
    fn from(state: NodeState) -> Self {
        use sys::eCAL_Process_eSeverity::*;
        use NodeState::*;
        match state {
            Healthy => proc_sev_healthy,
            Critical => proc_sev_critical,
            Failed => proc_sev_failed,
            Unknown => proc_sev_unknown,
            Warning => proc_sev_warning,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum SeverityLevel {
    Level1,
    Level2,
    Level3,
    Level4,
    Level5,
}

impl From<SeverityLevel> for sys::eCAL_Process_eSeverity_Level {
    fn from(level: SeverityLevel) -> Self {
        use sys::eCAL_Process_eSeverity_Level::*;
        use SeverityLevel::*;
        match level {
            Level1 => proc_sev_level1,
            Level2 => proc_sev_level2,
            Level3 => proc_sev_level3,
            Level4 => proc_sev_level4,
            Level5 => proc_sev_level5,
        }
    }
}

#[derive(Debug, Default)]
pub struct Cal {
    status_msg: ffi::CString,
}

impl Cal {
    pub fn new(unit_name: &str) -> Result<Self> {
        initialize(unit_name).and_then(|_| {
            let mut cal = Cal::default();
            cal.set_state(NodeState::Healthy, SeverityLevel::Level1, "ok")?;
            Ok(cal)
        })
    }

    /// Sets the process state and severity level.
    /// Can fail if the status message is unable to be
    /// converted to a CString.
    pub fn set_state(&mut self, state: NodeState, level: SeverityLevel, info: &str) -> Result<()> {
        self.status_msg = ffi::CString::new(info)?;
        unsafe { sys::eCAL_Process_SetState(state.into(), level.into(), self.status_msg.as_ptr()) };
        Ok(())
    }
}

impl Drop for Cal {
    fn drop(&mut self) {
        finalize();
    }
}

fn initialize(unit_name: &str) -> Result<()> {
    let mut args = env::args()
        .map(|arg| ffi::CString::new(arg).expect("Failed to build CString from arg"))
        .collect::<Vec<ffi::CString>>();
    let mut argv = args
        .iter_mut()
        .map(|a| a.as_ptr() as *mut c_char)
        .collect::<Vec<*mut c_char>>();
    let argc = argv.len() as c_int;

    let c_unit_name = ffi::CString::new(unit_name).expect("Failed to build CString from unit_name");

    let status = unsafe {
        sys::eCAL_Initialize(
            argc,
            argv.as_mut_ptr(),
            c_unit_name.as_ptr(),
            sys::eCAL_Init_Default,
        )
    };

    match status {
        -1 => {
            log::error!("Failed to initialize eCAL");
            return Err(CalError::InitializationFailed.into());
        }
        0 => log::info!("eCAL initiailized as '{}'.", unit_name),
        1 => log::warn!("eCAL was already initialized."),
        _ => log::warn!(
            "Unexpected status returned from eCAL_Initialize: {}",
            status
        ),
    }

    Ok(())
}

fn finalize() {
    unsafe {
        log::debug!("Finalizing eCAL system.");
        let _ = sys::eCAL_Finalize(sys::eCAL_Init_All);
    }
}

pub fn ok() -> bool {
    let status = unsafe { sys::eCAL_Ok() };
    log::trace!("eCAL_Ok == {}", status);
    status != 0
}

pub fn sleep(duration: Duration) {
    let ms = duration.as_millis();
    unsafe {
        sys::eCAL_Process_SleepMS(ms as c_long);
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn ecal_init_and_finalize() {
        let _ = ::env_logger::try_init();
        assert!(super::initialize("kcal_init_test").is_ok());
        super::sleep(std::time::Duration::from_millis(10));
        super::finalize();
    }

    #[test]
    fn ecal_init_and_finalize_raii() {
        let _ = ::env_logger::try_init();
        {
            let cal = super::Cal::new("kcal_tests");
            assert!(cal.is_ok());
            let _cal = cal.unwrap();
            assert!(super::ok());
        }
    }

    #[test]
    fn ecal_set_state() {
        let _ = ::env_logger::try_init();
        {
            let cal = super::Cal::new("kcal_tests");
            assert!(cal.is_ok());
            let mut cal = cal.unwrap();
            cal.set_state(
                super::NodeState::Healthy,
                super::SeverityLevel::Level1,
                "All good in the hood!",
            )
            .expect("Unable to set eCAL node state.");
            super::sleep(std::time::Duration::from_millis(20));
            assert!(super::ok());
        }
    }
}

unsafe impl<T, S> Send for Publisher<T, S> {}
unsafe impl<T, S> Sync for Publisher<T, S> {}