Skip to main content

Crate const_num_traits

Crate const_num_traits 

Source
Expand description

Numeric traits for generic mathematics

§Constant-time (CT) tiers

Every operation trait in this crate is classified by how implementable it is for constant-time integer types (types whose execution time must not depend on operand values):

  • Tier A — CT-implementable: branchless on the data; secret values flow only through arithmetic/bitwise instructions. Examples: the Wrapping*/Overflowing*/Carrying* families, PrimBits, Midpoint, AbsDiff, CastSigned, lossy Truncate.
  • Tier B — caller-leaky: the operation itself can be implemented branchlessly, but the bool/Option return type invites branching on secret-derived data at the call site. Examples: the Checked* family, Parity, IsPowerOfTwo, HighestOne. With the ct cargo feature, masked counterparts returning subtle::Choice/CtOption are available in ops::ct.
  • Tier C — CT-hostile: data-dependent control flow, division, or data-dependent panics. Examples: everything Div/Rem-based (Euclid, DivCeil, Ilog10, NextMultipleOf), the Strict* family (panics on a data-dependent condition), Num::from_str_radix.

Public-parameter convention: shift amounts, rotation counts, exponents and logarithm bases are treated as public values — branching on them does not demote a trait from Tier A/B. If your shift amount is itself a secret, none of these traits are appropriate as-is.

The tier of each trait family is noted in its module documentation under ops. Rule of thumb maintained by this crate: a Tier-A trait never has a Tier-C supertrait, and never requires PartialEq/Ord/Div/Rem.

§Compatibility

The const-num-traits crate is tested for rustc 1.86 and greater.

Re-exports§

