Skip to main content

ReadSamples

Trait ReadSamples 

Source
pub trait ReadSamples: Read {
    // Provided methods
    fn read_sample<T: BytesSample>(&mut self) -> Result<T> { ... }
    fn read_number<T: BytesSample>(&mut self) -> Result<T::NumericType> { ... }
    fn read_converted<T: RawSample + BytesSample, U: FloatCore + ToPrimitive>(
        &mut self,
    ) -> Result<U> { ... }
    fn read_samples_exact<T: BytesSample>(
        &mut self,
        buf: &mut [T],
    ) -> Result<()> { ... }
    fn read_numbers_exact<T: BytesSample>(
        &mut self,
        buf: &mut [T::NumericType],
    ) -> Result<()> { ... }
    fn read_converted_exact<T: RawSample + BytesSample, U: FloatCore + ToPrimitive>(
        &mut self,
        buf: &mut [U],
    ) -> Result<()> { ... }
    fn read_samples_to_limit_or_end<T: BytesSample>(
        &mut self,
        buf: &mut Vec<T>,
        limit: Option<usize>,
    ) -> Result<usize> { ... }
    fn read_numbers_to_limit_or_end<T: BytesSample>(
        &mut self,
        buf: &mut Vec<T::NumericType>,
        limit: Option<usize>,
    ) -> Result<usize> { ... }
    fn read_converted_to_limit_or_end<T: RawSample + BytesSample, U: FloatCore + ToPrimitive>(
        &mut self,
        buf: &mut Vec<U>,
        limit: Option<usize>,
    ) -> Result<usize> { ... }
}
Expand description

A trait that extends std::io::Read with methods for reading samples directly.

Provided Methods§

Source

fn read_sample<T: BytesSample>(&mut self) -> Result<T>

Read a single sample from the underlying reader.

This method reads a chunk of bytes from the underlying reader, and interprets it as a sample of type T.

§Type Parameters
  • T: A type implementing the BytesSample trait, defining the format of the sample to read.
§Returns
  • io::Result<T>: The read sample, or an error if reading failed.
§Errors

This function will return an error if:

  • The underlying reader returns an error.
  • The number of bytes read is not sufficient to represent a complete sample.
Source

fn read_number<T: BytesSample>(&mut self) -> Result<T::NumericType>

Read a single sample and return it as a numeric type.

This method reads a chunk of bytes from the underlying reader, interprets it as a sample of type T, and returns the sample converted to its associated NumericType.

§Type Parameters
  • T: A type implementing the BytesSample trait, defining the format of the sample to read.
§Returns
  • io::Result<T::NumericType>: The sample as a number, or an error if reading failed.
§Errors

This function will return an error if:

  • The underlying reader returns an error.
  • The number of bytes read is not sufficient to represent a complete sample.
Source

fn read_converted<T: RawSample + BytesSample, U: FloatCore + ToPrimitive>( &mut self, ) -> Result<U>

Read a single sample and convert it to a floating-point number.

This method reads a chunk of bytes from the underlying reader, interprets it as a sample of type T, and returns the sample converted to a floating-point number of type U. The conversion uses the to_scaled_float method from the RawSample trait, ensuring that the float is scaled between -1.0 and 1.0.

§Type Parameters
  • T: A type implementing both RawSample and BytesSample, defining the format of the sample to read.
  • U: A floating-point type implementing FloatCore and ToPrimitive, representing the desired output format.
§Returns
  • io::Result<U>: The converted sample as a float, or an error if reading failed.
§Errors

This function will return an error if:

  • The underlying reader returns an error.
  • The number of bytes read is not sufficient to represent a complete sample.
Source

fn read_samples_exact<T: BytesSample>(&mut self, buf: &mut [T]) -> Result<()>

Read multiple samples into a slice.

This method attempts to read enough bytes from the underlying reader to fill the buffer buf with samples of type T. It reads samples one at a time, populating the buffer in order.

§Type Parameters
  • T: A type implementing the BytesSample trait, defining the format of the samples to read.
§Arguments
  • buf: A mutable slice where the samples will be stored.
§Errors

This function will return an error if:

  • The underlying reader returns an error.
  • The number of bytes read is not sufficient to represent a complete sample.
  • The end of the reader is reached before all samples have been read.
Source

fn read_numbers_exact<T: BytesSample>( &mut self, buf: &mut [T::NumericType], ) -> Result<()>

Read multiple samples and store them as numeric types in a provided buffer.

