Skip to content

Commit

Permalink
Removes LazyRawWriter::finish, renames SequenceWriter::end to close
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Apr 10, 2024
1 parent a80ea20 commit 020e078
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 70 deletions.
8 changes: 4 additions & 4 deletions benches/write_many_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ fn write_eexp_with_symbol_values(value_writer: impl ValueWriter) -> IonResult<()
.write(symbol_id(black_box(11)))?
// $12 = "2022-12-07T20:59:59.744000Z" (string, not timestamp)
.write(symbol_id(black_box(12)))?;
nested_eexp.end()?;
eexp.end()
nested_eexp.close()?;
eexp.close()
}

fn write_eexp_with_string_values(value_writer: impl ValueWriter) -> IonResult<()> {
Expand All @@ -108,8 +108,8 @@ fn write_eexp_with_string_values(value_writer: impl ValueWriter) -> IonResult<()
nested_eexp
.write(black_box("region 4"))?
.write(black_box("2022-12-07T20:59:59.744000Z"))?;
nested_eexp.end()?;
eexp.end()
nested_eexp.close()?;
eexp.close()
}

fn symbol_id(sid: usize) -> RawSymbolTokenRef<'static> {
Expand Down
4 changes: 2 additions & 2 deletions examples/write_log_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ mod example {
eexp
// Ignore the part of the thread name that starts with the recurring prefix.
.write(&self.0[THREAD_NAME_PREFIX.len()..])?;
eexp.end()
eexp.close()
}
}

