martin-g commented on code in PR #469:
URL: https://github.com/apache/avro-rs/pull/469#discussion_r2826114493
##########
avro/src/writer.rs:
##########
@@ -542,6 +542,60 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
+/// A buffer that can be cleared.
+pub trait Clearable {
+ /// Clear the buffer, keeping the capacity.
Review Comment:
```suggestion
/// Clear the buffer.
```
There is no need to mention the `capacity` here. We don't really require it.
##########
avro/src/writer.rs:
##########
@@ -542,6 +542,60 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
+/// A buffer that can be cleared.
+pub trait Clearable {
+ /// Clear the buffer, keeping the capacity.
+ fn clear(&mut self);
+}
+
+impl Clearable for Vec<u8> {
+ fn clear(&mut self) {
+ self.clear();
+ }
+}
+
+impl<'a, W: Clearable + Write> Writer<'a, W> {
+ /// Reset the writer.
+ ///
+ /// This will clear the underlying writer and the internal buffer.
+ /// It will also clear any user metadata added.
+ ///
+ /// # Example
+ /// ```
+ /// # use apache_avro::{Writer, Schema, Error};
+ /// # let schema = Schema::Boolean;
+ /// # let values = [true, false];
+ /// # fn send(_: &Vec<u8>) {}
+ /// let mut writer = Writer::new(&schema, Vec::new())?;
+ ///
+ /// // Write some values
+ /// for value in values {
+ /// writer.append_value(value)?;
+ /// }
+ ///
+ /// // Flush the buffer and only then do something with buffer
+ /// writer.flush()?;
+ /// send(writer.get_ref());
+ ///
+ /// // Reset the writer
+ /// writer.reset();
+ ///
+ /// // Write some values again
+ /// for value in values {
+ /// writer.append(value)?;
+ /// }
+ ///
+ /// # Ok::<(), Error>(())
+ /// ```
Review Comment:
```suggestion
/// ```
///
/// # Warning
/// Any data that has been appended but not yet flushed will be silently
/// discarded. Call [`flush`](Writer::flush) before `reset()` if you need
/// to preserve in-flight records.
```
##########
avro/src/writer.rs:
##########
@@ -542,6 +542,60 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
+/// A buffer that can be cleared.
+pub trait Clearable {
+ /// Clear the buffer, keeping the capacity.
+ fn clear(&mut self);
+}
+
+impl Clearable for Vec<u8> {
+ fn clear(&mut self) {
+ self.clear();
+ }
+}
+
+impl<'a, W: Clearable + Write> Writer<'a, W> {
+ /// Reset the writer.
+ ///
+ /// This will clear the underlying writer and the internal buffer.
+ /// It will also clear any user metadata added.
+ ///
+ /// # Example
+ /// ```
+ /// # use apache_avro::{Writer, Schema, Error};
+ /// # let schema = Schema::Boolean;
+ /// # let values = [true, false];
+ /// # fn send(_: &Vec<u8>) {}
+ /// let mut writer = Writer::new(&schema, Vec::new())?;
+ ///
+ /// // Write some values
+ /// for value in values {
+ /// writer.append_value(value)?;
+ /// }
+ ///
+ /// // Flush the buffer and only then do something with buffer
+ /// writer.flush()?;
+ /// send(writer.get_ref());
+ ///
+ /// // Reset the writer
+ /// writer.reset();
+ ///
+ /// // Write some values again
+ /// for value in values {
+ /// writer.append(value)?;
+ /// }
+ ///
+ /// # Ok::<(), Error>(())
+ /// ```
+ pub fn reset(&mut self) {
+ self.buffer.clear();
+ self.writer.clear();
+ self.has_header = false;
Review Comment:
Do we need a new `marker` here ?!
##########
avro/src/writer.rs:
##########
@@ -542,6 +542,60 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
+/// A buffer that can be cleared.
+pub trait Clearable {
+ /// Clear the buffer, keeping the capacity.
+ fn clear(&mut self);
+}
+
+impl Clearable for Vec<u8> {
+ fn clear(&mut self) {
+ self.clear();
+ }
+}
+
+impl<'a, W: Clearable + Write> Writer<'a, W> {
+ /// Reset the writer.
+ ///
+ /// This will clear the underlying writer and the internal buffer.
+ /// It will also clear any user metadata added.
+ ///
+ /// # Example
+ /// ```
+ /// # use apache_avro::{Writer, Schema, Error};
+ /// # let schema = Schema::Boolean;
+ /// # let values = [true, false];
+ /// # fn send(_: &Vec<u8>) {}
+ /// let mut writer = Writer::new(&schema, Vec::new())?;
+ ///
+ /// // Write some values
+ /// for value in values {
+ /// writer.append_value(value)?;
+ /// }
+ ///
+ /// // Flush the buffer and only then do something with buffer
+ /// writer.flush()?;
+ /// send(writer.get_ref());
+ ///
+ /// // Reset the writer
+ /// writer.reset();
+ ///
+ /// // Write some values again
+ /// for value in values {
+ /// writer.append(value)?;
Review Comment:
```suggestion
/// writer.append_value(value)?;
```
##########
avro/src/writer.rs:
##########
@@ -542,6 +542,60 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
+/// A buffer that can be cleared.
+pub trait Clearable {
+ /// Clear the buffer, keeping the capacity.
+ fn clear(&mut self);
+}
+
+impl Clearable for Vec<u8> {
+ fn clear(&mut self) {
+ self.clear();
Review Comment:
This works but it looks like a recursion. Maybe:
```suggestion
Vec::clear(self);
```
to make it more explicit.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]