From 7f66940408751460920c8498b1f3ae1105c3f4b8 Mon Sep 17 00:00:00 2001 From: Kokoro2336 <2529677678@qq.com> Date: Sun, 1 Mar 2026 19:47:46 +0800 Subject: [PATCH 1/4] feature: add API to_row_in and from_row_in. --- rust/fory-core/src/row/mod.rs | 4 ++-- rust/fory-core/src/row/reader.rs | 5 +++++ rust/fory-core/src/row/writer.rs | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/rust/fory-core/src/row/mod.rs b/rust/fory-core/src/row/mod.rs index 6c6d66f0aa..3fdbf2e8bf 100644 --- a/rust/fory-core/src/row/mod.rs +++ b/rust/fory-core/src/row/mod.rs @@ -21,6 +21,6 @@ mod reader; mod row; mod writer; -pub use reader::{from_row, ArrayViewer, StructViewer}; +pub use reader::{from_row, from_row_from, ArrayViewer, StructViewer}; pub use row::Row; -pub use writer::{to_row, ArrayWriter, StructWriter}; +pub use writer::{to_row, to_row_in, ArrayWriter, StructWriter}; diff --git a/rust/fory-core/src/row/reader.rs b/rust/fory-core/src/row/reader.rs index 1549a55ea7..b375b573af 100644 --- a/rust/fory-core/src/row/reader.rs +++ b/rust/fory-core/src/row/reader.rs @@ -17,6 +17,7 @@ use super::{bit_util::calculate_bitmap_width_in_bytes, row::Row}; use byteorder::{ByteOrder, LittleEndian}; +use crate::buffer::Reader; struct FieldAccessorHelper<'a> { row: &'a [u8], @@ -122,3 +123,7 @@ impl<'r> MapViewer<'r> { pub fn from_row<'a, T: Row<'a>>(row: &'a [u8]) -> T::ReadResult { T::cast(row) } + +pub fn from_row_from<'a, T: Row<'a>>(reader: &Reader<'a>) -> T::ReadResult { + T::cast(&reader.bf[reader.cursor..]) +} diff --git a/rust/fory-core/src/row/writer.rs b/rust/fory-core/src/row/writer.rs index 06bf5dd3c9..e93f8f8aed 100644 --- a/rust/fory-core/src/row/writer.rs +++ b/rust/fory-core/src/row/writer.rs @@ -190,3 +190,10 @@ pub fn to_row<'a, T: Row<'a>>(v: &T) -> Result, Error> { T::write(v, &mut writer)?; Ok(buffer) } + +pub fn to_row_in<'a, T: Row<'a>>(buffer: &mut Vec, v: &T) -> Result { + let start = buffer.len(); + let mut writer = Writer::from_buffer(buffer); + T::write(v, &mut writer)?; + Ok(writer.len() - start) +} From a3ee131262fe7ed1c5fa416ed87acb0db7b90355 Mon Sep 17 00:00:00 2001 From: Kokoro2336 <2529677678@qq.com> Date: Sun, 1 Mar 2026 20:11:17 +0800 Subject: [PATCH 2/4] style: fmt the code. --- rust/fory-core/src/row/mod.rs | 2 +- rust/fory-core/src/row/reader.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/fory-core/src/row/mod.rs b/rust/fory-core/src/row/mod.rs index 3fdbf2e8bf..82bfe7201d 100644 --- a/rust/fory-core/src/row/mod.rs +++ b/rust/fory-core/src/row/mod.rs @@ -21,6 +21,6 @@ mod reader; mod row; mod writer; -pub use reader::{from_row, from_row_from, ArrayViewer, StructViewer}; +pub use reader::{from_row, from_row_in, ArrayViewer, StructViewer}; pub use row::Row; pub use writer::{to_row, to_row_in, ArrayWriter, StructWriter}; diff --git a/rust/fory-core/src/row/reader.rs b/rust/fory-core/src/row/reader.rs index b375b573af..42ef474607 100644 --- a/rust/fory-core/src/row/reader.rs +++ b/rust/fory-core/src/row/reader.rs @@ -16,8 +16,8 @@ // under the License. use super::{bit_util::calculate_bitmap_width_in_bytes, row::Row}; -use byteorder::{ByteOrder, LittleEndian}; use crate::buffer::Reader; +use byteorder::{ByteOrder, LittleEndian}; struct FieldAccessorHelper<'a> { row: &'a [u8], @@ -124,6 +124,6 @@ pub fn from_row<'a, T: Row<'a>>(row: &'a [u8]) -> T::ReadResult { T::cast(row) } -pub fn from_row_from<'a, T: Row<'a>>(reader: &Reader<'a>) -> T::ReadResult { +pub fn from_row_in<'a, T: Row<'a>>(reader: &Reader<'a>) -> T::ReadResult { T::cast(&reader.bf[reader.cursor..]) } From aa9959769d77c381aa274989adfc79e929b6583d Mon Sep 17 00:00:00 2001 From: Kokoro2336 <2529677678@qq.com> Date: Sun, 1 Mar 2026 20:40:16 +0800 Subject: [PATCH 3/4] chore: retrigger the CI. From 7df7a4dd57750092e0defa665105bad1078b254d Mon Sep 17 00:00:00 2001 From: Kokoro2336 <2529677678@qq.com> Date: Tue, 3 Mar 2026 13:41:16 +0800 Subject: [PATCH 4/4] test: add test for from_row_in and to_row_in. --- rust/tests/tests/test_row.rs | 49 +++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/rust/tests/tests/test_row.rs b/rust/tests/tests/test_row.rs index 488a57cfb9..c9a5b0a43c 100644 --- a/rust/tests/tests/test_row.rs +++ b/rust/tests/tests/test_row.rs @@ -17,7 +17,8 @@ use std::collections::BTreeMap; -use fory_core::row::{from_row, to_row}; +use fory_core::row::{from_row, from_row_in, to_row, to_row_in}; +use fory_core::Reader; use fory_derive::ForyRow; #[test] @@ -139,3 +140,49 @@ fn row() { assert_eq!(f5.get("k1").expect("should exists"), &"v1"); assert_eq!(f5.get("k2").expect("should exists"), &"v2"); } + +#[test] +fn test_to_row_in() { + #[derive(ForyRow)] + struct Data { + id: i32, + val: String, + } + + let data = Data { + id: 123, + val: "test".to_string(), + }; + + let mut buffer = vec![1, 2, 3]; + to_row_in(&mut buffer, &data).unwrap(); + + assert!(buffer.len() > 3); + let row = from_row::(&buffer[3..]); + assert_eq!(row.id(), 123); + assert_eq!(row.val(), "test"); +} + +#[test] +fn test_from_row_in() { + #[derive(ForyRow)] + struct Data { + id: i32, + val: String, + } + + let data = Data { + id: 456, + val: "reader_test".to_string(), + }; + + let mut buffer = vec![0u8; 5]; // padding + to_row_in(&mut buffer, &data).unwrap(); + + let mut reader = Reader::new(&buffer); + reader.skip(5).unwrap(); // skip padding + + let row = from_row_in::(&reader); + assert_eq!(row.id(), 456); + assert_eq!(row.val(), "reader_test"); +}