blob: 9eb3aaa368ef85820483a1835d4fa098acd5295c (
plain) (
blame)
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
|
use futures_lite::io::AsyncWriteExt as _;
/// Writes ttyrec frames to a [`futures_lite::io::AsyncWrite`] instance.
pub struct Writer<T: futures_lite::io::AsyncWrite> {
output: T,
creator: crate::Creator,
}
impl<T: futures_lite::io::AsyncWrite + std::marker::Unpin + Send> Writer<T> {
/// Creates a new [`Writer`](Self) from a [`futures_lite::io::AsyncWrite`]
/// instance.
pub fn new(output: T) -> Self {
Self {
output,
creator: crate::Creator::new(),
}
}
/// Writes a new frame to the output stream, using the current time and
/// given data.
///
/// # Errors
/// * [`Error::Write`](crate::Error::Write): There was an error writing to
/// the input stream.
pub async fn frame(&mut self, data: &[u8]) -> crate::Result<()> {
self.frame_at(std::time::Instant::now(), data).await
}
/// Writes a new frame to the output stream, using the given time and
/// data.
///
/// # Errors
/// * [`Error::Write`](crate::Error::Write): There was an error writing to
/// the input stream.
pub async fn frame_at(
&mut self,
cur_time: std::time::Instant,
data: &[u8],
) -> crate::Result<()> {
let frame = self.creator.frame_at(cur_time, data);
let bytes: Vec<u8> = frame.try_into()?;
self.output
.write_all(&bytes)
.await
.map_err(|source| crate::Error::Write { source })
}
}
|