pub struct Tryte<const SIZE: usize = 6> { /* private fields */ }Expand description
A struct representing a balanced ternary number with a fixed length of SIZE digits.
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
-364to364
§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 400to6 078 832 729 528 464 400
Implementations§
Source§impl<const SIZE: usize> Tryte<SIZE>
impl<const SIZE: usize> Tryte<SIZE>
Sourcepub const fn new(digits: [Digit; SIZE]) -> Self
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 SIZEDigitvalues 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);Sourcepub fn to_ternary(&self) -> Ternary
pub fn to_ternary(&self) -> Ternary
Converts the Tryte into its Ternary representation.
§Returns
A Ternary object representing the same balanced ternary number.
Sourcepub const fn to_digit_slice(&self) -> &[Digit]
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.
Sourcepub fn from_ternary(v: &Ternary) -> Self
pub fn from_ternary(v: &Ternary) -> Self
Sourcepub fn to_i64(&self) -> i64
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.
Sourcepub fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, with: Digit) -> Self
pub fn each_with(&self, f: impl Fn(Digit, Digit) -> Digit, with: Digit) -> Self
See Ternary::each_with.
Sourcepub fn each_zip(&self, f: impl Fn(Digit, Digit) -> Digit, other: Self) -> Self
pub fn each_zip(&self, f: impl Fn(Digit, Digit) -> Digit, other: Self) -> Self
See Ternary::each_zip.