1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use super::{AppendOnlyResumableRecorderMedium, ReadOnlyResumableRecorderMedium, ResumableRecorder, SourceKey};
use digest::Digest;
use sha1::Sha1;
use std::{
    fmt::{self, Debug},
    io::{Error as IoError, ErrorKind as IoErrorKind, Read, Result as IoResult, Write},
    marker::PhantomData,
};

#[cfg(feature = "async")]
use {
    super::{AppendOnlyAsyncResumableRecorderMedium, ReadOnlyAsyncResumableRecorderMedium},
    futures::future::BoxFuture,
};

/// 无断点恢复记录器
///
/// 实现了断点恢复记录器接口,但总是返回找不到记录
#[derive(Clone, Copy)]
pub struct DummyResumableRecorder<O = Sha1> {
    _unused: PhantomData<O>,
}

impl<O> DummyResumableRecorder<O> {
    /// 创建无断点恢复记录器
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }
}

impl<O> Default for DummyResumableRecorder<O> {
    #[inline]
    fn default() -> Self {
        Self {
            _unused: Default::default(),
        }
    }
}

impl<O: Clone + Digest + Send + Sync + Unpin> ResumableRecorder for DummyResumableRecorder<O> {
    type HashAlgorithm = O;

    #[inline]
    fn open_for_read(
        &self,
        _source_key: &SourceKey<Self::HashAlgorithm>,
    ) -> IoResult<Box<dyn ReadOnlyResumableRecorderMedium>> {
        Err(make_error())
    }

    #[inline]
    fn open_for_append(
        &self,
        _source_key: &SourceKey<Self::HashAlgorithm>,
    ) -> IoResult<Box<dyn AppendOnlyResumableRecorderMedium>> {
        Err(make_error())
    }

    #[inline]
    fn open_for_create_new(
        &self,
        _source_key: &SourceKey<Self::HashAlgorithm>,
    ) -> IoResult<Box<dyn AppendOnlyResumableRecorderMedium>> {
        Err(make_error())
    }

    #[inline]
    fn delete(&self, _source_key: &SourceKey<Self::HashAlgorithm>) -> IoResult<()> {
        Err(make_error())
    }

    #[inline]
    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn open_for_async_read<'a>(
        &'a self,
        _source_key: &'a SourceKey<Self::HashAlgorithm>,
    ) -> BoxFuture<'a, IoResult<Box<dyn ReadOnlyAsyncResumableRecorderMedium>>> {
        Box::pin(async move { Err(make_error()) })
    }

    #[inline]
    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn open_for_async_append<'a>(
        &'a self,
        _source_key: &'a SourceKey<Self::HashAlgorithm>,
    ) -> BoxFuture<'a, IoResult<Box<dyn AppendOnlyAsyncResumableRecorderMedium>>> {
        Box::pin(async move { Err(make_error()) })
    }

    #[inline]
    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn open_for_async_create_new<'a>(
        &'a self,
        _source_key: &'a SourceKey<Self::HashAlgorithm>,
    ) -> BoxFuture<'a, IoResult<Box<dyn AppendOnlyAsyncResumableRecorderMedium>>> {
        Box::pin(async move { Err(make_error()) })
    }

    #[inline]
    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn async_delete<'a>(&'a self, _source_key: &'a SourceKey<Self::HashAlgorithm>) -> BoxFuture<'a, IoResult<()>> {
        Box::pin(async move { Err(make_error()) })
    }
}

impl<O> Debug for DummyResumableRecorder<O> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DummyResumableRecorder").finish()
    }
}

/// 无断点恢复记录介质
///
/// 实现了断点恢复记录介质接口,但总是返回错误
#[derive(Debug, Clone, Copy)]
pub struct DummyResumableRecorderMedium;

impl Read for DummyResumableRecorderMedium {
    #[inline]
    fn read(&mut self, _buf: &mut [u8]) -> IoResult<usize> {
        Err(make_error())
    }
}

impl Write for DummyResumableRecorderMedium {
    #[inline]
    fn write(&mut self, _buf: &[u8]) -> IoResult<usize> {
        Err(make_error())
    }

    #[inline]
    fn flush(&mut self) -> IoResult<()> {
        Err(make_error())
    }
}

#[cfg(feature = "async")]
use {
    futures::{AsyncRead, AsyncWrite},
    std::{
        pin::Pin,
        task::{Context, Poll},
    },
};

#[cfg(feature = "async")]
impl AsyncRead for DummyResumableRecorderMedium {
    #[inline]
    fn poll_read(self: Pin<&mut Self>, _cx: &mut Context<'_>, _buf: &mut [u8]) -> Poll<IoResult<usize>> {
        Poll::Ready(Err(make_error()))
    }
}

#[cfg(feature = "async")]
impl AsyncWrite for DummyResumableRecorderMedium {
    #[inline]
    fn poll_write(self: Pin<&mut Self>, _cx: &mut Context<'_>, _buf: &[u8]) -> Poll<IoResult<usize>> {
        Poll::Ready(Err(make_error()))
    }

    #[inline]
    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<IoResult<()>> {
        Poll::Ready(Err(make_error()))
    }

    #[inline]
    fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<IoResult<()>> {
        Poll::Ready(Err(make_error()))
    }
}

fn make_error() -> IoError {
    IoError::new(IoErrorKind::Unsupported, "Unimplemented")
}