Skip to content

Commit

Permalink
Adds binary Ion 1.0 impl of the new writer API
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Dec 8, 2023
1 parent 51d5a45 commit e178fcc
Show file tree
Hide file tree
Showing 7 changed files with 2,004 additions and 531 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ num-integer = "0.1.44"
num-traits = "0.2"
arrayvec = "0.7"
smallvec = {version ="1.9.0", features = ["const_generics"]}
bumpalo = {version = "3.13.0", features = ["collections"]}
bumpalo = {version = "3.14.0", features = ["collections", "std"]}
digest = { version = "0.9", optional = true }
sha2 = { version = "0.9", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
Expand Down
32 changes: 19 additions & 13 deletions src/lazy/encoder/annotate.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use crate::lazy::encoder::value_writer::AnnotatableValueWriter;
use crate::lazy::encoder::write_as_ion::{WriteAsIon, WriteAsIonValue};
use crate::lazy::encoder::{AnnotatedValueWriter, LazyEncoder};
use crate::raw_symbol_token_ref::AsRawSymbolTokenRef;
use crate::IonResult;
use std::io::Write;

/// Associates a value to serialize with a sequence of annotations.
pub struct Annotated<'a, T: ?Sized, A> {
value: &'a T,
annotations: &'a [A],
}

/// Provides implementors with an extension method ([`annotate`](Annotate::annotate)) that allows
/// Provides implementors with an extension method ([`annotate`](Annotate::annotated_with)) that allows
/// them to be serialized with an associated sequence of annotations.
pub trait Annotate {
/// Pairs a reference to the provided value with a slice containing annotations.
Expand All @@ -25,7 +24,7 @@ pub trait Annotate {
/// let mut buffer = vec![];
/// let mut writer = LazyRawTextWriter_1_0::new(&mut buffer);
///
/// writer.write(42_usize.annotate(&["foo", "bar", "baz"]))?.flush()?;
/// writer.write(42_usize.annotated_with(&["foo", "bar", "baz"]))?.flush()?;
///
/// let expected = Element::read_one("foo::bar::baz::42")?;
/// let actual = Element::read_one(&buffer)?;
Expand All @@ -34,7 +33,7 @@ pub trait Annotate {
///# Ok(())
///# }
/// ```
fn annotate<'a, A: AsRawSymbolTokenRef>(
fn annotated_with<'a, A: AsRawSymbolTokenRef>(
&'a self,
annotations: &'a [A],
) -> Annotated<'a, Self, A>;
Expand All @@ -43,9 +42,9 @@ pub trait Annotate {
// Any Rust value that can be serialized as an Ion value can call `annotate`.
impl<T> Annotate for T
where
T: WriteAsIonValue,
T: ?Sized + WriteAsIonValue,
{
fn annotate<'a, A: AsRawSymbolTokenRef>(
fn annotated_with<'a, A: AsRawSymbolTokenRef>(
&'a self,
annotations: &'a [A],
) -> Annotated<'a, Self, A> {
Expand All @@ -58,16 +57,23 @@ where

// The `Annotated` struct implements `WriteAsIon` by serializing its sequence of annotations
// and then invoking the inner value's implementation of `WriteAsIonValue`.
impl<'b, T, A> WriteAsIon for Annotated<'b, T, A>
impl<'annotations, T, A> WriteAsIon for Annotated<'annotations, T, A>
where
T: WriteAsIonValue,
A: AsRawSymbolTokenRef,
{
fn write_as_ion<'a, W: Write + 'a, E: LazyEncoder<W>, V: AnnotatedValueWriter<'a, W, E>>(
&self,
annotations_writer: V,
) -> IonResult<()> {
let value_writer = annotations_writer.write_annotations(self.annotations.iter())?;
fn write_as_ion<V: AnnotatableValueWriter>(&self, writer: V) -> IonResult<()> {
let value_writer = writer.with_annotations(self.annotations);
self.value.write_as_ion_value(value_writer)
}
}

impl<'annotations, T, A> WriteAsIon for &Annotated<'annotations, T, A>
where
T: WriteAsIonValue,
A: AsRawSymbolTokenRef,
{
fn write_as_ion<V: AnnotatableValueWriter>(&self, writer: V) -> IonResult<()> {
(*self).write_as_ion(writer)
}
}
Loading

0 comments on commit e178fcc

Please sign in to comment.