hashmismatch / freertos.rs

A Rust wrapper for FreeRTOS.
232 stars 22 forks source link

Question about passing huge struct through queue #12

Closed lexxvir closed 6 years ago

lexxvir commented 6 years ago

Is it possible to pass huge struct through queue without copying whole struct?

It seems that freertos.rs makes copying of item in Queue::send() (though it possibly be removed by optimizations), then FreeRTOS' xQueueSend also does copy.

Box<T> could be used for this purpose but as I see, Box doesn't implement Copy trait that required by Queue.

rudib commented 6 years ago

No, queues cannot be used for heap allocated data structures, which is by FreeRTOS's design, and which is enforced using Rust's copy semantics. Try to redesign your application to pass only Copy structures. If you really need that, you might want to look into storing data into a global, shared vector (Arc<Mutex<Vec>>) and passing plain IDs using queues.

lexxvir commented 6 years ago

Sorry for annoying, I believe that in FreeRTOS it possible to allocation data in heap and then send pointer to allocated memory to queue. Then receiver will be responsible to free memory. Of course allocated memory must not used by sender when he sent pointer to queue.

In Rust we can use Box for this purpose, but, as I understood now, it is require separated implementation, that will be:

  1. consume Box by into_raw
  2. send raw pointer by queue
  3. restore Box at receiver side by Box::from_raw from raw pointer
rudib commented 6 years ago

Sure, you can do that. No changes are necessary within this Rust library, you can easily implement that as a trivial typed Rust struct that only holds a single usize pointer. But support for this won't be added to this project.