11//! The Writer is the primary interface for writing values in avro encoded format.
22
3- use rand:: { thread_rng, Rng } ;
4-
53use crate :: codec:: Codec ;
64use crate :: schema:: Schema ;
75use crate :: value:: Value ;
8- use serde:: Serialize ;
9-
106use crate :: config:: { DEFAULT_FLUSH_INTERVAL , MAGIC_BYTES , SYNC_MARKER_SIZE } ;
117use crate :: error:: { AvrowErr , AvrowResult } ;
128use crate :: schema:: Registry ;
139use crate :: schema:: Variant ;
1410use crate :: serde_avro;
1511use crate :: util:: { encode_long, encode_raw_bytes} ;
1612use crate :: value:: Map ;
13+ use serde:: Serialize ;
14+ use rand:: { thread_rng, Rng } ;
1715use std:: collections:: HashMap ;
1816use std:: default:: Default ;
1917use std:: io:: Write ;
@@ -34,7 +32,7 @@ pub struct WriterBuilder<'a, W> {
3432}
3533
3634impl < ' a , W : Write > WriterBuilder < ' a , W > {
37- /// Creates a builder instance to construct a Writer
35+ /// Creates a builder instance to construct a Writer.
3836 pub fn new ( ) -> Self {
3937 WriterBuilder {
4038 metadata : Default :: default ( ) ,
@@ -52,35 +50,34 @@ impl<'a, W: Write> WriterBuilder<'a, W> {
5250 self
5351 }
5452
55- /// Set one of the available codec . This requires the respective code feature flags to be enabled.
53+ /// Set one of the available codecs . This requires the respective feature flags to be enabled.
5654 pub fn set_codec ( mut self , codec : Codec ) -> Self {
5755 self . codec = codec;
5856 self
5957 }
6058
61- /// Provide the writer with a reference to the schema file
59+ /// Provide the writer with a reference to the schema file.
6260 pub fn set_schema ( mut self , schema : & ' a Schema ) -> Self {
6361 self . schema = Some ( schema) ;
6462 self
6563 }
6664
67- /// Set the underlying output stream. This can be anything which implements the Write trait.
65+ /// Set the underlying output stream. This can be any type that implements the ` Write` trait.
6866 pub fn set_datafile ( mut self , w : W ) -> Self {
6967 self . datafile = Some ( w) ;
7068 self
7169 }
7270
73- /// Set the flush interval (bytes) for the internal block buffer. It's the amount of bytes post which
74- /// the internal buffer is written to the underlying datafile. Defaults to [DEFAULT_FLUSH_INTERVAL].
71+ /// Set the flush interval (in bytes) for the internal buffer. It's the amount of bytes post which
72+ /// the internal buffer is written to the underlying datafile or output stream..
73+ /// Defaults to [`DEFAULT_FLUSH_INTERVAL`](config/constant.DEFAULT_FLUSH_INTERVAL.html).
7574 pub fn set_flush_interval ( mut self , interval : usize ) -> Self {
7675 self . flush_interval = interval;
7776 self
7877 }
7978
80- /// Builds the Writer instance consuming this builder.
79+ /// Builds the ` Writer` instance consuming this builder.
8180 pub fn build ( self ) -> AvrowResult < Writer < ' a , W > > {
82- // write the metadata
83- // Writer::with_codec(&self.schema, self.datafile, self.codec)
8481 let mut writer = Writer {
8582 out_stream : self . datafile . ok_or ( AvrowErr :: WriterBuildFailed ) ?,
8683 schema : self . schema . ok_or ( AvrowErr :: WriterBuildFailed ) ?,
@@ -103,7 +100,7 @@ impl<'a, W: Write> Default for WriterBuilder<'a, W> {
103100
104101/// The Writer is the primary interface for writing values to an avro datafile or a byte container (say a `Vec<u8>`).
105102/// It takes a reference to the schema for validating the values being written
106- /// and an output stream W which can be any type
103+ /// and an output stream `W` which can be any type
107104/// implementing the [Write](https://doc.rust-lang.org/std/io/trait.Write.html) trait.
108105pub struct Writer < ' a , W > {
109106 out_stream : W ,
@@ -116,7 +113,8 @@ pub struct Writer<'a, W> {
116113}
117114
118115impl < ' a , W : Write > Writer < ' a , W > {
119- /// Creates a new avro Writer instance taking a reference to a `Schema` and and a `Write`.
116+ /// Creates a new avro `Writer` instance taking a reference to a `Schema`
117+ /// and a type implementing [`Write`](https://doc.rust-lang.org/std/io/trait.Write.html).
120118 pub fn new ( schema : & ' a Schema , out_stream : W ) -> AvrowResult < Self > {
121119 let mut writer = Writer {
122120 out_stream,
@@ -131,8 +129,8 @@ impl<'a, W: Write> Writer<'a, W> {
131129 Ok ( writer)
132130 }
133131
134- /// Same as the new method, but additionally takes a `Codec` as parameter.
135- /// Codecs can be used to compress the encoded data being written in avro format .
132+ /// Same as the ` new` method, but additionally takes a `Codec` as parameter.
133+ /// Codecs can be used to compress the encoded data being written in an avro datafile .
136134 /// Supported codecs as per spec are:
137135 /// * null (default): No compression is applied.
138136 /// * [snappy](https://en.wikipedia.org/wiki/Snappy_(compression)) (`--features snappy`)
@@ -157,7 +155,9 @@ impl<'a, W: Write> Writer<'a, W> {
157155 /// Appends a value to the buffer.
158156 /// Before a value gets written, it gets validated with the schema referenced
159157 /// by this writer.
160- /// **Note**: writes are buffered internally as per the flush interval and the underlying
158+ ///
159+ /// # Note:
160+ /// writes are buffered internally as per the flush interval (for performance) and the underlying
161161 /// buffer may not reflect values immediately.
162162 /// Call [`flush`](struct.Writer.html#method.flush) to explicitly write all buffered data.
163163 /// Alternatively calling [`into_inner`](struct.Writer.html#method.into_inner) on the writer
@@ -194,7 +194,6 @@ impl<'a, W: Write> Writer<'a, W> {
194194 }
195195
196196 /// Sync/flush any buffered data to the underlying buffer.
197- /// Note: This method is called to ensure that all
198197 pub fn flush ( & mut self ) -> AvrowResult < ( ) > {
199198 // bail if no data is written or it has already been flushed before
200199 if self . block_count == 0 {
@@ -248,8 +247,8 @@ impl<'a, W: Write> Writer<'a, W> {
248247 Ok ( ( ) )
249248 }
250249
251- /// Consumes self and yields the inner Write instance.
252- /// Additionally calls flush if no flush has happened before this call.
250+ /// Consumes self and yields the inner ` Write` instance.
251+ /// Additionally calls ` flush` if no flush has happened before this call.
253252 pub fn into_inner ( mut self ) -> AvrowResult < W > {
254253 self . flush ( ) ?;
255254 Ok ( self . out_stream )
0 commit comments