How to submit command with image buffer as payload from another thread

166 Views Asked by At

I am trying to download image using another thread and to send downloaded bytes in ImageBuf object to main thread using Druid command system. The code I am using is:

let event_sink = ctx.get_external_handle();
    thread::spawn(move || {
        let content_bytes = download_bytes("https://media.istockphoto.com/photos/generic-drug-box-picture-id1054301628");
        let raw_content_bytes : &[u8] = content_bytes.as_ref();
        let image_buffer = ImageBuf::from_data(raw_content_bytes).expect("Failed to store bytes into image buffer");

        event_sink.submit_command(UPDATE_IMAGE_COMMAND, image_buffer, Target::from(image_widget_id));
    });

The last line throws an error message saying:

the trait bound `Box<()>: From<ImageBuf>` is not satisfied
the following implementations were found:
<Box<(dyn StdError + 'a)> as From<E>>
<Box<(dyn StdError + 'static)> as From<&str>>
<Box<(dyn StdError + 'static)> as From<Cow<'a, str>>>
<Box<(dyn StdError + 'static)> as From<anyhow::Error>>
and 25 others
required because of the requirements on the impl of `Into<Box<()>>` for `ImageBuf`

I am not sure what this really means, I have been looking through examples and tried wrapping image_buffer into box but none of it have helped so far.

Do note that in Druid examples there is one example(blocking_function) that sends raw number(u32) as payload, but when I try to put hardcoded number instead of image_buffer in my code, I still get the same error message.

1

There are 1 best solutions below

0
On BEST ANSWER

TL;DR

Add generic argument: pub(crate) const UPDATE_IMAGE_COMMAND: Selector<ImageBuf> = Selector::new("update_image");

Thanks to comment from @Caesar i applied .submit_command::<ImageBuf>(…) and figured out that druid Selector have generic type on them which determines what kind of payload can be sent with the finally generated Command. Default type for Selector is <T = ()> so the fix for my problem was just to add ImageBuf as generic argument for selector as shown in the code snippet above