diff --git a/src/lazy/encoder/binary/v1_0/container_writers.rs b/src/lazy/encoder/binary/v1_0/container_writers.rs index 237cfdd3..784860f7 100644 --- a/src/lazy/encoder/binary/v1_0/container_writers.rs +++ b/src/lazy/encoder/binary/v1_0/container_writers.rs @@ -9,7 +9,9 @@ use crate::lazy::encoder::value_writer::{SequenceWriter, StructWriter}; use crate::lazy::encoder::write_as_ion::WriteAsIon; use crate::raw_symbol_ref::AsRawSymbolRef; use crate::result::{EncodingError, IonFailure}; -use crate::{v1_0, Encoding, IonError, IonResult, RawSymbolRef, SymbolId, ValueWriterConfig}; +use crate::{ + v1_0, ContextWriter, Encoding, IonError, IonResult, RawSymbolRef, SymbolId, ValueWriterConfig, +}; /// A helper type that holds fields and logic that is common to [`BinaryListWriter_1_0`], /// [`BinarySExpWriter_1_0`], and [`BinaryStructWriter_1_0`]. @@ -197,10 +199,15 @@ impl<'value, 'top> BinaryListWriter_1_0<'value, 'top> { } } -impl<'top> MakeValueWriter for BinaryListWriter_1_0<'_, 'top> { - type ValueWriter<'a> = BinaryValueWriter_1_0<'a, 'top> where Self: 'a; +impl<'top> ContextWriter for BinaryListWriter_1_0<'_, 'top> { + type NestedValueWriter<'a> + = BinaryValueWriter_1_0<'a, 'top> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for BinaryListWriter_1_0<'_, '_> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { BinaryValueWriter_1_0::new( self.container_writer.allocator, &mut self.container_writer.child_values_buffer, @@ -216,10 +223,15 @@ impl SequenceWriter for BinaryListWriter_1_0<'_, '_> { } } -impl<'top> MakeValueWriter for BinarySExpWriter_1_0<'_, 'top> { - type ValueWriter<'a> = BinaryValueWriter_1_0<'a, 'top> where Self: 'a; +impl<'top> ContextWriter for BinarySExpWriter_1_0<'_, 'top> { + type NestedValueWriter<'a> + = BinaryValueWriter_1_0<'a, 'top> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for BinarySExpWriter_1_0<'_, '_> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { BinaryValueWriter_1_0::new( self.container_writer.allocator, &mut self.container_writer.child_values_buffer, @@ -322,12 +334,15 @@ impl FieldEncoder for BinaryStructWriter_1_0<'_, '_> { } } -impl<'top> MakeValueWriter for BinaryStructWriter_1_0<'_, 'top> { - type ValueWriter<'a> = BinaryValueWriter_1_0<'a, 'top> +impl<'top> ContextWriter for BinaryStructWriter_1_0<'_, 'top> { + type NestedValueWriter<'a> + = BinaryValueWriter_1_0<'a, 'top> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for BinaryStructWriter_1_0<'_, '_> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { BinaryValueWriter_1_0::new( self.container_writer.allocator, &mut self.container_writer.child_values_buffer, diff --git a/src/lazy/encoder/binary/v1_0/writer.rs b/src/lazy/encoder/binary/v1_0/writer.rs index f3fa92f9..857c6381 100644 --- a/src/lazy/encoder/binary/v1_0/writer.rs +++ b/src/lazy/encoder/binary/v1_0/writer.rs @@ -13,7 +13,7 @@ use crate::lazy::encoder::LazyRawWriter; use crate::lazy::encoding::Encoding; use crate::unsafe_helpers::{mut_ref_to_ptr, ptr_to_mut_ref, ptr_to_ref}; use crate::write_config::{WriteConfig, WriteConfigKind}; -use crate::{IonEncoding, IonResult}; +use crate::{ContextWriter, IonEncoding, IonResult}; /// A "raw"-level streaming binary Ion writer. This writer does not provide symbol table /// management; symbol-related operations (e.g. setting field IDs and annotations or writing symbol @@ -162,10 +162,15 @@ impl LazyRawWriter for LazyRawBinaryWriter_1_0 { } } -impl MakeValueWriter for LazyRawBinaryWriter_1_0 { - type ValueWriter<'a> = BinaryValueWriter_1_0<'a, 'a> where Self: 'a; +impl ContextWriter for LazyRawBinaryWriter_1_0 { + type NestedValueWriter<'a> + = BinaryValueWriter_1_0<'a, 'a> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for LazyRawBinaryWriter_1_0 { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { self.value_writer() } } diff --git a/src/lazy/encoder/binary/v1_1/container_writers.rs b/src/lazy/encoder/binary/v1_1/container_writers.rs index eb486e66..71211f66 100644 --- a/src/lazy/encoder/binary/v1_1/container_writers.rs +++ b/src/lazy/encoder/binary/v1_1/container_writers.rs @@ -8,7 +8,7 @@ use crate::lazy::encoder::value_writer::{EExpWriter, SequenceWriter, StructWrite use crate::lazy::encoder::value_writer_config::ValueWriterConfig; use crate::lazy::encoder::write_as_ion::WriteAsIon; use crate::raw_symbol_ref::AsRawSymbolRef; -use crate::{v1_1, Encoding, IonResult, UInt}; +use crate::{v1_1, ContextWriter, Encoding, IonResult, UInt}; /// A helper type that holds fields and logic that is common to [`BinaryListWriter_1_1`], /// [`BinarySExpWriter_1_1`], and [`BinaryStructWriter_1_1`]. @@ -202,10 +202,15 @@ impl<'value, 'top> BinaryListWriter_1_1<'value, 'top> { } } -impl<'top> MakeValueWriter for BinaryListWriter_1_1<'_, 'top> { - type ValueWriter<'a> = BinaryValueWriter_1_1<'a, 'top> where Self: 'a; +impl<'top> ContextWriter for BinaryListWriter_1_1<'_, 'top> { + type NestedValueWriter<'a> + = BinaryValueWriter_1_1<'a, 'top> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for BinaryListWriter_1_1<'_, '_> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { self.container_writer.value_writer() } } @@ -267,10 +272,15 @@ impl<'value, 'top> BinarySExpWriter_1_1<'value, 'top> { } } -impl<'top> MakeValueWriter for BinarySExpWriter_1_1<'_, 'top> { - type ValueWriter<'a> = BinaryValueWriter_1_1<'a, 'top> where Self: 'a; +impl<'top> ContextWriter for BinarySExpWriter_1_1<'_, 'top> { + type NestedValueWriter<'a> + = BinaryValueWriter_1_1<'a, 'top> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for BinarySExpWriter_1_1<'_, '_> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { let value_writer_config = self.container_writer.config(); BinaryValueWriter_1_1::new( self.container_writer.allocator(), @@ -373,12 +383,15 @@ impl FieldEncoder for BinaryStructWriter_1_1<'_, '_> { } } -impl<'top> MakeValueWriter for BinaryStructWriter_1_1<'_, 'top> { - type ValueWriter<'a> = BinaryValueWriter_1_1<'a, 'top> +impl<'top> ContextWriter for BinaryStructWriter_1_1<'_, 'top> { + type NestedValueWriter<'a> + = BinaryValueWriter_1_1<'a, 'top> where - Self: 'a,; + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for BinaryStructWriter_1_1<'_, '_> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { self.container_writer.value_writer() } } @@ -418,10 +431,15 @@ impl<'value, 'top> BinaryEExpWriter_1_1<'value, 'top> { } } -impl<'top> MakeValueWriter for BinaryEExpWriter_1_1<'_, 'top> { - type ValueWriter<'a> = BinaryValueWriter_1_1<'a, 'top> where Self: 'a; +impl<'top> ContextWriter for BinaryEExpWriter_1_1<'_, 'top> { + type NestedValueWriter<'a> + = BinaryValueWriter_1_1<'a, 'top> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for BinaryEExpWriter_1_1<'_, '_> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { BinaryValueWriter_1_1::new(self.allocator, self.buffer, self.value_writer_config) } } diff --git a/src/lazy/encoder/binary/v1_1/writer.rs b/src/lazy/encoder/binary/v1_1/writer.rs index 2a8f921d..591ec79a 100644 --- a/src/lazy/encoder/binary/v1_1/writer.rs +++ b/src/lazy/encoder/binary/v1_1/writer.rs @@ -14,7 +14,7 @@ use crate::lazy::encoder::LazyRawWriter; use crate::lazy::encoding::Encoding; use crate::unsafe_helpers::{mut_ref_to_ptr, ptr_to_mut_ref, ptr_to_ref}; use crate::write_config::{WriteConfig, WriteConfigKind}; -use crate::{IonEncoding, IonResult}; +use crate::{ContextWriter, IonEncoding, IonResult}; /// A "raw"-level streaming binary Ion 1.1 writer. This writer does not provide encoding module /// management; symbol- and macro- related operations require the caller to perform their own @@ -162,13 +162,15 @@ impl LazyRawWriter for LazyRawBinaryWriter_1_1 { } } -impl MakeValueWriter for LazyRawBinaryWriter_1_1 { - type ValueWriter<'a> +impl ContextWriter for LazyRawBinaryWriter_1_1 { + type NestedValueWriter<'a> = BinaryValueWriter_1_1<'a, 'a> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for LazyRawBinaryWriter_1_1 { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { self.value_writer() } } diff --git a/src/lazy/encoder/text/v1_0/value_writer.rs b/src/lazy/encoder/text/v1_0/value_writer.rs index 0e799bc7..35010e68 100644 --- a/src/lazy/encoder/text/v1_0/value_writer.rs +++ b/src/lazy/encoder/text/v1_0/value_writer.rs @@ -19,7 +19,8 @@ use crate::text::text_formatter::{FmtValueFormatter, IoValueFormatter}; use crate::text::whitespace_config::WhitespaceConfig; use crate::types::{ContainerType, ParentType}; use crate::{ - v1_0, Decimal, Encoding, Int, IonResult, IonType, RawSymbolRef, Timestamp, ValueWriterConfig, + v1_0, ContextWriter, Decimal, Encoding, Int, IonResult, IonType, RawSymbolRef, Timestamp, + ValueWriterConfig, }; pub struct TextValueWriter_1_0<'value, W: Write> { @@ -296,10 +297,15 @@ impl<'top, W: Write> TextListWriter_1_0<'top, W> { } } -impl MakeValueWriter for TextListWriter_1_0<'_, W> { - type ValueWriter<'a> = TextValueWriter_1_0<'a, W> where Self: 'a; +impl ContextWriter for TextListWriter_1_0<'_, W> { + type NestedValueWriter<'a> + = TextValueWriter_1_0<'a, W> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for TextListWriter_1_0<'_, W> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { self.container_writer.value_writer() } } @@ -353,10 +359,15 @@ impl<'a, W: Write> TextSExpWriter_1_0<'a, W> { } } -impl MakeValueWriter for TextSExpWriter_1_0<'_, W> { - type ValueWriter<'a> = TextValueWriter_1_0<'a, W> where Self: 'a; +impl ContextWriter for TextSExpWriter_1_0<'_, W> { + type NestedValueWriter<'a> + = TextValueWriter_1_0<'a, W> + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for TextSExpWriter_1_0<'_, W> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { self.container_writer.value_writer() } } @@ -422,12 +433,15 @@ impl FieldEncoder for TextStructWriter_1_0<'_, W> { } } -impl MakeValueWriter for TextStructWriter_1_0<'_, W> { - type ValueWriter<'a> = TextValueWriter_1_0<'a, W> +impl ContextWriter for TextStructWriter_1_0<'_, W> { + type NestedValueWriter<'a> + = TextValueWriter_1_0<'a, W> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for TextStructWriter_1_0<'_, W> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { TextValueWriter_1_0 { writer: self.container_writer.writer, depth: self.container_writer.depth + 1, @@ -449,7 +463,10 @@ impl StructWriter for TextStructWriter_1_0<'_, W> { } impl<'value, W: Write + 'value> AnnotatableWriter for TextAnnotatedValueWriter_1_0<'value, W> { - type AnnotatedValueWriter<'a> = TextAnnotatedValueWriter_1_0<'a, W> where Self: 'a; + type AnnotatedValueWriter<'a> + = TextAnnotatedValueWriter_1_0<'a, W> + where + Self: 'a; fn with_annotations<'a>( self, @@ -477,7 +494,10 @@ impl<'value, W: Write + 'value> ValueWriter for TextAnnotatedValueWriter_1_0<'va } impl AnnotatableWriter for TextValueWriter_1_0<'_, W> { - type AnnotatedValueWriter<'a> = TextAnnotatedValueWriter_1_0<'a, W> where Self: 'a; + type AnnotatedValueWriter<'a> + = TextAnnotatedValueWriter_1_0<'a, W> + where + Self: 'a; fn with_annotations<'a>( self, diff --git a/src/lazy/encoder/text/v1_0/writer.rs b/src/lazy/encoder/text/v1_0/writer.rs index 90db6a80..77c8f0a4 100644 --- a/src/lazy/encoder/text/v1_0/writer.rs +++ b/src/lazy/encoder/text/v1_0/writer.rs @@ -13,7 +13,7 @@ use crate::text::whitespace_config::{ }; use crate::types::ParentType; use crate::write_config::WriteConfigKind; -use crate::{IonEncoding, IonResult, TextFormat, WriteConfig}; +use crate::{ContextWriter, IonEncoding, IonResult, TextFormat, WriteConfig}; /// A raw text Ion 1.0 writer. pub struct LazyRawTextWriter_1_0 { @@ -60,12 +60,15 @@ impl SequenceWriter for LazyRawTextWriter_1_0 { } } -impl MakeValueWriter for LazyRawTextWriter_1_0 { - type ValueWriter<'a> = TextValueWriter_1_0<'a, W> +impl ContextWriter for LazyRawTextWriter_1_0 { + type NestedValueWriter<'a> + = TextValueWriter_1_0<'a, W> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for LazyRawTextWriter_1_0 { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { self.value_writer() } } diff --git a/src/lazy/encoder/text/v1_1/value_writer.rs b/src/lazy/encoder/text/v1_1/value_writer.rs index 04447bdd..aec77462 100644 --- a/src/lazy/encoder/text/v1_1/value_writer.rs +++ b/src/lazy/encoder/text/v1_1/value_writer.rs @@ -12,7 +12,9 @@ use crate::lazy::text::raw::v1_1::reader::MacroIdRef; use crate::raw_symbol_ref::AsRawSymbolRef; use crate::result::IonFailure; use crate::types::{ContainerType, ParentType}; -use crate::{v1_1, Decimal, Encoding, Int, IonResult, IonType, Timestamp, ValueWriterConfig}; +use crate::{ + v1_1, ContextWriter, Decimal, Encoding, Int, IonResult, IonType, Timestamp, ValueWriterConfig, +}; use delegate::delegate; use std::io::Write; @@ -180,13 +182,15 @@ pub struct TextListWriter_1_1<'value, W: Write> { writer_1_0: TextListWriter_1_0<'value, W>, } -impl MakeValueWriter for TextListWriter_1_1<'_, W> { - type ValueWriter<'a> +impl ContextWriter for TextListWriter_1_1<'_, W> { + type NestedValueWriter<'a> = TextValueWriter_1_1<'a, W> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for TextListWriter_1_1<'_, W> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { TextValueWriter_1_1 { value_writer_1_0: self.writer_1_0.make_value_writer(), } @@ -205,13 +209,15 @@ pub struct TextSExpWriter_1_1<'value, W: Write> { writer_1_0: TextSExpWriter_1_0<'value, W>, } -impl MakeValueWriter for TextSExpWriter_1_1<'_, W> { - type ValueWriter<'a> +impl ContextWriter for TextSExpWriter_1_1<'_, W> { + type NestedValueWriter<'a> = TextValueWriter_1_1<'a, W> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for TextSExpWriter_1_1<'_, W> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { TextValueWriter_1_1 { value_writer_1_0: self.writer_1_0.make_value_writer(), } @@ -236,13 +242,15 @@ impl FieldEncoder for TextStructWriter_1_1<'_, W> { } } -impl MakeValueWriter for TextStructWriter_1_1<'_, W> { - type ValueWriter<'a> +impl ContextWriter for TextStructWriter_1_1<'_, W> { + type NestedValueWriter<'a> = TextValueWriter_1_1<'a, W> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for TextStructWriter_1_1<'_, W> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { TextValueWriter_1_1 { value_writer_1_0: self.writer_1_0.make_value_writer(), } @@ -296,13 +304,15 @@ impl<'value, W: Write + 'value> SequenceWriter for TextEExpWriter_1_1<'value, W> } } -impl<'value, W: Write + 'value> MakeValueWriter for TextEExpWriter_1_1<'value, W> { - type ValueWriter<'a> +impl<'value, W: Write + 'value> ContextWriter for TextEExpWriter_1_1<'value, W> { + type NestedValueWriter<'a> = TextValueWriter_1_1<'a, W> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl<'value, W: Write + 'value> MakeValueWriter for TextEExpWriter_1_1<'value, W> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { TextValueWriter_1_1 { value_writer_1_0: self.container_writer.value_writer(), } diff --git a/src/lazy/encoder/text/v1_1/writer.rs b/src/lazy/encoder/text/v1_1/writer.rs index 9c160d9b..31d0c96c 100644 --- a/src/lazy/encoder/text/v1_1/writer.rs +++ b/src/lazy/encoder/text/v1_1/writer.rs @@ -10,7 +10,7 @@ use crate::text::whitespace_config::{ COMPACT_WHITESPACE_CONFIG, LINES_WHITESPACE_CONFIG, PRETTY_WHITESPACE_CONFIG, }; use crate::write_config::WriteConfigKind; -use crate::{IonEncoding, IonResult, TextFormat, WriteConfig}; +use crate::{ContextWriter, IonEncoding, IonResult, TextFormat, WriteConfig}; // Text Ion 1.1 is a syntactic superset of Ion 1.0. The types comprising this writer implementation // delegates nearly all of their functionality to the 1.0 text writer. @@ -28,13 +28,15 @@ impl SequenceWriter for LazyRawTextWriter_1_1 { } } -impl MakeValueWriter for LazyRawTextWriter_1_1 { - type ValueWriter<'a> +impl ContextWriter for LazyRawTextWriter_1_1 { + type NestedValueWriter<'a> = TextValueWriter_1_1<'a, W> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for LazyRawTextWriter_1_1 { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { let value_writer_1_0 = self.writer_1_0.make_value_writer(); TextValueWriter_1_1 { value_writer_1_0 } } diff --git a/src/lazy/encoder/value_writer.rs b/src/lazy/encoder/value_writer.rs index d377bb44..723fa852 100644 --- a/src/lazy/encoder/value_writer.rs +++ b/src/lazy/encoder/value_writer.rs @@ -5,17 +5,22 @@ use crate::lazy::text::raw::v1_1::reader::MacroIdRef; use crate::raw_symbol_ref::AsRawSymbolRef; use crate::{Decimal, Int, IonResult, IonType, RawSymbolRef, Timestamp, UInt}; -pub mod internal { - use crate::lazy::encoder::value_writer::ValueWriter; +// This module is `pub(crate)` to deter crates from providing their own implementations of these traits. +pub(crate) mod internal { use crate::raw_symbol_ref::AsRawSymbolRef; - use crate::IonResult; + use crate::{ContextWriter, IonResult}; - pub trait MakeValueWriter { - type ValueWriter<'a>: ValueWriter - where - Self: 'a; - - fn make_value_writer(&mut self) -> Self::ValueWriter<'_>; + pub trait MakeValueWriter: ContextWriter { + /// Constructs a new instance of the implementation's preferred `ValueWriter` type. + /// + /// Instances of `ValueWriter` are single-use by design, as this allows the crate to + /// statically guarantee that the application cannot (e.g.) try to encode multiple + /// values after an annotations sequence or in a struct field. + /// + /// This trait (and this method by extension) are kept `pub(crate)` to prevent users + /// from circumventing this restriction, creating and then using multiple value writers + /// in situations where that would produce illegal data. + fn make_value_writer(&mut self) -> ::NestedValueWriter<'_>; } /// A (private) prerequisite for [`StructWriter`](super::StructWriter) implementations. @@ -31,6 +36,18 @@ pub mod internal { } } +/// The context in which a given value is found: either a container or the top level. +// +// This type is a public-facing version of `MakeValueWriter`. +// It allows applications to refer to the value writer's associated types without exposing +// the `make_value_writer` method. +pub trait ContextWriter { + /// The `ValueWriter` type family the implementor uses to encode data nested in this context. + type NestedValueWriter<'a>: ValueWriter + where + Self: 'a; +} + pub trait EExpWriter: SequenceWriter { // TODO: more methods for writing tagless encodings fn write_flex_uint(&mut self, _value: impl Into) -> IonResult<()> { @@ -218,10 +235,11 @@ impl<'field, StructWriterType> FieldWriter<'field, StructWriterType> { } } -impl AnnotatableWriter - for FieldWriter<'_, StructWriterType> -{ - type AnnotatedValueWriter<'a> = AnnotatedFieldWriter<'a, StructWriterType> where Self: 'a; +impl AnnotatableWriter for FieldWriter<'_, StructWriterType> { + type AnnotatedValueWriter<'a> + = AnnotatedFieldWriter<'a, StructWriterType> + where + Self: 'a; fn with_annotations<'a>( self, @@ -240,13 +258,13 @@ impl AnnotatableWriter impl<'field, StructWriterType: StructWriter> ValueWriter for FieldWriter<'field, StructWriterType> { type ListWriter = - <::ValueWriter<'field> as ValueWriter>::ListWriter; + <::NestedValueWriter<'field> as ValueWriter>::ListWriter; type SExpWriter = - <::ValueWriter<'field> as ValueWriter>::SExpWriter; + <::NestedValueWriter<'field> as ValueWriter>::SExpWriter; type StructWriter = - <::ValueWriter<'field> as ValueWriter>::StructWriter; + <::NestedValueWriter<'field> as ValueWriter>::StructWriter; type EExpWriter = - <::ValueWriter<'field> as ValueWriter>::EExpWriter; + <::NestedValueWriter<'field> as ValueWriter>::EExpWriter; delegate_value_writer_to!(fallible closure |self_: Self| { self_.struct_writer.encode_field_name(self_.name)?; @@ -278,7 +296,10 @@ impl<'field, StructWriterType: StructWriter> AnnotatedFieldWriter<'field, Struct impl AnnotatableWriter for AnnotatedFieldWriter<'_, StructWriterType> { - type AnnotatedValueWriter<'a> = AnnotatedFieldWriter<'a, StructWriterType> where Self: 'a; + type AnnotatedValueWriter<'a> + = AnnotatedFieldWriter<'a, StructWriterType> + where + Self: 'a; fn with_annotations<'a>( self, @@ -299,13 +320,13 @@ impl<'field, StructWriterType: StructWriter> ValueWriter for AnnotatedFieldWriter<'field, StructWriterType> { type ListWriter = - <<::ValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::ListWriter; + <<::NestedValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::ListWriter; type SExpWriter = - <<::ValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::SExpWriter; + <<::NestedValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::SExpWriter; type StructWriter = - <<::ValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::StructWriter; + <<::NestedValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::StructWriter; type EExpWriter = - <<::ValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::EExpWriter; + <<::NestedValueWriter<'field> as AnnotatableWriter>::AnnotatedValueWriter<'field> as ValueWriter>::EExpWriter; delegate_value_writer_to!(fallible closure |self_: Self| { self_.struct_writer.encode_field_name(self_.name)?; @@ -370,7 +391,7 @@ pub trait SequenceWriter: MakeValueWriter { // to `::End` in a variety of APIs. type Resources; - fn value_writer(&mut self) -> Self::ValueWriter<'_> { + fn value_writer(&mut self) -> Self::NestedValueWriter<'_> { ::make_value_writer(self) } @@ -413,22 +434,28 @@ pub trait SequenceWriter: MakeValueWriter { impl AsRef<[u8]> => write_blob, ); - fn list_writer(&mut self) -> IonResult< as ValueWriter>::ListWriter> { + fn list_writer( + &mut self, + ) -> IonResult< as ValueWriter>::ListWriter> { self.value_writer().list_writer() } - fn sexp_writer(&mut self) -> IonResult< as ValueWriter>::SExpWriter> { + fn sexp_writer( + &mut self, + ) -> IonResult< as ValueWriter>::SExpWriter> { self.value_writer().sexp_writer() } - fn struct_writer(&mut self) -> IonResult< as ValueWriter>::StructWriter> { + fn struct_writer( + &mut self, + ) -> IonResult< as ValueWriter>::StructWriter> { self.value_writer().struct_writer() } fn eexp_writer<'a>( &'a mut self, macro_id: impl Into>, - ) -> IonResult< as ValueWriter>::EExpWriter> { + ) -> IonResult< as ValueWriter>::EExpWriter> { self.value_writer().eexp_writer(macro_id) } diff --git a/src/lazy/encoder/writer.rs b/src/lazy/encoder/writer.rs index 7e25a060..c12cc608 100644 --- a/src/lazy/encoder/writer.rs +++ b/src/lazy/encoder/writer.rs @@ -25,8 +25,8 @@ use crate::raw_symbol_ref::AsRawSymbolRef; use crate::result::IonFailure; use crate::write_config::WriteConfig; use crate::{ - Decimal, Element, ElementWriter, Int, IonResult, IonType, IonVersion, MacroTable, RawSymbolRef, - Symbol, SymbolTable, Timestamp, UInt, Value, + ContextWriter, Decimal, Element, ElementWriter, Int, IonResult, IonType, IonVersion, + MacroTable, RawSymbolRef, Symbol, SymbolTable, Timestamp, UInt, Value, }; pub(crate) struct WriterContext { @@ -184,13 +184,15 @@ impl Writer { } } -impl MakeValueWriter for Writer { - type ValueWriter<'a> - = ApplicationValueWriter<'a, > as MakeValueWriter>::ValueWriter<'a>> +impl ContextWriter for Writer { + type NestedValueWriter<'a> + = ApplicationValueWriter<'a, > as ContextWriter>::NestedValueWriter<'a>> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for Writer { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { let raw_value_writer = self.data_writer.make_value_writer(); ApplicationValueWriter { @@ -535,13 +537,15 @@ impl<'value, V: ValueWriter> ApplicationStructWriter<'value, V> { } } -impl MakeValueWriter for ApplicationStructWriter<'_, V> { - type ValueWriter<'a> - = ApplicationValueWriter<'a, ::ValueWriter<'a>> +impl ContextWriter for ApplicationStructWriter<'_, V> { + type NestedValueWriter<'a> + = ApplicationValueWriter<'a, ::NestedValueWriter<'a>> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for ApplicationStructWriter<'_, V> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { ApplicationValueWriter::new( self.encoding, self.value_writer_config, @@ -636,13 +640,15 @@ impl<'value, V: ValueWriter> ApplicationListWriter<'value, V> { } } -impl MakeValueWriter for ApplicationListWriter<'_, V> { - type ValueWriter<'a> - = ApplicationValueWriter<'a, ::ValueWriter<'a>> +impl ContextWriter for ApplicationListWriter<'_, V> { + type NestedValueWriter<'a> + = ApplicationValueWriter<'a, ::NestedValueWriter<'a>> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for ApplicationListWriter<'_, V> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { ApplicationValueWriter::new( self.encoding, self.value_writer_config, @@ -679,13 +685,15 @@ impl<'value, V: ValueWriter> ApplicationSExpWriter<'value, V> { } } -impl MakeValueWriter for ApplicationSExpWriter<'_, V> { - type ValueWriter<'a> - = ApplicationValueWriter<'a, ::ValueWriter<'a>> +impl ContextWriter for ApplicationSExpWriter<'_, V> { + type NestedValueWriter<'a> + = ApplicationValueWriter<'a, ::NestedValueWriter<'a>> where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for ApplicationSExpWriter<'_, V> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { ApplicationValueWriter::new( self.encoding, self.value_writer_config, @@ -730,16 +738,18 @@ impl SequenceWriter for ApplicationEExpWriter<'_, V> { } } -impl MakeValueWriter for ApplicationEExpWriter<'_, V> { - type ValueWriter<'a> +impl ContextWriter for ApplicationEExpWriter<'_, V> { + type NestedValueWriter<'a> = ApplicationValueWriter< 'a, - <::EExpWriter as MakeValueWriter>::ValueWriter<'a>, + <::EExpWriter as ContextWriter>::NestedValueWriter<'a>, > where Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for ApplicationEExpWriter<'_, V> { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { ApplicationValueWriter::new( self.encoding, self.value_writer_config, diff --git a/src/lazy/never.rs b/src/lazy/never.rs index c852575a..8eabd4f1 100644 --- a/src/lazy/never.rs +++ b/src/lazy/never.rs @@ -19,7 +19,7 @@ use crate::lazy::span::Span; use crate::lazy::text::raw::v1_1::arg_group::EExpArg; use crate::lazy::text::raw::v1_1::reader::MacroIdRef; use crate::raw_symbol_ref::AsRawSymbolRef; -use crate::{Decimal, Int, IonResult, IonType, Timestamp, ValueWriterConfig}; +use crate::{ContextWriter, Decimal, Int, IonResult, IonType, Timestamp, ValueWriterConfig}; /// An uninhabited type that signals to the compiler that related code paths are not reachable. #[derive(Debug, Copy, Clone)] @@ -63,11 +63,15 @@ impl StructWriter for Never { } } -impl MakeValueWriter for Never { - type ValueWriter<'a> = Never - where Self: 'a; +impl ContextWriter for Never { + type NestedValueWriter<'a> + = Never + where + Self: 'a; +} - fn make_value_writer(&mut self) -> Self::ValueWriter<'_> { +impl MakeValueWriter for Never { + fn make_value_writer(&mut self) -> Self::NestedValueWriter<'_> { unreachable!("MakeValueWriter::value_writer in Never") } } @@ -75,8 +79,10 @@ impl MakeValueWriter for Never { impl EExpWriter for Never {} impl AnnotatableWriter for Never { - type AnnotatedValueWriter<'a> = Never - where Self: 'a; + type AnnotatedValueWriter<'a> + = Never + where + Self: 'a; fn with_annotations<'a>( self, diff --git a/src/lib.rs b/src/lib.rs index 31727d2f..614eaaf7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -211,8 +211,7 @@ macro_rules! v1_x_reader_writer { lazy::value_ref::ValueRef, lazy::r#struct::{LazyStruct, LazyField}, lazy::sequence::{LazyList, LazySExp}, - lazy::encoder::value_writer::internal::MakeValueWriter, - lazy::encoder::value_writer::{ValueWriter, StructWriter, SequenceWriter, EExpWriter}, + lazy::encoder::value_writer::{ValueWriter, ContextWriter, StructWriter, SequenceWriter, EExpWriter}, lazy::any_encoding::IonEncoding, lazy::expanded::compiler::TemplateCompiler, lazy::expanded::template::TemplateMacro,