Struct spectrusty_formats::tap::TapChunkWriter
source · pub struct TapChunkWriter<W> { /* private fields */ }
Expand description
A tool for writing TAP file chunks to byte streams.
Data can be written in one of 3 ways:
- Writing tap chunk data at once with TapChunkWriter::write_header or TapChunkWriter::write_chunk.
- Writing tap chunk data with multiple writes via TapChunkWriter::begin and then TapChunkWriteTran.
- Writing tap chunks from decoded TAPE pulse iterators with TapChunkWriter::write_pulses_as_tap_chunks.
Implementations§
source§impl<W> TapChunkWriter<W>
impl<W> TapChunkWriter<W>
sourcepub fn into_inner(self) -> PulseDecodeWriter<W>
pub fn into_inner(self) -> PulseDecodeWriter<W>
Returns the underlying pulse decode writer.
sourcepub fn get_mut(&mut self) -> &mut PulseDecodeWriter<W>
pub fn get_mut(&mut self) -> &mut PulseDecodeWriter<W>
Returns a mutable reference to the inner pulse decode writer.
sourcepub fn get_ref(&self) -> &PulseDecodeWriter<W>
pub fn get_ref(&self) -> &PulseDecodeWriter<W>
Returns a shared reference to the inner pulse decode writer.
source§impl<W> TapChunkWriter<W>where
W: Write + Seek,
impl<W> TapChunkWriter<W>where
W: Write + Seek,
sourcepub fn try_new(wr: W) -> Result<Self>
pub fn try_new(wr: W) -> Result<Self>
Returns a new instance of TapChunkWriter
with the given writer on success.
The stream cursor should be positioned where the next chunk will be written.
This method does not write any data, but moves the stream cursor to make room for the next block’s length indicator.
sourcepub fn flush(&mut self) -> Result<()>
pub fn flush(&mut self) -> Result<()>
Flushes the underlying writer, ensuring that all intermediately buffered contents reach their destination (invokes Write::flush).
sourcepub fn end_pulse_chunk(&mut self) -> Result<usize>
pub fn end_pulse_chunk(&mut self) -> Result<usize>
Forces pending pulse decode data transfer to end.
Returns the number of TAP chunks written.
Examples found in repository?
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
pub fn write_chunk<D: AsRef<[u8]>>(&mut self, chunk: D) -> Result<usize> {
let data = chunk.as_ref();
let size = u16::try_from(data.len()).map_err(|_|
Error::new(ErrorKind::InvalidData, "TAP chunk too large."))?;
let nchunks = self.end_pulse_chunk()?;
let wr = self.mpwr.get_mut();
let chunk_head = wr.seek(SeekFrom::Start(self.chunk_head))?;
debug_assert_eq!(chunk_head, self.chunk_head);
wr.write_all(&size.to_le_bytes())?;
wr.write_all(data)?;
let chunk_start = wr.seek(SeekFrom::Current(LEN_PREFIX_SIZE as i64))?;
self.chunk_head = chunk_start.checked_sub(LEN_PREFIX_SIZE).unwrap();
Ok(nchunks + 1)
}
/// Creates a transaction allowing for multiple data writes to the same *TAP* chunk.
///
/// Flushes internal [mic pulse writer][PulseDecodeWriter::end].
///
/// Returns a transaction holder, which can be used to write data to the current chunk.
pub fn begin(&mut self) -> Result<TapChunkWriteTran<'_, W>> {
let nchunks = self.end_pulse_chunk()?;
Ok(TapChunkWriteTran { checksum: 0, nchunks, uncommitted: 0, writer: self })
}
sourcepub fn write_header(&mut self, header: &Header) -> Result<usize>
pub fn write_header(&mut self, header: &Header) -> Result<usize>
Writes a provided header as a TAP chunk.
Flushes internal mic pulse writer before proceeding with writing the header.
Returns the number of TAP chunks written.
Examples found in repository?
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
fn file_to_tap_writer<S: AsRef<[u8]>, W: Write + Seek>(
&self,
file_name: S,
wr: &mut TapChunkWriter<W>
) -> io::Result<bool>
{
let mut sector_iter = self.file_sectors(file_name);
#[allow(clippy::never_loop)]
let mut tran = loop {
if let Some(sector) = sector_iter.next() {
let sector = sector.unwrap();
if let Some(header) = sector.tap_header()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
{
wr.write_header(&header)?;
let mut tran = wr.begin()?;
tran.write_all(slice::from_ref(&DATA_BLOCK_FLAG))?;
tran.write_all(§or.data_record()[HD_SIZE..])?;
break tran
}
}
return Ok(false)
};
for sector in sector_iter {
let sector = sector.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let record = sector.data_record();
tran.write_all(record)?;
}
tran.commit(true)?;
Ok(true)
}
sourcepub fn write_chunk<D: AsRef<[u8]>>(&mut self, chunk: D) -> Result<usize>
pub fn write_chunk<D: AsRef<[u8]>>(&mut self, chunk: D) -> Result<usize>
Writes provided data as a TAP chunk.
Flushes internal mic pulse writer before proceeding with writing the data.
Returns the number of TAP chunks written.
sourcepub fn begin(&mut self) -> Result<TapChunkWriteTran<'_, W>>
pub fn begin(&mut self) -> Result<TapChunkWriteTran<'_, W>>
Creates a transaction allowing for multiple data writes to the same TAP chunk.
Flushes internal mic pulse writer.
Returns a transaction holder, which can be used to write data to the current chunk.
Examples found in repository?
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
fn file_to_tap_writer<S: AsRef<[u8]>, W: Write + Seek>(
&self,
file_name: S,
wr: &mut TapChunkWriter<W>
) -> io::Result<bool>
{
let mut sector_iter = self.file_sectors(file_name);
#[allow(clippy::never_loop)]
let mut tran = loop {
if let Some(sector) = sector_iter.next() {
let sector = sector.unwrap();
if let Some(header) = sector.tap_header()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
{
wr.write_header(&header)?;
let mut tran = wr.begin()?;
tran.write_all(slice::from_ref(&DATA_BLOCK_FLAG))?;
tran.write_all(§or.data_record()[HD_SIZE..])?;
break tran
}
}
return Ok(false)
};
for sector in sector_iter {
let sector = sector.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let record = sector.data_record();
tran.write_all(record)?;
}
tran.commit(true)?;
Ok(true)
}
sourcepub fn write_pulses_as_tap_chunks<I>(&mut self, iter: I) -> Result<usize>where
I: Iterator<Item = NonZeroU32>,
pub fn write_pulses_as_tap_chunks<I>(&mut self, iter: I) -> Result<usize>where
I: Iterator<Item = NonZeroU32>,
Interprets pulse intervals from the provided iterator as bytes and writes them to the underlying writer as TAP chunks.
See PulseDecodeWriter.
Returns the number of TAP chunks written.
Auto Trait Implementations§
impl<W> RefUnwindSafe for TapChunkWriter<W>where
W: RefUnwindSafe,
impl<W> Send for TapChunkWriter<W>where
W: Send,
impl<W> Sync for TapChunkWriter<W>where
W: Sync,
impl<W> Unpin for TapChunkWriter<W>where
W: Unpin,
impl<W> UnwindSafe for TapChunkWriter<W>where
W: UnwindSafe,
Blanket Implementations§
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
source§impl<S, T> IntoSample<S> for Twhere
S: FromSample<T>,
impl<S, T> IntoSample<S> for Twhere
S: FromSample<T>,
source§fn into_sample(self) -> S
fn into_sample(self) -> S
S
a sample type from self
.§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds. Read more§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds. Read more§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds. Read more§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds. Read more§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds. Read more