-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to return a reader? #259
Comments
cannot return value referencing temporary value |
Okay, so my understanding is that you're trying to keep the original If that's correct, there are a few issues with your initial attempt:
Here's my first attempt at something like what you're asking (full repo), based on the use anyhow::Result;
use capnp::{message::ReaderOptions, serialize::SliceSegments};
use points_capnp::point;
pub struct ReaderWrapper<'a> {
reader: capnp::message::Reader<SliceSegments<'a>>,
}
impl<'a> TryFrom<&'a [u8]> for ReaderWrapper<'a> {
type Error = anyhow::Error;
fn try_from(mut buffer: &'a [u8]) -> Result<ReaderWrapper<'a>> {
let reader =
capnp::serialize::read_message_from_flat_slice(&mut buffer, ReaderOptions::new())?;
Ok(ReaderWrapper { reader })
}
}
impl<'a> ReaderWrapper<'a> {
fn print_point(&self) -> Result<()> {
let point_reader = self.reader.get_root::<point::Reader>()?;
println!("x = {:.2}", point_reader.get_x());
println!("y = {:.2}", point_reader.get_y());
Ok(())
}
} Note that your I thought this would be a good starting point - I haven't tried storing the "typed" version yet, although I think it might be possible with something like the owning_ref crate, although I'm not 100% sure. |
@OliverEvans96 I do realise this is an old issue but I am trying something similar and can't get it working either - I feel like I am missing something basic so any help is welcome This feels like basic usage to me but I am trying cache the underlying bytes as well as the specific I tried several combinations, the problem with simply storing the Caching both data structures on self is tricky (at least I wasn't able to) because It feels like I am missing something, but not sure what |
@alexbleotu does |
read_message_from_flat_slice returns a Reader<SliceSegments<'a>>, is there a way to completely avoid referencing the buffer? My use case is that I have a memory mapped buffer and I want to store it in a structure, together with the |
Here's a trick for making a capnp Reader hold on to e.g. a let bytes: Box<[u8]> = foo();
let capnp_reader_options = capnp::message::ReaderOptions::new();
let segments = capnp::serialize::BufferSegments::new(bytes, capnp_reader_options)?;
let reader = capnp::message::Reader::new(segments, capnp_reader_options)
.into_typed::<foo::Owned>(); Now, if you wanted to also hold on to your data, I think you can use |
I want to store a
Reader
inside of a struct but it only uses a reference a does not implement Clone.The text was updated successfully, but these errors were encountered: