www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - deep copy thread local to shared or how to send() struct with

Hi!

I need to send Publish struct from 
https://github.com/tchaloupka/vibe-mqtt to another vibe.d task. 
And there is the problem. First of all, I can't make it immutable 
because send() wants to mutate it. I can't send local copy 
because of "Aliases to mutable thread-local data not allowed.". 
And I can't convert it to shared because of "cannot implicitly 
convert expression `packet` of type `Publish` to 
`shared(FixedHeader)`".

I don't understand error message about FixedHeader. Why it tries 
to convert struct to its field? As I understand the real problem 
here is `ubyte[] payload` dynamic array field in Publish which is 
allocated separately, so to make a shared copy of Publish it need 
the deep copy of this struct. How to do it right? I don't want to 
copy all fields manually, is there a standard way to accomplish 
it?


```
struct MqttOnPublish
{
   Publish packet;
}

   override void onPublish(Publish packet)
   {
      super.onPublish(packet);

      // source/vcm/mqtt/bridge.d(69,41): Error: cannot implicitly 
convert expression `packet` of type `Publish` to 
`shared(FixedHeader)`
      shared Publish sp = shared Publish(packet);

/* /usr/include/dmd/phobos/std/concurrency.d(575,5): Error: 
static assert  "Aliases to mutable thread-local data not allowed."
../../.dub/packages/vibe-d-0.8.1/vibe-d/core/vibe/core/concurrency.d(1243,64): 
      instantiated from here: send!(MqttOnPublish)
source/vision/eventbus.d(72,27):        instantiated from here: 
send!(MqttOnPublish)
source/vision/eventbus.d(42,13):        instantiated from here: 
emit!(MqttOnPublish)
source/vcm/mqtt/bridge.d(70,14):        instantiated from here: 
emit!(MqttOnPublish)
*/
      bus.emit(MqttOnPublish(packet));

   }
```
Nov 23