Repository: capnproto/capnproto-rust (language: rust)

Issue:
Serializing from borrowed TypedReader
Hello,

I happen to face a situation where in some API I have a borrowed TypedReader and I need to serialize it.

I might have missed something, but it seems to serialize from a reader, I need access to the ReaderSegments which I can only obtain by doing typed_reader.into_inner().into_segments() which is consuming the input TypedReader.
However the serializing functions (the _to_words ones) actually accept a borrowed ReaderSegments.

Is there a reason for not exposing a borrowed ReaderSegments?
If we added a method like `get_reader_segments(&self) -> &S` it should fix my issue.

Thank you for your help.


Discussion / hints:
Adding a `message::Reader::get_segments()` method makes sense to me.
Okay I'll try to open a PR soon :-)

Fix patch:
diff --git a/capnp/src/message.rs b/capnp/src/message.rs
index 51f4574e7..60f57311f 100644
--- a/capnp/src/message.rs
+++ b/capnp/src/message.rs
@@ -298,6 +298,11 @@ where
     pub fn size_in_words(&self) -> usize {
         self.arena.size_in_words()
     }
+
+    /// Retrieves the underlying [`ReaderSegments`] object.
+    pub fn get_segments(&self) -> &S {
+        self.arena.get_segments()
+    }
 }
 
 /// A message reader whose value is known to be of type `T`.
@@ -330,6 +335,11 @@ where
     pub fn into_inner(self) -> Reader<S> {
         self.message
     }
+
+    /// Retrieves the underlying [`ReaderSegments`] object.
+    pub fn get_segments(&self) -> &S {
+        self.message.get_segments()
+    }
 }
 
 impl<S, T> From<Reader<S>> for TypedReader<S, T>
diff --git a/capnp/src/private/arena.rs b/capnp/src/private/arena.rs
index c339cdda2..ee304b5c9 100644
--- a/capnp/src/private/arena.rs
+++ b/capnp/src/private/arena.rs
@@ -82,6 +82,10 @@ where
     pub fn into_segments(self) -> S {
         self.segments
     }
+
+    pub(crate) fn get_segments(&self) -> &S {
+        &self.segments
+    }
 }
 
 impl<S> ReaderArena for ReaderArenaImpl<S>


Test patch: