fn closest_prefix_for<C: AsRef<Constraint>>(exponent: i32, constraint: C) -> Prefix {
        use std::convert::TryFrom;

        match constraint.as_ref() {
            Constraint::None => {
                Prefix::try_from(exponent.clamp(Prefix::Yocto as i32, Prefix::Yotta as i32))
                    .unwrap_or(Prefix::Unit)
            }
            Constraint::UnitOnly => Prefix::Unit,
            Constraint::UnitAndAbove => {
                Prefix::try_from(exponent.clamp(Prefix::Unit as i32, Prefix::Yotta as i32))
                    .unwrap_or(Prefix::Unit)
            }
            Constraint::UnitAndBelow => {
                Prefix::try_from(exponent.clamp(Prefix::Yocto as i32, Prefix::Unit as i32))
                    .unwrap_or(Prefix::Unit)
            }
            Constraint::Custom(allowed_prefixes) => {
                if allowed_prefixes.is_empty() {
                    panic!("At least one prefix should be allowed");
                }
                let smallest_prefix = *allowed_prefixes.first().unwrap();
                if exponent < smallest_prefix as i32 {
                    return smallest_prefix;
                }
                allowed_prefixes
                    .iter()
                    .take_while(|&&prefix| prefix as i32 <= exponent)
                    .cloned()
                    .last()
                    .unwrap_or(Prefix::Unit)
            }
        }
    }
/// Returns the closest prefix for the provided exponent, respecting the
/// optional constraint.
///
/// # Panics
///
/// - If the `Custom` allowed prefixes is an empty vector.
///
Returns the closest prefix for the provided exponent, respecting the
optional constraint.
Returns the closest prefix for the provided exponent respecting the optional constraint .
fn closest_prefix_for<C: AsRef<Constraint>>(exponent: i32, constraint: C) -> Prefix {
        use std::convert::TryFrom;
        match constraint.as_ref() {
            Constraint::None => {
                Prefix::try_from(exponent.clamp(Prefix::Yocto as i32, Prefix::Yotta as i32))
                    .unwrap_or(Prefix::Unit)
            }
            Constraint::UnitOnly => Prefix::Unit,
            Constraint::UnitAndAbove => {
                Prefix::try_from(exponent.clamp(Prefix::Unit as i32, Prefix::Yotta as i32))
                    .unwrap_or(Prefix::Unit)
            }
            Constraint::UnitAndBelow => {
                Prefix::try_from(exponent.clamp(Prefix::Yocto as i32, Prefix::Unit as i32))
                    .unwrap_or(Prefix::Unit)
            }
            Constraint::Custom(allowed_prefixes) => {
                if allowed_prefixes.is_empty() {
                    panic!("At least one prefix should be allowed");
                }
                let smallest_prefix = *allowed_prefixes.first().unwrap();
                if exponent < smallest_prefix as i32 {
                    return smallest_prefix;
                }
                allowed_prefixes
                    .iter()
                    .take_while(|&&prefix| prefix as i32 <= exponent)
                    .cloned()
                    .last()
                    .unwrap_or(Prefix::Unit)
            }
        }
    }
fn closest_prefix_for < C : AsRef < Constraint > > ( exponent : i32 , constraint : C ) -> Prefix { use std :: convert :: TryFrom ; match constraint . as_ref ( ) { Constraint :: None => { Prefix :: try_from ( exponent . clamp ( Prefix :: Yocto as i32 , Prefix :: Yotta as i32 ) ) . unwrap_or ( Prefix :: Unit ) } Constraint :: UnitOnly => Prefix :: Unit , Constraint :: UnitAndAbove => { Prefix :: try_from ( exponent . clamp ( Prefix :: Unit as i32 , Prefix :: Yotta as i32 ) ) . unwrap_or ( Prefix :: Unit ) } Constraint :: UnitAndBelow => { Prefix :: try_from ( exponent . clamp ( Prefix :: Yocto as i32 , Prefix :: Unit as i32 ) ) . unwrap_or ( Prefix :: Unit ) } Constraint :: Custom ( allowed_prefixes ) => { if allowed_prefixes . is_empty ( ) { panic ! ( " " ) ; } let smallest_prefix = * allowed_prefixes . first ( ) . unwrap ( ) ; if exponent < smallest_prefix as i32 { return smallest_prefix ; } allowed_prefixes . iter ( ) . take_while ( | & & prefix | prefix as i32 <= exponent ) . cloned ( ) . last ( ) . unwrap_or ( Prefix :: Unit ) } } }
Returns the closest prefix for the provided exponent, respecting the
optional constraint.
Returns the closest prefix for the provided exponent respecting the optional constraint .
exponent
i32
constraint
C
panics
If the `Custom` allowed prefixes is an empty vector.
If the ` Custom ` allowed prefixes is an empty vector .