Skip to main content

WithPrecision

Trait WithPrecision 

Source
pub trait WithPrecision: BitsPrecision {
    // Required method
    fn widen_to_precision(self, bits_precision: u32) -> Self;

    // Provided methods
    fn zero_with_precision(bits_precision: u32) -> Self
       where Self: Zero { ... }
    fn one_with_precision(bits_precision: u32) -> Self
       where Self: One { ... }
    fn widen_to_precision_of(self, witness: &Self) -> Self { ... }
    fn zero_with_precision_of(witness: &Self) -> Self
       where Self: Zero { ... }
    fn one_with_precision_of(witness: &Self) -> Self
       where Self: One { ... }
}
Expand description

Establishes a value’s operating width from a witness — the constructive companion to BitsPrecision. Method names mirror crypto-bigint’s BoxedUint::{zero_with_precision, one_with_precision, widen}.

Why it exists: Zero::zero/One::one are minimal-width on a runtime-width carrier, so a reducer seeded with zero() and grown toward a modulus width operates at the seed width and wraps early — correct on a fixed-width type, silently truncated on a variable-width one. T::zero_with_precision_of(&m) seeds at the modulus width instead.

widen_to_precision grows, never shrinks, and preserves the value (identity on a fixed-width carrier). It preserves value only to a representation-compatible width — a fixed-point carrier’s fractional format must match — which is why the _of forms take a witness value, not a bit count. Those forms borrow the witness, so they carry no Copy bound and serve Clone-only carriers, the case the family exists for.

use const_num_traits::WithPrecision;

// Fixed-width: width is the type; the requested precision is ignored.
assert_eq!(WithPrecision::widen_to_precision(5u32, 256), 5u32);
assert_eq!(WithPrecision::zero_with_precision_of(&99u32), 0u32);
let one: u32 = WithPrecision::one_with_precision(128);
assert_eq!(one, 1);

Required Methods§

Source

fn widen_to_precision(self, bits_precision: u32) -> Self

Returns self re-represented over a width of at least bits_precision, preserving its numeric value. Never shrinks; on a fixed-width carrier the width is the type and this is the identity.

Provided Methods§

Source

fn zero_with_precision(bits_precision: u32) -> Self
where Self: Zero,

A Zero carried at a width of at least bits_precision.

Source

fn one_with_precision(bits_precision: u32) -> Self
where Self: One,

A One carried at a width of at least bits_precision.

Source

fn widen_to_precision_of(self, witness: &Self) -> Self

widen_to_precision to the width of a witness value — typically the modulus a reducer operates over. Prefer this to hand-picking a bit count: the witness carries the intended width.

Source

fn zero_with_precision_of(witness: &Self) -> Self
where Self: Zero,

A Zero carried at the witness’s width — the width-safe seed for a generic accumulator.

Source

fn one_with_precision_of(witness: &Self) -> Self
where Self: One,

A One carried at the witness’s width.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl WithPrecision for i8

Source§

fn widen_to_precision(self, _bits_precision: u32) -> i8

Source§

impl WithPrecision for i16

Source§

fn widen_to_precision(self, _bits_precision: u32) -> i16

Source§

impl WithPrecision for i32

Source§

fn widen_to_precision(self, _bits_precision: u32) -> i32

Source§

impl WithPrecision for i64

Source§

fn widen_to_precision(self, _bits_precision: u32) -> i64

Source§

impl WithPrecision for i128

Source§

fn widen_to_precision(self, _bits_precision: u32) -> i128

Source§

impl WithPrecision for isize

Source§

fn widen_to_precision(self, _bits_precision: u32) -> isize

Source§

impl WithPrecision for u8

Source§

fn widen_to_precision(self, _bits_precision: u32) -> u8

Source§

impl WithPrecision for u16

Source§

fn widen_to_precision(self, _bits_precision: u32) -> u16

Source§

impl WithPrecision for u32

Source§

fn widen_to_precision(self, _bits_precision: u32) -> u32

Source§

impl WithPrecision for u64

Source§

fn widen_to_precision(self, _bits_precision: u32) -> u64

Source§

impl WithPrecision for u128

Source§

fn widen_to_precision(self, _bits_precision: u32) -> u128

Source§

impl WithPrecision for usize

Source§

fn widen_to_precision(self, _bits_precision: u32) -> usize

Implementors§