pub use crate::bounds::Bounded;
pub use crate::float::Float;libm or std
pub use crate::float::FloatConst;
pub use crate::cast::AsPrimitive;
pub use crate::cast::FromPrimitive;
pub use crate::cast::NumCast;
pub use crate::cast::ToPrimitive;
pub use crate::cast::cast;
pub use crate::identities::ConstOne;
pub use crate::identities::ConstZero;
pub use crate::identities::One;
pub use crate::identities::Zero;
pub use crate::identities::one;
pub use crate::identities::zero;
pub use crate::int::PrimBits;
pub use crate::int::PrimInt;
pub use crate::ops::bits::BitWidth;
pub use crate::ops::bits::DepositBits;
pub use crate::ops::bits::ExtractBits;
pub use crate::ops::bits::FunnelShl;
pub use crate::ops::bits::FunnelShr;
pub use crate::ops::bits::HighestOne;
pub use crate::ops::bits::IsolateHighestOne;
pub use crate::ops::bits::IsolateLowestOne;
pub use crate::ops::bits::LowestOne;
pub use crate::ops::bits::ShlExact;
pub use crate::ops::bits::ShrExact;
pub use crate::ops::bits::UnboundedShl;
pub use crate::ops::bits::UnboundedShr;
pub use crate::ops::bytes::FromBytes;
pub use crate::ops::bytes::ToBytes;
pub use crate::ops::carrying::BorrowingSub;
pub use crate::ops::carrying::CarryingAdd;
pub use crate::ops::carrying::CarryingMul;
pub use crate::ops::carrying::WideningMul;
pub use crate::ops::checked::CheckedAbs;
pub use crate::ops::checked::CheckedAdd;
pub use crate::ops::checked::CheckedDiv;
pub use crate::ops::checked::CheckedMul;
pub use crate::ops::checked::CheckedNeg;
pub use crate::ops::checked::CheckedPow;
pub use crate::ops::checked::CheckedRem;
pub use crate::ops::checked::CheckedShl;
pub use crate::ops::checked::CheckedShr;
pub use crate::ops::checked::CheckedSub;
pub use crate::ops::clmul::CarryingCarrylessMul;
pub use crate::ops::clmul::CarrylessMul;
pub use crate::ops::clmul::WideningCarrylessMul;
pub use crate::ops::convert::AbsDiff;
pub use crate::ops::convert::CastSigned;
pub use crate::ops::convert::CastUnsigned;
pub use crate::ops::convert::CheckedCast;
pub use crate::ops::convert::ClampMagnitude;
pub use crate::ops::convert::SaturatingCast;
pub use crate::ops::convert::StrictCast;
pub use crate::ops::convert::Truncate;
pub use crate::ops::convert::UnsignedAbs;
pub use crate::ops::convert::Widen;
pub use crate::ops::convert::WrappingCast;
pub use crate::ops::ct::CtCheckedAdd;ct
pub use crate::ops::ct::CtCheckedMul;ct
pub use crate::ops::ct::CtCheckedNeg;ct
pub use crate::ops::ct::CtCheckedSignedDiff;ct
pub use crate::ops::ct::CtCheckedSub;ct
pub use crate::ops::ct::CtIsPowerOfTwo;ct
pub use crate::ops::ct::CtIsZero;ct
pub use crate::ops::ct::CtParity;ct
pub use crate::ops::euclid::CheckedEuclid;
pub use crate::ops::euclid::Euclid;
pub use crate::ops::euclid::OverflowingEuclid;
pub use crate::ops::euclid::WrappingEuclid;
pub use crate::ops::float_ops::Algebraic;
pub use crate::ops::float_ops::Erf;
pub use crate::ops::float_ops::FloatBits;
pub use crate::ops::float_ops::Gamma;
pub use crate::ops::float_ops::Maximum;
pub use crate::ops::float_ops::Minimum;
pub use crate::ops::float_ops::NextDown;
pub use crate::ops::float_ops::NextUp;
pub use crate::ops::float_ops::RoundTiesEven;
pub use crate::ops::from_ascii::AsciiErrorKind;
pub use crate::ops::from_ascii::AsciiParseError;
pub use crate::ops::from_ascii::FromAscii;
pub use crate::ops::inv::Inv;
pub use crate::ops::log::Ilog;
pub use crate::ops::log::Ilog2;
pub use crate::ops::log::Ilog10;
pub use crate::ops::mixed::CheckedAddSigned;
pub use crate::ops::mixed::CheckedAddUnsigned;
pub use crate::ops::mixed::CheckedSignedDiff;
pub use crate::ops::mixed::CheckedSubSigned;
pub use crate::ops::mixed::CheckedSubUnsigned;
pub use crate::ops::mixed::OverflowingAddSigned;
pub use crate::ops::mixed::OverflowingAddUnsigned;
pub use crate::ops::mixed::OverflowingSubSigned;
pub use crate::ops::mixed::OverflowingSubUnsigned;
pub use crate::ops::mixed::SaturatingAddSigned;
pub use crate::ops::mixed::SaturatingAddUnsigned;
pub use crate::ops::mixed::SaturatingSubSigned;
pub use crate::ops::mixed::SaturatingSubUnsigned;
pub use crate::ops::mixed::StrictAddSigned;
pub use crate::ops::mixed::StrictAddUnsigned;
pub use crate::ops::mixed::StrictSubSigned;
pub use crate::ops::mixed::StrictSubUnsigned;
pub use crate::ops::mixed::WrappingAddSigned;
pub use crate::ops::mixed::WrappingAddUnsigned;
pub use crate::ops::mixed::WrappingSubSigned;
pub use crate::ops::mixed::WrappingSubUnsigned;
pub use crate::ops::mul_add::MulAdd;
pub use crate::ops::mul_add::MulAddAssign;
pub use crate::ops::overflowing::OverflowingAbs;
pub use crate::ops::overflowing::OverflowingAdd;
pub use crate::ops::overflowing::OverflowingDiv;
pub use crate::ops::overflowing::OverflowingMul;
pub use crate::ops::overflowing::OverflowingNeg;
pub use crate::ops::overflowing::OverflowingPow;
pub use crate::ops::overflowing::OverflowingRem;
pub use crate::ops::overflowing::OverflowingShl;
pub use crate::ops::overflowing::OverflowingShr;
pub use crate::ops::overflowing::OverflowingSub;
pub use crate::ops::parity::Parity;
pub use crate::ops::pow2::IsPowerOfTwo;
pub use crate::ops::pow2::NextPowerOfTwo;
pub use crate::ops::rounding::DivCeil;
pub use crate::ops::rounding::DivExact;
pub use crate::ops::rounding::DivFloor;
pub use crate::ops::rounding::Midpoint;
pub use crate::ops::rounding::MultipleOf;
pub use crate::ops::rounding::NextMultipleOf;
pub use crate::ops::saturating::Saturating;
pub use crate::ops::saturating::SaturatingAbs;
pub use crate::ops::saturating::SaturatingAdd;
pub use crate::ops::saturating::SaturatingDiv;
pub use crate::ops::saturating::SaturatingMul;
pub use crate::ops::saturating::SaturatingNeg;
pub use crate::ops::saturating::SaturatingPow;
pub use crate::ops::saturating::SaturatingSub;
pub use crate::ops::sqrt::CheckedIsqrt;
pub use crate::ops::sqrt::Isqrt;
pub use crate::ops::strict::StrictAbs;
pub use crate::ops::strict::StrictAdd;
pub use crate::ops::strict::StrictDiv;
pub use crate::ops::strict::StrictEuclid;
pub use crate::ops::strict::StrictMul;
pub use crate::ops::strict::StrictNeg;
pub use crate::ops::strict::StrictPow;
pub use crate::ops::strict::StrictRem;
pub use crate::ops::strict::StrictShl;
pub use crate::ops::strict::StrictShr;
pub use crate::ops::strict::StrictSub;
pub use crate::ops::typestate::CtNonZero;ct
pub use crate::ops::typestate::BitIndex;
pub use crate::ops::typestate::BitIndexOps;
pub use crate::ops::typestate::DivNonZero;
pub use crate::ops::typestate::Even;
pub use crate::ops::typestate::Finite;
pub use crate::ops::typestate::HasNonZero;
pub use crate::ops::typestate::NonMin;
pub use crate::ops::typestate::NonNegative;
pub use crate::ops::typestate::Odd;
pub use crate::ops::typestate::Positive;
pub use crate::ops::typestate::PowerOfTwo;
pub use crate::ops::typestate::PowerOfTwoOps;
pub use crate::ops::typestate::TypestateError;
pub use crate::ops::wrapping::WrappingAbs;
pub use crate::ops::wrapping::WrappingAdd;
pub use crate::ops::wrapping::WrappingDiv;
pub use crate::ops::wrapping::WrappingMul;
pub use crate::ops::wrapping::WrappingNeg;
pub use crate::ops::wrapping::WrappingPow;
pub use crate::ops::wrapping::WrappingRem;
pub use crate::ops::wrapping::WrappingShl;
pub use crate::ops::wrapping::WrappingShr;
pub use crate::ops::wrapping::WrappingSub;
pub use crate::personality::Ct;
pub use crate::personality::HasPersonality;
pub use crate::personality::Nct;
pub use crate::personality::Personality;
pub use crate::personality::PersonalityMarker;
pub use crate::personality::PersonalityTag;
pub use crate::pow::Pow;
pub use crate::pow::checked_pow;
pub use crate::pow::pow;
pub use crate::sign::Signed;
pub use crate::sign::Signum;
pub use crate::sign::Unsigned;
pub use crate::sign::abs;
pub use crate::sign::abs_sub;
pub use crate::sign::signum;

