Am I doing wrong with Send + Sync of struct with Box

so, I'm writing a reactive extensions library. It's workable, I just wonder if is there any way safer (more correctly) to do this

pub struct Subscription<'a> {
    unsubscribe: Box<dyn FnOnce() + 'a>

unsafe impl<'a> Send for Subscription<'a> {}
unsafe impl<'a> Sync for Subscription<'a> {}

It's easy to trigger undefined behavior with this code. Imagin that Box<dyn FnOnce()> is a closure that contains Rc and clone it on execution. You can send it to another thread in safe code due to your unsafe impl Send for Subscription, and calling it in there is UB. To fix it, try this impl.

pub struct Subscription<'a> {
  unsubscribe: Box<dyn FnOnce() + Send + Sync + 'a>,

thank you, think about this before, but it will be followed by many change effect by others (cause not only this struct has this kind of structure). I assume the FnOnce only contains 'static and be sent once.
but seem like I have to change it anw

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.