spectrusty_utils/
io.rs

1/*
2    Copyright (C) 2020-2022  Rafal Michalski
3
4    This file is part of SPECTRUSTY, a Rust library for building emulators.
5
6    For the full copyright notice, see the lib.rs file.
7*/
8use std::io;
9
10#[cfg(feature = "snapshot")] use core::fmt;
11#[cfg(feature = "snapshot")]
12use serde::{Serialize, Deserialize, Serializer, Deserializer, de::{self, Visitor}};
13
14/// Special version of [io::Sink] that implements Default and Clone traits.
15#[derive(Debug)]
16pub struct Sink(io::Sink);
17
18/// Special version of [io::Empty] that implements Default and Clone traits.
19#[derive(Debug)]
20pub struct Empty(io::Empty);
21
22impl Default for Sink {
23    fn default() -> Self {
24        Sink(io::sink())
25    }
26}
27
28impl Clone for Sink {
29    fn clone(&self) -> Self {
30        Self::default()
31    }
32}
33
34impl io::Write for Sink {
35    #[inline]
36    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
37        self.0.write(buf)
38    }
39
40    #[inline]
41    fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
42        self.0.write_vectored(bufs)
43    }
44
45    #[inline]
46    fn flush(&mut self) -> io::Result<()> {
47        self.0.flush()
48    }
49}
50
51impl io::Read for Empty {
52    #[inline]
53    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
54        self.0.read(buf)
55    }
56}
57
58impl io::BufRead for Empty {
59    #[inline]
60    fn fill_buf(&mut self) -> io::Result<&[u8]> {
61        self.0.fill_buf()
62    }
63    #[inline]
64    fn consume(&mut self, n: usize) {
65        self.0.consume(n)
66    }
67}
68
69impl Default for Empty {
70    fn default() -> Self {
71        Empty(io::empty())
72    }
73}
74
75impl Clone for Empty {
76    fn clone(&self) -> Self {
77        Self::default()
78    }
79}
80
81#[cfg(feature = "snapshot")]
82macro_rules! impl_serde_unit {
83    ($ty:ty, $name:expr) => {
84        impl Serialize for $ty {
85            fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
86                serializer.serialize_unit_struct($name)
87            }
88        }
89
90        impl<'de> Deserialize<'de> for $ty {
91            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
92                where D: Deserializer<'de>
93            {
94                struct UnitVisitor;
95
96                impl<'de> Visitor<'de> for UnitVisitor {
97                    type Value = $ty;
98
99                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
100                        formatter.write_str("unit")
101                    }
102
103                    fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
104                        Ok(<$ty>::default())
105                    }
106                }
107                deserializer.deserialize_unit_struct($name, UnitVisitor)
108            }
109        }
110    };
111}
112
113#[cfg(feature = "snapshot")]
114impl_serde_unit!(Sink, "Sink");
115#[cfg(feature = "snapshot")]
116impl_serde_unit!(Empty, "Empty");