Modules§

bounds
cast
float
identities
int
ops
personality
Personality typestate: a zero-size marker that selects which implementation of an operation is picked at monomorphization.
pow
prelude
One-stop trait import: use const_num_traits::prelude::*;
reallibm or std
sign

Macros§

impl_from_primitive_minimal
Implements FromPrimitive from just a from_i64 and a from_u64 conversion, deriving the other twelve methods the way the original num-traits default methods did.
impl_to_primitive_minimal
Implements ToPrimitive from just a to_i64 and a to_u64 conversion, deriving the other twelve methods the way the original num-traits default methods did.

Structs§

ParseFloatError

Enums§

FloatErrorKind

Traits§

FromStrRadix
Conversion from a string in a given radix.
Num
The base trait for numeric types, covering 0 and 1 values, comparisons, basic numeric operations, and string conversion.
NumAssign
The trait for Num types which also implement assignment operators.
NumAssignOps
Generic trait for types implementing numeric assignment operators (like +=).
NumAssignRef
The trait for NumAssign types which also implement assignment operations taking the second operand by reference.
NumOps
Generic trait for types implementing basic numeric operations
NumRef
The trait for Num types which also implement numeric operations taking the second operand by reference.
RefNum
The trait for Num references which implement numeric operations, taking the second operand either by value or by reference.
RingOps
Generic trait for types implementing the division-free basic numeric operations: addition, subtraction and multiplication.

Functions§

clamp
A value bounded by a minimum and a maximum
clamp_max
A value bounded by a maximum value
clamp_min
A value bounded by a minimum value