`datum` does not live long enough, but works when passing the trait

Playground Link

When I send the Datum (trait) over the channel, it works, but if I send the reference over the channel of the field stored in Datum, i.e., &self.value, it does not work.

How can I solve the error in the function send_over_channel_vec?

Is this because I am passing the ownership when passing the trait? But at some point, I will have to extract the value and pass; in that case, what is the right way to do it?

You could extract the value from the type implementing Datum by changing your method values to take self by value and not by reference. Playground.

1 Like

Thank you @jofas.

What if I have other methods in Datum trait? Eg,

impl Datum for MyData {
    fn values(&self) -> &Vec<u8> {
        &self.value
    }
    fn keys(&self) -> &Vec<u8> {
        &self.keys
    }
}

If I consume Self, then I won't be able to call other methods, right?

Yes, once you've consumed MyData because you've extracted the underlying vector, you won't be able to use it again. Another option, if you need to keep your instance of MyData around, would be to clone the data from MyData and send the clone.

Thank you. I was trying to avoid clone() because that data could be a lot. Datum is coming from tonic, and I have written a trait to abstract the internals.

Is there anything I can do differently (some refactoring) to pass the reference without cloning the Vec<u8>?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.