Tryte

Struct Tryte 

Source
pub struct Tryte<const SIZE: usize = 6> { /* private fields */ }
Expand description

The Tryte<S> struct represents a Copy type balanced ternary number with exactly S digits (6 by default). Each digit in a balanced ternary system can have one of three values: -1, 0, or 1.

A Tryte<6> can holds value between -364 and +364.

The underlying representation of the number is an array of SIZE Digit values. This struct provides conversion methods to and from other formats.

§Default SIZE

SIZE is 6 by default (the size of a tryte in a Setun computer).

6 trits ~= 9.505 bits

-364 to 364

§Warning

Because arithmetic operations are performed in with 64 bits integers, SIZE cannot be > 40.

40 trits ~= 63.398 bits

-6 078 832 729 528 464 400 to 6 078 832 729 528 464 400

Implementations§

Source§

impl<const SIZE: usize> Tryte<SIZE>

Source

pub const MAX: Self

++...++

Source

pub const MIN: Self

--...--

Source

pub const ZERO: Self

00...00

Source

pub const fn new(digits: [Digit; SIZE]) -> Self

Creates a new Tryte instance from a given array of Digits.

§Arguments
  • raw - An array of exactly SIZE Digit values representing the balanced ternary digits.
§Returns

A new Tryte instance with the specified balanced ternary digits.

§Panics

Panic if SIZE > 40 as 41 trits would be too much information for 64 bits.

§Examples
use balanced_ternary::{Tryte, Digit::{Pos, Zero, Neg}};

let digits = [Pos, Zero, Neg, Zero, Pos, Neg];
let tryte = Tryte::new(digits);
assert_eq!(tryte.to_digit_slice(), &digits);
Source

pub fn to_ternary(&self) -> Ternary

Converts the Tryte into its Ternary representation.

§Returns

A Ternary object representing the same balanced ternary number.

Source

pub const fn to_digit_slice(&self) -> &[Digit]

Retrieves a slice containing the digits of the Tryte.

§Returns

A slice referencing the six-digit array of the Tryte.

This function allows access to the raw representation of the balanced ternary number as a slice of Digit values.

Source

pub fn from_ternary(v: &Ternary) -> Self

Creates a Tryte from the given Ternary.

§Arguments
  • v - A reference to a Ternary object.
§Panics

This function panics if the Ternary contains more than SIZE digits.

Source

pub fn to_i64(&self) -> i64

Converts the Tryte into a signed 64-bit integer.

§Returns

A i64 representing the decimal value of the Tryte.

Source

pub fn from_i64(v: i64) -> Self

Creates a Tryte from a signed 64-bit integer.

§Arguments
  • v - A signed 64-bit integer.
§Returns

A Tryte representing the equivalent ternary number.

Trait Implementations§

Source§

impl<const SIZE: usize> Add for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<const SIZE: usize> BitAnd for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl<const SIZE: usize> BitOr for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl<const SIZE: usize> BitXor for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<const SIZE: usize> Clone for Tryte<SIZE>

Source§

fn clone(&self) -> Tryte<SIZE>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<const SIZE: usize> Debug for Tryte<SIZE>

Source§

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

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

impl<const SIZE: usize> DigitOperate for Tryte<SIZE>

Source§

fn digit(&self, index: usize) -> Option<Digit>

Retrieves the digit at the specified index in the Tryte.

§Arguments
  • index - The index of the digit to retrieve (0-based, right-to-left).
§Returns

The Digit at the specified index or None.

Source§

fn each(&self, f: impl Fn(Digit) -> Digit) -> Self

Source§

fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, with: Digit) -> Self

Source§

fn each_zip(&self, f: impl Fn(Digit, Digit) -> Digit, other: Self) -> Self

Source§

fn each_zip_carry( &self, f: impl Fn(Digit, Digit, Digit) -> (Digit, Digit), other: Self, ) -> Self

Source§

fn to_digits(&self) -> Vec<Digit>

Returns every individual Digit of this DigitOperate object.
Source§

impl<const SIZE: usize> Display for Tryte<SIZE>

Source§

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

Formats the Tryte for display.

The Tryte is displayed in its balanced ternary representation as a SIZE-character string.

Source§

impl<const SIZE: usize> Div for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<const SIZE: usize> From<&str> for Tryte<SIZE>

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<String> for Tryte<SIZE>

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<Ternary> for Tryte<SIZE>

Source§

fn from(value: Ternary) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<Tryte<SIZE>> for String

Source§

fn from(value: Tryte<SIZE>) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<Tryte<SIZE>> for Ternary

Source§

fn from(value: Tryte<SIZE>) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<Tryte<SIZE>> for i64

Source§

fn from(value: Tryte<SIZE>) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> From<i64> for Tryte<SIZE>

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl<const SIZE: usize> FromStr for Tryte<SIZE>

Source§

type Err = ParseTernaryError

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

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

Parses a string s to return a value of this type. Read more
Source§

impl<const SIZE: usize> Hash for Tryte<SIZE>

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<const SIZE: usize> Mul for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<const SIZE: usize> Neg for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<const SIZE: usize> Not for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<const SIZE: usize> PartialEq for Tryte<SIZE>

Source§

fn eq(&self, other: &Tryte<SIZE>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<const SIZE: usize> Sub for Tryte<SIZE>

Source§

type Output = Tryte<SIZE>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<const SIZE: usize> Copy for Tryte<SIZE>

Source§

impl<const SIZE: usize> Eq for Tryte<SIZE>

Source§

impl<const SIZE: usize> StructuralPartialEq for Tryte<SIZE>

Auto Trait Implementations§

§

impl<const SIZE: usize> Freeze for Tryte<SIZE>

§

impl<const SIZE: usize> RefUnwindSafe for Tryte<SIZE>

§

impl<const SIZE: usize> Send for Tryte<SIZE>

§

impl<const SIZE: usize> Sync for Tryte<SIZE>

§

impl<const SIZE: usize> Unpin for Tryte<SIZE>

§

impl<const SIZE: usize> UnwindSafe for Tryte<SIZE>

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.