noodles_bgzf/gzi/io/writer.rs
1mod index;
2
3use std::io::{self, Write};
4
5use self::index::write_index;
6use crate::gzi::Index;
7
8/// A GZ index (GZI) writer.
9pub struct Writer<W> {
10 inner: W,
11}
12
13impl<W> Writer<W> {
14 /// Returns a reference to the underlying writer.
15 ///
16 /// # Examples
17 ///
18 /// ```
19 /// # use std::io;
20 /// use noodles_bgzf::gzi;
21 /// let writer = gzi::io::Writer::new(io::sink());
22 /// let _inner = writer.get_ref();
23 /// ```
24 pub fn get_ref(&self) -> &W {
25 &self.inner
26 }
27
28 /// Returns a mutable reference to the underlying writer.
29 ///
30 /// # Examples
31 ///
32 /// ```
33 /// # use std::io;
34 /// use noodles_bgzf::gzi;
35 /// let mut writer = gzi::io::Writer::new(io::sink());
36 /// let _inner = writer.get_mut();
37 /// ```
38 pub fn get_mut(&mut self) -> &mut W {
39 &mut self.inner
40 }
41
42 /// Returns the underlying writer.
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// # use std::io;
48 /// use noodles_bgzf::gzi;
49 /// let writer = gzi::io::Writer::new(io::sink());
50 /// let _inner = writer.into_inner();
51 /// ```
52 pub fn into_inner(self) -> W {
53 self.inner
54 }
55}
56
57impl<W> Writer<W>
58where
59 W: Write,
60{
61 /// Creates a GZ index writer.
62 ///
63 /// # Examples
64 ///
65 /// ```
66 /// # use std::io;
67 /// use noodles_bgzf::gzi;
68 /// let writer = gzi::io::Writer::new(io::sink());
69 /// ```
70 pub fn new(inner: W) -> Self {
71 Self { inner }
72 }
73
74 /// Writes a GZ index.
75 ///
76 /// # Examples
77 ///
78 /// ```
79 /// # use std::io;
80 /// use noodles_bgzf::gzi;
81 /// let index = gzi::Index::default();
82 /// let mut writer = gzi::io::Writer::new(io::sink());
83 /// writer.write_index(&index)?;
84 /// # Ok::<_, std::io::Error>(())
85 /// ```
86 pub fn write_index(&mut self, index: &Index) -> io::Result<()> {
87 write_index(&mut self.inner, index)
88 }
89}