rc_writer/
rc_option_writer.rs1use std::cell::RefCell;
2use std::io::{self, ErrorKind, Write};
3use std::rc::Rc;
4
5pub struct RcOptionWriter<W: Write> {
6 inner: Rc<RefCell<Option<W>>>,
7}
8
9impl<W: Write> RcOptionWriter<W> {
10 #[inline]
11 pub fn new(writer: Rc<RefCell<Option<W>>>) -> RcOptionWriter<W> {
12 RcOptionWriter {
13 inner: writer,
14 }
15 }
16}
17
18impl<W: Write> Write for RcOptionWriter<W> {
19 #[inline]
20 fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
21 match self.inner.borrow_mut().as_mut() {
22 Some(writer) => writer.write(buf),
23 None => Err(io::Error::new(ErrorKind::BrokenPipe, "the writer has been removed out")),
24 }
25 }
26
27 #[inline]
28 fn flush(&mut self) -> Result<(), io::Error> {
29 match self.inner.borrow_mut().as_mut() {
30 Some(writer) => writer.flush(),
31 None => Err(io::Error::new(ErrorKind::BrokenPipe, "the writer has been removed out")),
32 }
33 }
34}