Skip to main content

Word

Struct Word 

Source
pub struct Word(/* private fields */);
Expand description

A word, 27 balanced trits.

Stores the numeric value in the host integer; see the module docs. Every value of this type is in range by construction, so value() never returns something out of bounds.

Implementations§

Source§

impl Word

Source

pub const TRITS: u32 = 27

Number of trits in this type.

Source

pub const MODULUS: i128

3^TRITS, the number of distinct values.

Source

pub const MAX: Self

The largest representable value, (3^n-1)/2.

Source

pub const MIN: Self

The smallest representable value, -(3^n-1)/2.

The range is symmetric, so there is no value whose negation overflows (D-01).

Source

pub const ZERO: Self

Zero.

Source

pub const ONE: Self

One.

Source

pub const NEG_ONE: Self

Negative one.

Source

pub const fn in_range(v: i64) -> bool

True when v is representable in this many trits.

Source

pub const fn new(v: i64) -> Option<Self>

Wraps a host value, or None if out of range.

Source

pub const fn from_value(v: i64) -> Self

Wraps a host value, panicking if out of range.

For constants and tests where the range is known statically.

Source

pub const fn from_i128_wrapping(v: i128) -> Self

Reduces an i128 into range modulo 3^n. Always succeeds.

Source

pub const fn from_i128(v: i128) -> Option<Self>

Reduces an i128 into range, or None if it does not fit.

Source

pub const fn value(self) -> i64

The numeric value.

Source

pub const fn to_i128(self) -> i128

The numeric value widened to i128.

Source

pub const fn trits(self) -> [Trit; 27]

The trits, least significant first.

Source

pub const fn from_trits(trits: [Trit; 27]) -> Self

Rebuilds a value from trits, least significant first.

Source

pub const fn trit(self, index: u32) -> Trit

The trit at index, counting from the least significant.

§Panics

If index >= TRITS.

Source

pub const fn to_bct(self) -> u64

Packs into BCT: two bits per trit, least significant trit in the low bits.

Source

pub const fn from_bct(bits: u64) -> Result<Self, NatError>

Unpacks from BCT, rejecting the 10 NaT pattern.

NaT marks poisoned host memory and must never reach a guest, so decoding it is an error rather than a silent zero (D-03).

Source

pub const fn checked_add(self, rhs: Self) -> Option<Self>

Addition, None on overflow.

Source

pub const fn checked_sub(self, rhs: Self) -> Option<Self>

Subtraction, None on overflow.

Source

pub const fn checked_mul(self, rhs: Self) -> Option<Self>

Multiplication, None on overflow.

Source

pub const fn checked_div(self, rhs: Self) -> Option<Self>

Truncating division, None only when rhs is zero.

Division cannot overflow: the range is symmetric, so there is no MIN / -1 case (D-01).

Source

pub const fn checked_rem(self, rhs: Self) -> Option<Self>

Remainder, None only when rhs is zero.

Source

pub const fn neg(self) -> Self

Negation. Cannot overflow; the range is symmetric (D-01).

Source

pub const fn abs(self) -> Self

Absolute value. Total; the range is symmetric (D-01).

Source

pub const fn wrapping_add(self, rhs: Self) -> Self

Addition modulo 3^n.

Source

pub const fn wrapping_sub(self, rhs: Self) -> Self

Subtraction modulo 3^n.

Source

pub const fn checked_shl_trits(self, k: u32) -> Option<Self>

Shift left by k trits, multiplying by 3^k. None on overflow. Exact by construction.

Source

pub const fn shr_trits(self, k: u32) -> Self

Shift right by k trits, dropping the low k digits. This is division by 3^k rounded to nearest: the dropped digits are worth at most (3^k-1)/2, half a unit in the last place.

This is not the host’s /, which truncates toward zero. For 1,598,046,941,971 / 27 truncation leaves a remainder of 19 against a ULP of 27. Implementing this as self.0 / pow3(k) was a bug, caught by truncation_is_round_to_nearest.

Ties cannot occur: 3^k is odd, so twice the remainder never equals it. No round-half-to-even rule is needed.

Source

pub const fn cmp3(self, rhs: Self) -> Trit

Three-way comparison returning one trit (D-06).

Neg for less, Zero for equal, Pos for greater.

Source

pub const fn signum(self) -> Trit

The sign, as a trit.

Source

pub fn map_trits(self, f: impl Fn(Trit) -> Trit) -> Self

Applies a per-trit unary operation.

Source

pub fn zip_trits(self, rhs: Self, f: impl Fn(Trit, Trit) -> Trit) -> Self

Applies a per-trit binary operation, used by MIN and MAX.

Source

pub fn trit_and(self, rhs: Self) -> Self

Per-trit logical AND (minimum).

Source

pub fn trit_or(self, rhs: Self) -> Self

Per-trit logical OR (maximum).

Source

pub fn trit_not(self) -> Self

Per-trit logical NOT. Identical to arithmetic negation, since negating a balanced ternary number flips every digit (D-01).

Source§

impl Word

Source

pub const TRYTES: usize = 3

Number of trytes in the representation.

Source

pub const fn tryte(self, index: usize) -> Tryte

Tryte index, counting from the least significant.

§Panics

If index >= TRYTES.

Source

pub const fn from_trytes(trytes: [Tryte; 3]) -> Self

Rebuilds a value from its trytes, least significant first.

Source§

impl Word

Source

pub const fn wrapping_mul(self, rhs: Self) -> Self

Multiplication modulo 3^n.

Source§

impl Word

Source

pub const fn widen(self) -> DoubleWord

Widens to a DoubleWord. Always exact.

Source

pub const fn widening_mul(self, rhs: Self) -> DoubleWord

The exact product of two words, as a double word.

Cannot overflow: the product of two p-trit values is exactly 2p trits, and 27 + 27 is the double-word width. A DoubleWord accumulator therefore holds each product exactly and only the summation rounds (D-02).

Trait Implementations§

Source§

impl Clone for Word

Source§

fn clone(&self) -> Word

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Word

Source§

impl Debug for Word

Source§

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

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

impl Default for Word

Source§

fn default() -> Word

Returns the “default value” for a type. Read more
Source§

impl Display for Word

Source§

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

Balanced ternary digits, most significant first, T for -1.

Source§

impl Eq for Word

Source§

impl FromStr for Word

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses balanced ternary digits, most significant first. Underscores are ignored as separators.

Source§

type Err = ParseTritsError

The associated error which can be returned from parsing.
Source§

impl Hash for Word

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Neg for Word

Source§

type Output = Word

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for Word

Source§

fn cmp(&self, other: &Word) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Word

Source§

fn eq(&self, other: &Word) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Word

Source§

fn partial_cmp(&self, other: &Word) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Word

Auto Trait Implementations§

§

impl Freeze for Word

§

impl RefUnwindSafe for Word

§

impl Send for Word

§

impl Sync for Word

§

impl Unpin for Word

§

impl UnsafeUnpin for Word

§

impl UnwindSafe for Word

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<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.