capnproto-rust icon indicating copy to clipboard operation
capnproto-rust copied to clipboard

`!Send` impl for various reader types

Open weiznich opened this issue 3 years ago • 4 comments

Is there any reason why capnp::struct_list::Reader and similar types are not Send? That makes it quite hard to use them in a async context, where each value of those iterator is later used to spawn of a future task.

weiznich avatar Jan 17 '22 16:01 weiznich

I just stumbled on this. I don't understand why some things would have to be !Send when the TypedReader is okay to send. It seems the *_capnp::foo::Reader just contains raw pointers and ended up being !Send by default. Those should be fine to mark as Send.

See also https://github.com/capnproto/capnproto-rust/issues/180#issuecomment-938226071

// this is a TypedReader<BufferSegments<&[u8]>, ...> 
let typed_reader = ...;

// the TypedReader is fine to send across await
while let Some(blah) = my_stream.next().await {
    // but the *_capnp::foo::Reader isn't
    // and I have to do this on every iteration
    let foo = typed_reader.get().unwrap();
    ...
}

tv42 avatar Jan 02 '24 20:01 tv42

Okay so https://github.com/capnproto/capnproto-rust/issues/479#issuecomment-1913742538 points out Readers are not safe to Send because they borrow the underlying buffer. Hmm.

tv42 avatar Feb 01 '24 17:02 tv42

In my use case, my data source is (moral equivalent of) Arc<Box<u8>>. It'd be nice to have a Send or Send+Sync Reader when the data source happens to be Send/Sync.

tv42 avatar Feb 01 '24 17:02 tv42