This method reads a sequence of samples from the underlying reader and stores them in the provided buffer buf. Each sample is read and interpreted as a type T, then converted to its associated NumericType before being stored in the buffer.

§Type Parameters
  • T: A type implementing the BytesSample trait, defining the format of the samples to read.
§Arguments
  • buf: A mutable slice where the samples will be stored. The length of the slice determines how many samples are read.
§Returns
  • io::Result<()>: Ok(()) if all samples were read successfully.
§Errors

This function will return an error if:

  • The underlying reader returns an error.
  • The number of bytes read is not sufficient to represent a complete sample.
  • The end of the reader is reached before all samples have been read.
Source

fn read_converted_exact<T: RawSample + BytesSample, U: FloatCore + ToPrimitive>( &mut self, buf: &mut [U], ) -> Result<()>

Read multiple samples, convert them to floats, and store them in a provided buffer.

This method reads a sequence of samples from the underlying reader, converts each sample to a floating-point number of type U, and stores the results in the provided buffer buf.

§Type Parameters
  • T: A type implementing both RawSample and BytesSample, defining the format of the samples to read.
  • U: A floating-point type implementing FloatCore and ToPrimitive, representing the desired output format.
§Arguments
  • buf: A mutable slice where the converted samples will be stored. The length of the slice determines how many samples are read.
§Returns
  • io::Result<()>: Ok(()) if all samples were read and converted successfully.
§Errors

This function will return an error if:

  • The underlying reader returns an error.
  • The number of bytes read is not sufficient to represent a complete sample.
  • The end of the reader is reached before all samples have been read.
Source

fn read_samples_to_limit_or_end<T: BytesSample>( &mut self, buf: &mut Vec<T>, limit: Option<usize>, ) -> Result<usize>

Read samples until the end of the stream, storing them in a vector.

This method reads samples from the underlying reader until reaching the end of the stream, the optional limit, or encountering an error. Each sample is read and interpreted as a type T before being appended to the provided vector buf.

Only complete samples are read. If the last bytes at the end of the stream are too few to make up a complete sample, then they are ignored.

§Type Parameters
  • T: A type implementing the BytesSample trait, defining the format of the samples to read.
§Arguments
  • buf: A mutable vector where the samples will be appended.
  • limit: An optional limit for how many samples to read.
§Returns

The number of samples read.

§Errors
  • The underlying reader returns an error (except for EOF).
Source

fn read_numbers_to_limit_or_end<T: BytesSample>( &mut self, buf: &mut Vec<T::NumericType>, limit: Option<usize>, ) -> Result<usize>

Read samples until the end of the stream, storing them as numeric types in a vector.

This method reads samples from the underlying reader until reaching the end of the stream, the optional limit, or encountering an error. Each sample is read and interpreted as a type T, then converted to its associated NumericType before being appended to the provided vector buf.

Only complete samples are read. If the last bytes at the end of the stream are too few to make up a complete sample, then they are ignored.

§Type Parameters
  • T: A type implementing the BytesSample trait, defining the format of the samples to read.
§Arguments
  • buf: A mutable vector where the samples will be appended.
  • limit: An optional limit for how many samples to read.
§Returns
  • io::Result<usize>: The number of samples read, or an error if reading failed.
§Errors

This function will return an error if:

  • The underlying reader returns an error (except for EOF).
Source

fn read_converted_to_limit_or_end<T: RawSample + BytesSample, U: FloatCore + ToPrimitive>( &mut self, buf: &mut Vec<U>, limit: Option<usize>, ) -> Result<usize>

Read samples until the end of the stream, converting them to floats, and store in a vector.

This method reads samples from the underlying reader until reaching the end of the stream, the optional limit, or encountering an error. Each sample is read, converted to a floating-point number of type U, and appended to the provided vector buf.

Only complete samples are read. If the last bytes at the end of the stream are too few to make up a complete sample, then they are ignored.

§Type Parameters
  • T: A type implementing both RawSample and BytesSample, defining the format of the samples to read.
  • U: A floating-point type implementing FloatCore and ToPrimitive, representing the desired output format.
§Arguments
  • buf: A mutable vector where the converted samples will be appended.
  • limit: An optional limit for how many samples to read.
§Returns
  • io::Result<usize>: The number of samples read and converted, or an error if reading failed.
§Errors

This function will return an error if:

  • The underlying reader returns an error (except for EOF).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<R: Read + ?Sized> ReadSamples for R