pub struct Tryte3(/* private fields */);Expand description
A balanced ternary byte consisting of 3 trits.
§Value Range
A Tryte3 can represent values from -13 to +13:
Value = t0 * 1 + t1 * 3 + t2 * 9
Min: -1 - 3 - 9 = -13
Max: +1 + 3 + 9 = +13§Internal Representation
Stored as a single u8 with the following encoding:
- Bits 0-1: trit 0 (least significant)
- Bits 2-3: trit 1
- Bits 4-5: trit 2 (most significant)
Each trit is encoded as: 0=N(-1), 1=Z(0), 2=P(+1)
§Examples
use trit_vsa::Tryte3;
let t = Tryte3::from_value(5).unwrap();
assert_eq!(t.value(), 5);
// Decompose into trits: 5 = -1 + (-1)*3 + 1*9 = -1 - 3 + 9
let trits = t.to_trits();
assert_eq!(trits[0].value(), -1); // t0 = -1
assert_eq!(trits[1].value(), -1); // t1 = -1
assert_eq!(trits[2].value(), 1); // t2 = +1Implementations§
Source§impl Tryte3
impl Tryte3
Sourcepub fn from_value(value: i32) -> Result<Self>
pub fn from_value(value: i32) -> Result<Self>
Create a tryte from an integer value.
§Arguments
value- Integer value (-13 to +13)
§Errors
Returns TernaryError::InvalidTryteValue if value is outside range.
§Examples
use trit_vsa::Tryte3;
let t = Tryte3::from_value(7).unwrap();
assert_eq!(t.value(), 7);
assert!(Tryte3::from_value(14).is_err());
assert!(Tryte3::from_value(-14).is_err());Sourcepub fn from_trits(trits: [Trit; 3]) -> Self
pub fn from_trits(trits: [Trit; 3]) -> Self
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Tryte3
impl<'de> Deserialize<'de> for Tryte3
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Copy for Tryte3
impl Eq for Tryte3
impl StructuralPartialEq for Tryte3
Auto Trait Implementations§
impl Freeze for Tryte3
impl RefUnwindSafe for Tryte3
impl Send for Tryte3
impl Sync for Tryte3
impl Unpin for Tryte3
impl UnwindSafe for Tryte3
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more