-
Notifications
You must be signed in to change notification settings - Fork 35
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
Lazy reader support for s-expressions #627
Changes from all commits
e0a83d8
89f79aa
840be4d
5db1ff0
07d4a70
181e0a5
357ca8f
716ff34
e29fec5
8f79a36
4cb9b2b
54470d2
78014e7
a6a3aa8
4fc9078
11174ac
719dbaa
f603872
4696ca5
c7129ac
44435ea
d50e05b
8283422
4b53bb3
60d5a17
db9718d
37264a3
f935c64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
#[cfg(feature = "experimental-lazy-reader")] | ||
use ion_rs::IonResult; | ||
|
||
#[cfg(not(feature = "experimental-lazy-reader"))] | ||
fn main() { | ||
println!("This example requires the 'experimental-lazy-reader' feature to work."); | ||
} | ||
|
||
#[cfg(feature = "experimental-lazy-reader")] | ||
use ion_rs::IonResult; | ||
|
||
#[cfg(feature = "experimental-lazy-reader")] | ||
fn main() -> IonResult<()> { | ||
lazy_reader_example::read_all_values() | ||
} | ||
|
||
#[cfg(feature = "experimental-lazy-reader")] | ||
mod lazy_reader_example { | ||
use std::fs::File; | ||
use std::process::exit; | ||
|
||
use memmap::MmapOptions; | ||
|
||
use ion_rs::lazy::r#struct::LazyBinaryStruct; | ||
use ion_rs::lazy::reader::LazyBinaryReader; | ||
use ion_rs::lazy::sequence::LazyBinarySequence; | ||
use ion_rs::lazy::value::LazyBinaryValue; | ||
use ion_rs::lazy::value_ref::ValueRef; | ||
use ion_rs::IonResult; | ||
use memmap::MmapOptions; | ||
use std::fs::File; | ||
use std::process::exit; | ||
|
||
pub fn read_all_values() -> IonResult<()> { | ||
let args: Vec<String> = std::env::args().collect(); | ||
|
@@ -53,14 +53,17 @@ mod lazy_reader_example { | |
fn count_value_and_children(lazy_value: &LazyBinaryValue) -> IonResult<usize> { | ||
use ValueRef::*; | ||
let child_count = match lazy_value.read()? { | ||
List(s) | SExp(s) => count_sequence_children(&s)?, | ||
List(s) => count_sequence_children(s.iter())?, | ||
SExp(s) => count_sequence_children(s.iter())?, | ||
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ In binary Ion, the parsing logic for the bodies of lists and for s-expressions is identical. However, in text the parsing is substantially different. Not only do they have different delimiters for opening ( In order to accommodate these differences without introducing runtime overhead via branching or dynamic dispatch, I had to breaking the Meanwhile, the |
||
Struct(s) => count_struct_children(&s)?, | ||
_ => 0, | ||
}; | ||
Ok(1 + child_count) | ||
} | ||
|
||
fn count_sequence_children(lazy_sequence: &LazyBinarySequence) -> IonResult<usize> { | ||
fn count_sequence_children<'a, 'b>( | ||
lazy_sequence: impl Iterator<Item = IonResult<LazyBinaryValue<'a, 'b>>>, | ||
) -> IonResult<usize> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ This example program takes an iterator to generically handle either a list or sexp. We can always introduce a |
||
let mut count = 0; | ||
for value in lazy_sequence { | ||
count += count_value_and_children(&value?)?; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,7 +188,7 @@ mod tests { | |
let lazy_list = reader.next()?.expect_value()?.read()?.expect_list()?; | ||
// Exercise the `Debug` impl | ||
println!("Lazy Raw Sequence: {:?}", lazy_list); | ||
let mut list_values = lazy_list.iter(); | ||
let mut list_values = lazy_list.sequence.iter(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗺️ In the binary reader, the |
||
assert_eq!(list_values.next().expect("first")?.ion_type(), IonType::Int); | ||
assert_eq!( | ||
list_values.next().expect("second")?.ion_type(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗺️
cargo fmt
rearranged the imports a bit in this file.