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<Tryte3, TernaryError>
pub fn from_value(value: i32) -> Result<Tryte3, TernaryError>
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]) -> Tryte3
pub fn from_trits(trits: [Trit; 3]) -> Tryte3
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Tryte3
impl<'de> Deserialize<'de> for Tryte3
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Tryte3, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Tryte3, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Serialize for Tryte3
impl Serialize for Tryte3
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Serialize this value into the given Serde serializer. 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more