pub trait TryTranslate<T>: Sized {
    type Err;

    // Required method
    fn try_translate(value: T) -> Result<Self, Self::Err>;
}
Expand description

Trait for performing checked translations, where it’s checked if a translation would result in loss of precision.

This will fail if we try to perform a translation between two integer types which are not exactly equivalent.

Examples

use audio::translate::{TryTranslate, IntTranslationError};

assert_eq!(i16::try_translate(-1.0f32), Ok(i16::MIN));
assert_eq!(i16::try_translate(i32::MIN), Ok(i16::MIN));
assert_eq!(i16::try_translate(0x70000000i32), Ok(0x7000i16));
assert!(matches!(i16::try_translate(0x70000001i32), Err(IntTranslationError { .. })));

Required Associated Types§

source

type Err

Error kind raised from the translation.

Required Methods§

source

fn try_translate(value: T) -> Result<Self, Self::Err>

Perform a conversion.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl TryTranslate<i16> for i8

§

type Err = IntTranslationError

source§

fn try_translate(value: i16) -> Result<Self, Self::Err>

source§

impl TryTranslate<i16> for u8

§

type Err = IntTranslationError

source§

fn try_translate(value: i16) -> Result<Self, Self::Err>

source§

impl TryTranslate<i32> for i8

§

type Err = IntTranslationError

source§

fn try_translate(value: i32) -> Result<Self, Self::Err>

source§

impl TryTranslate<i32> for i16

§

type Err = IntTranslationError

source§

fn try_translate(value: i32) -> Result<Self, Self::Err>

source§

impl TryTranslate<i32> for u8

§

type Err = IntTranslationError

source§

fn try_translate(value: i32) -> Result<Self, Self::Err>

source§

impl TryTranslate<i32> for u16

§

type Err = IntTranslationError

source§

fn try_translate(value: i32) -> Result<Self, Self::Err>

source§

impl TryTranslate<i64> for i8

§

type Err = IntTranslationError

source§

fn try_translate(value: i64) -> Result<Self, Self::Err>

source§

impl TryTranslate<i64> for i16

§

type Err = IntTranslationError

source§

fn try_translate(value: i64) -> Result<Self, Self::Err>

source§

impl TryTranslate<i64> for i32

§

type Err = IntTranslationError

source§

fn try_translate(value: i64) -> Result<Self, Self::Err>

source§

impl TryTranslate<i64> for u8

§

type Err = IntTranslationError

source§

fn try_translate(value: i64) -> Result<Self, Self::Err>

source§

impl TryTranslate<i64> for u16

§

type Err = IntTranslationError

source§

fn try_translate(value: i64) -> Result<Self, Self::Err>

source§

impl TryTranslate<i64> for u32

§

type Err = IntTranslationError

source§

fn try_translate(value: i64) -> Result<Self, Self::Err>

source§

impl TryTranslate<u16> for i8

§

type Err = IntTranslationError

source§

fn try_translate(value: u16) -> Result<Self, Self::Err>

source§

impl TryTranslate<u16> for u8

§

type Err = IntTranslationError

source§

fn try_translate(value: u16) -> Result<Self, Self::Err>

source§

impl TryTranslate<u32> for i8

§

type Err = IntTranslationError

source§

fn try_translate(value: u32) -> Result<Self, Self::Err>

source§

impl TryTranslate<u32> for i16

§

type Err = IntTranslationError

source§

fn try_translate(value: u32) -> Result<Self, Self::Err>

source§

impl TryTranslate<u32> for u8

§

type Err = IntTranslationError

source§

fn try_translate(value: u32) -> Result<Self, Self::Err>

source§

impl TryTranslate<u32> for u16

§

type Err = IntTranslationError

source§

fn try_translate(value: u32) -> Result<Self, Self::Err>

source§

impl TryTranslate<u64> for i8

§

type Err = IntTranslationError

source§

fn try_translate(value: u64) -> Result<Self, Self::Err>

source§

impl TryTranslate<u64> for i16

§

type Err = IntTranslationError

source§

fn try_translate(value: u64) -> Result<Self, Self::Err>

source§

impl TryTranslate<u64> for i32

§

type Err = IntTranslationError

source§

fn try_translate(value: u64) -> Result<Self, Self::Err>

source§

impl TryTranslate<u64> for u8

§

type Err = IntTranslationError

source§

fn try_translate(value: u64) -> Result<Self, Self::Err>

source§

impl TryTranslate<u64> for u16

§

type Err = IntTranslationError

source§

fn try_translate(value: u64) -> Result<Self, Self::Err>

source§

impl TryTranslate<u64> for u32

§

type Err = IntTranslationError

source§

fn try_translate(value: u64) -> Result<Self, Self::Err>

Implementors§

source§

impl<T, U> TryTranslate<T> for U
where U: Translate<T>,