Skip to main content

ConversionError

Enum ConversionError 

Source
#[non_exhaustive]
pub enum ConversionError { AudioConversion { value: String, source_type: String, target_type: String, reason: String, }, NumericCast { value: String, source_type: String, target_type: String, reason: String, }, UnsupportedConversion { source_type: String, target_type: String, }, }
Expand description

Errors that occur during type conversion and casting operations.

§Purpose

Covers both audio-aware conversions (e.g. i16 ↔ f32 with normalisation) and raw numeric casts between sample types. The distinction between AudioConversion and NumericCast reflects whether the operation understood audio semantics.

§Intended Usage

Returned by to_format, to_type, cast_as, and cast_to. Prefer the audio-aware variants (to_format, to_type) over raw casts unless you deliberately need non-normalised bit patterns.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

AudioConversion

Failed to convert between audio sample types with audio-aware scaling.

Fields

§value: String

The sample value that failed to convert (as string for display).

§source_type: String

Source sample type name.

§target_type: String

Target sample type name.

§reason: String

Detailed reason for the conversion failure.

§

NumericCast

Failed to perform raw numeric casting between types.

Fields

§value: String

The numeric value that failed to cast (as string for display).

§source_type: String

Source numeric type name.

§target_type: String

Target numeric type name.

§reason: String

Detailed reason for the casting failure.

§

UnsupportedConversion

The conversion operation is not supported between the specified types.

Fields

§source_type: String

Source type name.

§target_type: String

Target type name.

Implementations§

Source§

impl ConversionError

Source

pub fn audio_conversion<V, S, T, R>( value: V, source_type: S, target_type: T, reason: R, ) -> Self
where V: ToString, S: ToString, T: ToString, R: ToString,

Creates a ConversionError::AudioConversion error.

Use this when an audio-aware type conversion fails — for example, when a normalised float value cannot be represented in the target integer type.

§Arguments
  • value — The sample value that failed to convert (converted to string for display).
  • source_type — Name of the source sample type (e.g. "i16").
  • target_type — Name of the target sample type (e.g. "f32").
  • reason — Human-readable explanation of why the conversion failed.
§Returns

A ConversionError::AudioConversion variant.

§Examples
use audio_samples::{AudioSampleError, ConversionError};

let err = ConversionError::audio_conversion("32768", "i16", "i8", "value out of range");
assert!(matches!(err, ConversionError::AudioConversion { .. }));

// Wraps into the root error type via the From impl.
let audio_err: AudioSampleError = err.into();
assert!(matches!(audio_err, AudioSampleError::Conversion(_)));
Source

pub fn numeric_cast<V, S, T, R>( value: V, source_type: S, target_type: T, reason: R, ) -> Self
where V: ToString, S: ToString, T: ToString, R: ToString,

Creates a ConversionError::NumericCast error.

Use this when a raw numeric cast fails — for example, when casting a large f64 value to u8 would overflow or produce a meaningless bit pattern.

§Arguments
  • value — The numeric value that failed to cast (converted to string for display).
  • source_type — Name of the source numeric type (e.g. "f64").
  • target_type — Name of the target numeric type (e.g. "u8").
  • reason — Human-readable explanation of why the cast failed.
§Returns

A ConversionError::NumericCast variant.

§Examples
use audio_samples::ConversionError;

let err = ConversionError::numeric_cast("300.0", "f64", "u8", "value exceeds 255");
assert!(matches!(err, ConversionError::NumericCast { .. }));

Trait Implementations§

Source§

impl Clone for ConversionError

Source§

fn clone(&self) -> ConversionError

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ConversionError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for ConversionError

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for ConversionError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<ConversionError> for AudioSampleError

Source§

fn from(source: ConversionError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Src, Dst> ConvertTo<Dst> for Src
where Dst: ConvertFrom<Src>,

Source§

fn convert_to(self) -> Dst

Converts this sample into Dst using audio-aware scaling. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.