Expand All @@ -227,7 +227,7 @@ mod example {
// Wrap the thread name in the `ThreadName` wrapper to change its serialization.
.write(ThreadName(&event.thread_name))?
.write(&event.parameters)?;
eexp.end()
eexp.close()
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/lazy/encoder/binary/v1_0/container_writers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ impl<'value, 'top> MakeValueWriter for BinaryListWriter_1_0<'value, 'top> {
}

impl<'value, 'top> SequenceWriter for BinaryListWriter_1_0<'value, 'top> {
type End = ();
type Resources = ();

fn end(self) -> IonResult<Self::End> {
fn close(self) -> IonResult<Self::Resources> {
self.container_writer.end()
}
}
Expand All @@ -227,9 +227,9 @@ impl<'value, 'top> MakeValueWriter for BinarySExpWriter_1_0<'value, 'top> {
}

impl<'value, 'top> SequenceWriter for BinarySExpWriter_1_0<'value, 'top> {
type End = ();
type Resources = ();

fn end(self) -> IonResult<Self::End> {
fn close(self) -> IonResult<Self::Resources> {
self.container_writer.end()
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/lazy/encoder/binary/v1_0/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ mod tests {
let expected = Element::read_all(expected)?;
let mut writer = LazyRawBinaryWriter_1_0::new(Vec::new())?;
test(&mut writer)?;
let buffer = writer.finish()?;
let buffer = writer.close()?;
let actual = Element::read_all(buffer)?;
assert!(
IonData::eq(&expected, &actual),
Expand Down Expand Up @@ -502,7 +502,7 @@ mod tests {
#[test]
fn write_empty_list() -> IonResult<()> {
let expected = "[]";
writer_test(expected, |writer| writer.list_writer()?.end())
writer_test(expected, |writer| writer.list_writer()?.close())
}

#[test]
Expand Down Expand Up @@ -530,14 +530,14 @@ mod tests {
.write(Timestamp::with_ymd(2023, 11, 9).build()?)?
.write([0xE0u8, 0x01, 0x00, 0xEA])?
.write([1, 2, 3])?;
list.end()
list.close()
})
}

#[test]
fn write_empty_sexp() -> IonResult<()> {
let expected = "()";
writer_test(expected, |writer| writer.sexp_writer()?.end())
writer_test(expected, |writer| writer.sexp_writer()?.close())
}

#[test]
Expand Down Expand Up @@ -565,7 +565,7 @@ mod tests {
.write(Timestamp::with_ymd(2023, 11, 9).build()?)?
.write([0xE0u8, 0x01, 0x00, 0xEA])?
.write([1, 2, 3])?;
sexp.end()
sexp.close()
})
}

Expand Down
13 changes: 4 additions & 9 deletions src/lazy/encoder/binary/v1_0/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ impl<W: Write> LazyRawBinaryWriter_1_0<W> {
Ok(())
}

pub fn finish(mut self) -> IonResult<W> {
self.flush()?;
Ok(self.output)
}

pub(crate) fn value_writer(&mut self) -> BinaryValueWriter_1_0<'_, '_> {
let top_level = match self.encoding_buffer_ptr {
// If the `encoding_buffer_ptr` is set, we already allocated an encoding buffer on
Expand Down Expand Up @@ -129,7 +124,6 @@ impl<W: Write> LazyRawWriter<W> for LazyRawBinaryWriter_1_0<W> {
delegate! {
to self {
fn flush(&mut self) -> IonResult<()>;
fn finish(self) -> IonResult<W>;
}
}
}
Expand All @@ -143,10 +137,11 @@ impl<W: Write> MakeValueWriter for LazyRawBinaryWriter_1_0<W> {
}

impl<W: Write> SequenceWriter for LazyRawBinaryWriter_1_0<W> {
type End = W;
type Resources = W;

fn end(self) -> IonResult<Self::End> {
self.finish()
fn close(mut self) -> IonResult<Self::Resources> {
self.flush()?;
Ok(self.output)
}
// Uses the default method implementations from SequenceWriter
}
12 changes: 6 additions & 6 deletions src/lazy/encoder/binary/v1_1/container_writers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ impl<'value, 'top> MakeValueWriter for BinaryListWriter_1_1<'value, 'top> {
}

impl<'value, 'top> SequenceWriter for BinaryListWriter_1_1<'value, 'top> {
type End = ();
type Resources = ();

fn write<V: WriteAsIon>(&mut self, value: V) -> IonResult<&mut Self> {
self.container_writer.write(value)?;
Ok(self)
}

fn end(self) -> IonResult<Self::End> {
fn close(self) -> IonResult<Self::Resources> {
self.container_writer.end()
}
}
Expand Down Expand Up @@ -251,14 +251,14 @@ impl<'value, 'top> MakeValueWriter for BinarySExpWriter_1_1<'value, 'top> {
}

impl<'value, 'top> SequenceWriter for BinarySExpWriter_1_1<'value, 'top> {
type End = ();
type Resources = ();

fn write<V: WriteAsIon>(&mut self, value: V) -> IonResult<&mut Self> {
self.container_writer.write(value)?;
Ok(self)
}

fn end(self) -> IonResult<Self::End> {
fn close(self) -> IonResult<Self::Resources> {
self.container_writer.end()
}
}
Expand Down Expand Up @@ -383,9 +383,9 @@ impl<'value, 'top> MakeValueWriter for BinaryEExpWriter_1_1<'value, 'top> {
}

impl<'value, 'top> SequenceWriter for BinaryEExpWriter_1_1<'value, 'top> {
type End = ();
type Resources = ();

fn end(self) -> IonResult<Self::End> {
fn close(self) -> IonResult<Self::Resources> {
// Nothing to do
// TODO: When we have length-prefixed macro invocations, this will require a step to flush the buffered encoding.
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/lazy/encoder/binary/v1_1/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,7 @@ mod tests {
.with_delimited_containers()
.list_writer()?;
list.write_all(*value)?;
list.end()
list.close()
},
expected_encoding,
)?;
Expand Down Expand Up @@ -2323,7 +2323,7 @@ mod tests {
.with_delimited_containers()
.sexp_writer()?;
sexp.write_all(*value)?;
sexp.end()
sexp.close()
},
expected_encoding,
)?;
Expand Down Expand Up @@ -2781,7 +2781,7 @@ mod tests {
args.write_symbol("foo")?
.write_symbol("bar")?
.write_symbol("baz")?;
args.end()
args.close()
},
&[
0x00, // Invoke macro address 0
Expand Down
14 changes: 4 additions & 10 deletions src/lazy/encoder/binary/v1_1/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ impl<W: Write> LazyRawBinaryWriter_1_1<W> {
Ok(())
}

pub fn finish(mut self) -> IonResult<W> {
self.flush()?;
Ok(self.output)
}

// All methods called on the writer are inherently happening at the top level. At the top level,
// the lifetimes `'value` and `'top` are identical. In this method signature, '_ is used for both.
pub(crate) fn value_writer(&mut self) -> BinaryValueWriter_1_1<'_, '_> {
Expand Down Expand Up @@ -140,7 +135,6 @@ impl<W: Write> LazyRawWriter<W> for LazyRawBinaryWriter_1_1<W> {
delegate! {
to self {
fn flush(&mut self) -> IonResult<()>;
fn finish(self) -> IonResult<W>;
}
}
}
Expand All @@ -154,10 +148,10 @@ impl<W: Write> MakeValueWriter for LazyRawBinaryWriter_1_1<W> {
}

impl<W: Write> SequenceWriter for LazyRawBinaryWriter_1_1<W> {
type End = W;
type Resources = W;

fn end(self) -> IonResult<Self::End> {
// Ending the top-level sequence is the same as calling `finish` on the writer itself.
self.finish()
fn close(mut self) -> IonResult<Self::Resources> {
self.flush()?;
Ok(self.output)
}
}
4 changes: 1 addition & 3 deletions src/lazy/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ pub(crate) mod private {
}

/// An Ion writer without an encoding context (that is: symbol/macro tables).
pub trait LazyRawWriter<W: Write>: SequenceWriter<End = W> {
pub trait LazyRawWriter<W: Write>: SequenceWriter<Resources = W> {
fn new(output: W) -> IonResult<Self>
where
Self: Sized;
fn build<E: Encoding>(config: WriteConfig<E>, output: W) -> IonResult<Self>
where
Self: Sized;
fn flush(&mut self) -> IonResult<()>;

fn finish(self) -> IonResult<W>;
}

#[cfg(test)]
Expand Down
14 changes: 4 additions & 10 deletions src/lazy/encoder/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ impl<W: Write> LazyRawTextWriter_1_0<W> {
Ok(())
}

pub fn finish(mut self) -> IonResult<W> {
self.flush()?;
Ok(self.output)
}

/// Helper method to construct this format's `ValueWriter` implementation.
#[inline]
fn value_writer(&mut self) -> TextValueWriter_1_0<'_, W> {
Expand All @@ -61,11 +56,11 @@ impl<W: Write> LazyRawTextWriter_1_0<W> {
}

impl<W: Write> SequenceWriter for LazyRawTextWriter_1_0<W> {
type End = W;
type Resources = W;

fn end(self) -> IonResult<Self::End> {
// Calling `end()` on the top level sequence is the same as calling `finish()` on the writer.
self.finish()
fn close(mut self) -> IonResult<Self::Resources> {
self.flush()?;
Ok(self.output)
}
}

Expand Down Expand Up @@ -99,7 +94,6 @@ impl<W: Write> LazyRawWriter<W> for LazyRawTextWriter_1_0<W> {
delegate! {
to self {
fn flush(&mut self) -> IonResult<()>;
fn finish(self) -> IonResult<W>;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lazy/encoder/text/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ impl<'top, W: Write> MakeValueWriter for TextListWriter_1_0<'top, W> {
}

impl<'top, W: Write> SequenceWriter for TextListWriter_1_0<'top, W> {
type End = ();
type Resources = ();

fn write<V: WriteAsIon>(&mut self, value: V) -> IonResult<&mut Self> {
self.write(value)
}

fn end(self) -> IonResult<Self::End> {
fn close(self) -> IonResult<Self::Resources> {
self.end()
}
}
Expand Down Expand Up @@ -270,15 +270,15 @@ impl<'value, W: Write> MakeValueWriter for TextSExpWriter_1_0<'value, W> {
}

impl<'a, W: Write> SequenceWriter for TextSExpWriter_1_0<'a, W> {
type End = ();
type Resources = ();

delegate! {
to self {
fn write<V: WriteAsIon>(&mut self, value: V) -> IonResult<&mut Self>;
}
}

fn end(self) -> IonResult<Self::End> {
fn close(self) -> IonResult<Self::Resources> {
self.end()
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/lazy/encoder/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ pub trait ValueWriter: Sized {
type AnnotatedValueWriter<'a, SymbolType: AsRawSymbolTokenRef + 'a>: ValueWriter
where
Self: 'a;
type ListWriter: SequenceWriter<End = ()>;
type SExpWriter: SequenceWriter<End = ()>;
type ListWriter: SequenceWriter<Resources = ()>;
type SExpWriter: SequenceWriter<Resources = ()>;
type StructWriter: StructWriter;
type EExpWriter: EExpWriter<End = ()>;
type EExpWriter: EExpWriter<Resources = ()>;

fn write_null(self, ion_type: IonType) -> IonResult<()>;
fn write_bool(self, value: bool) -> IonResult<()>;
Expand Down Expand Up @@ -75,13 +75,13 @@ pub trait ValueWriter: Sized {
fn write_list<V: WriteAsIon, I: IntoIterator<Item = V>>(self, values: I) -> IonResult<()> {
let mut list = self.list_writer()?;
list.write_all(values)?;
list.end()
list.close()
}

fn write_sexp<V: WriteAsIon, I: IntoIterator<Item = V>>(self, values: I) -> IonResult<()> {
let mut sexp = self.sexp_writer()?;
sexp.write_all(values)?;
sexp.end()
sexp.close()
}

fn write_struct<K: AsRawSymbolTokenRef, V: WriteAsIon, I: IntoIterator<Item = (K, V)>>(
Expand Down Expand Up @@ -300,15 +300,15 @@ macro_rules! delegate_and_return_self {
}

pub trait SequenceWriter: MakeValueWriter {
/// The type returned by the [`end`](Self::end) method.
/// The type returned by the [`end`](Self::close) method.
///
/// For top-level writers, this can be any resource(s) owned by the writer that need to survive
/// after the writer is dropped. (For example, a `BufWriter` or `Vec` serving as the output.)
///
/// Containers and E-expressions must use `()`.
// ^^^ This constraint could be loosened if needed, but it requires using verbose references
// to `<MyType as SequenceWriter>::End` in a variety of APIs.
type End;
type Resources;

fn value_writer(&mut self) -> Self::ValueWriter<'_> {
<Self as MakeValueWriter>::make_value_writer(self)
Expand All @@ -334,7 +334,7 @@ pub trait SequenceWriter: MakeValueWriter {
/// Closes out the sequence being written. Delimited writers can use this opportunity to emit
/// a sentinel value, and length-prefixed writers can flush any buffered data to the output
/// buffer.
fn end(self) -> IonResult<Self::End>;
fn close(self) -> IonResult<Self::Resources>;

// Creates functions that delegate to the ValueWriter method of the same name but which then
// return `self` so it can be re-used/chained.
Expand Down
Loading

0 comments on commit 020e078

Please sign in to comment.