reactive-rs icon indicating copy to clipboard operation
reactive-rs copied to clipboard

lifetime conflict for a simple map

Open M-Adoo opened this issue 6 years ago • 3 comments

I try to map a trait object to downcast to another type, in the map operator. I don't know can how i do it in ractive-rs, just below simple demo will complain lifetime conflict. how can I return a reference in map? Should the observer callback accept a type is better than a reference ?

  #[test]
  fn test_stream() {
    let quotes = SimpleBroadcast::new();
    quotes
      .map(|e: &i32| e)
      .subscribe_ctx(|ctx, e| println!("recieve {}", e));

    quotes.send(1);
  }

M-Adoo avatar May 12 '19 18:05 M-Adoo

  1. In this case you have to dereference e, otherwise you're sending a reference (with a lifetime) down the pipeline.
  2. If you carefully read the example in the readme, it shows that you should use .clone() - because otherwise the broadcast would be moved and you won't be able to send to it.

So a correct example is (unless you want to do something else):

  #[test]
  fn test_stream() {
    let quotes = SimpleBroadcast::new();
    quotes
      .clone()
      .map(|e: &i32| *e)
      .subscribe_ctx(|ctx, e| println!("receive {}", e));

    quotes.send(1);
  }

aldanor avatar May 12 '19 21:05 aldanor

thx for your response. But when a type is not impl Copy, I can't direct dereference e.

M-Adoo avatar May 13 '19 03:05 M-Adoo

thx for your response. But when a type is not impl Copy, I can't direct dereference e.

But if the type implements Clone, you can clone it?

Otherwise, you will probably have to deal with RefCell and Rc, depending on your exact situation.

aldanor avatar May 13 '19 18:05 aldanor