pub struct SampleWriter<W> { /* private fields */ }Expand description
Streams frames of samples into a Write destination.
Each call to write_frames encodes its samples with
encode and writes them immediately; wrap the destination in a
std::io::BufWriter when writing many small blocks to a file.
§Examples
use sigmf::datatype::Datatype;
use sigmf::utils::data::{SampleReader, SampleValue, SampleWriter};
use std::io::Cursor;
use std::str::FromStr;
let datatype = Datatype::from_str("rf32_be")?;
let mut writer = SampleWriter::new(Vec::new(), datatype.clone(), 1)?;
writer.write_frames(&[SampleValue::RealF32(0.5), SampleValue::RealF32(-1.0)])?;
writer.write_frames(&[SampleValue::RealF32(2.0)])?;
let bytes = writer.finish()?;
assert_eq!(bytes.len(), 12);
let mut reader = SampleReader::new(Cursor::new(bytes), datatype, 1)?;
assert_eq!(
reader.read_frames(2)?,
Some(vec![SampleValue::RealF32(0.5), SampleValue::RealF32(-1.0)])
);
assert_eq!(reader.read_frames(2)?, Some(vec![SampleValue::RealF32(2.0)]));
assert_eq!(reader.read_frames(2)?, None);Implementations§
Source§impl<W: Write> SampleWriter<W>
impl<W: Write> SampleWriter<W>
Sourcepub fn new(
writer: W,
datatype: Datatype,
channel_count: usize,
) -> Result<Self, SigmfError>
pub fn new( writer: W, datatype: Datatype, channel_count: usize, ) -> Result<Self, SigmfError>
Creates a writer that encodes channel_count-channel frames of
datatype into writer.
§Errors
Returns SigmfError::InvalidMetadata if channel_count is zero.
Sourcepub fn write_frames(
&mut self,
samples: &[SampleValue],
) -> Result<(), SigmfError>
pub fn write_frames( &mut self, samples: &[SampleValue], ) -> Result<(), SigmfError>
Encodes and writes one block of complete frames.
§Errors
Returns any error from encode (in which case nothing is written),
or SigmfError::Io if writing fails.
Sourcepub fn finish(self) -> Result<W, SigmfError>
pub fn finish(self) -> Result<W, SigmfError>
Auto Trait Implementations§
impl<W> Freeze for SampleWriter<W>where
W: Freeze,
impl<W> RefUnwindSafe for SampleWriter<W>where
W: RefUnwindSafe,
impl<W> Send for SampleWriter<W>where
W: Send,
impl<W> Sync for SampleWriter<W>where
W: Sync,
impl<W> Unpin for SampleWriter<W>where
W: Unpin,
impl<W> UnsafeUnpin for SampleWriter<W>where
W: UnsafeUnpin,
impl<W> UnwindSafe for SampleWriter<W>where
W: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more