TritBuf

Struct TritBuf 

Source
pub struct TritBuf<T: RawEncodingBuf = T1B1Buf<Btrit>>(/* private fields */);
Expand description

A buffer containing trits.

This type is roughly analogous to Vec or String. It supports pushing and popping trits and dereferences to Trits. It may be borrowed as a trit slice, either mutably or immutably.

Implementations§

Source§

impl<T: RawEncodingBuf> TritBuf<T>

Source

pub fn new() -> Self

Create a new empty TritBuf.

Source

pub fn with_capacity(cap: usize) -> Self

Create a new empty TritBuf, backed by the given capacity, cap. The resulting TritBuf will contain at least enough space to contain cap trits without needing to reallocate.

Source

pub fn filled(len: usize, trit: <T::Slice as RawEncoding>::Trit) -> Self

Create a new TritBuf of the given length, filled with copies of the provided trit.

Source

pub fn zeros(len: usize) -> Self

Create a new TritBuf of the given length, filled with zero trit.

Source

pub fn from_trits(trits: &[<T::Slice as RawEncoding>::Trit]) -> Self

Create a new TritBuf containing the trits from the given slice of trits.

Source

pub fn clear(&mut self)

Clears the buffer, removing all values. Note that this method has no effect on the allocated capacity of the buffer.

Source

pub fn push(&mut self, trit: <T::Slice as RawEncoding>::Trit)

Push a trit to the back of this TritBuf.

Source

pub fn pop(&mut self) -> Option<<T::Slice as RawEncoding>::Trit>

Pop a trit from the back of this TritBuf, returning it if successful.

Source

pub fn append<U: RawEncoding<Trit = <T::Slice as RawEncoding>::Trit> + ?Sized>( &mut self, trits: &Trits<U>, )

Append a trit slice to the end of this TritBuf.

Source

pub fn as_slice(&self) -> &Trits<T::Slice>

Extracts a trit slice containing the data within this buffer.

Note that TritBuf dereferences to Trits anyway, so it’s usually sufficient to take a reference to TritBuf or to just call &Trits methods on it rather than explicitly calling this method first.

Source

pub fn as_slice_mut(&mut self) -> &mut Trits<T::Slice>

Extracts a mutable trit slice containing the data within this buffer.

Note that TritBuf dereferences to Trits anyway, so it’s usually sufficient to take a reference to TritBuf or to just call &mut Trits methods on it rather explicitly calling this method first.

Source

pub fn capacity(&self) -> usize

Returns the number of trits the TritBuf can hold without reallocating.

Source§

impl TritBuf<T3B1Buf>

Source

pub fn pad_zeros(&mut self)

Pad the trit buffer with Btrit::Zero until the buffer’s length is a multiple of 3.

This method is often used in conjunction with Trits::as_trytes.

Source

pub fn padded_zeros(self) -> Self

Pad the trit buffer with Btrit::Zero until the buffer’s length is a multiple of 3.

This method is often used in conjunction with Trits::as_trytes.

Source§

impl<T: RawEncodingBuf> TritBuf<T>
where T::Slice: RawEncoding<Trit = Btrit>,

Source

pub fn from_i8s(trits: &[i8]) -> Result<Self, <Btrit as TryFrom<i8>>::Error>

Create a new TritBuf containing the trits given by the slice of i8s.

Source§

impl<T: RawEncodingBuf> TritBuf<T>
where T::Slice: RawEncoding<Trit = Utrit>,

Source

pub fn from_u8s(trits: &[u8]) -> Result<Self, <Btrit as TryFrom<u8>>::Error>

Create a new TritBuf containing the trits given by the slice of u8s.

Source§

impl<T> TritBuf<T1B1Buf<T>>
where T: Trit, T::Target: Trit,

Source

pub fn into_shifted(self) -> TritBuf<T1B1Buf<<T as ShiftTernary>::Target>>

Transform this TritBuf into a shifted representation. If the buffer contains balanced trits (Btrit), the returned buffer will contain unbalanced trits (Utrit).

Methods from Deref<Target = Trits<T::Slice>>§

Source

pub fn is_empty(&self) -> bool

Returns true if the trit slice is empty.

Source

pub fn len(&self) -> usize

Returns the number of trits in this trit slice.

Source

pub fn as_i8_slice(&self) -> &[i8]

Interpret this slice as an (core::i8) slice.

§Panics

This function will panic if the slice is not byte-aligned.

Source

pub unsafe fn as_i8_slice_mut(&mut self) -> &mut [i8]

Interpret this slice as a mutable (core::i8) slice.

§Panics

This function will panic if the slice is not byte-aligned.

§Safety

This function is marked unsafe because modification of the trit slice in a manner that is not valid for this encoding is undefined behaviour.

Source

pub unsafe fn get_unchecked(&self, index: usize) -> T::Trit

Fetch the trit at the given index of this trit slice without first checking whether the index is in bounds. Providing an index that is not less than the length of this slice is undefined behaviour.

This is perhaps the ‘least bad’ unsafe function in this crate: not because any form of undefined behaviour is better or worse than another (after all, the point of undefined behaviour is that it is undefined) but because it’s the easiest to use correctly.

§Safety

An index with a value less then the result of Trits::len must be used. Any other value is undefined behaviour.

Source

pub unsafe fn set_unchecked(&mut self, index: usize, trit: T::Trit)

Set the trit at the given index of this trit slice without first checking whether the index is in bounds. Providing an index that is not less than the length of this slice is undefined behaviour.

This is perhaps the ‘least bad’ unsafe function in this crate: not because any form of undefined behaviour is better or worse than another (after all, the point of undefined behaviour is that it is undefined) but because it’s the easiest to use correctly.

§Safety

An index with a value less then the result of Trits::len must be used. Any other value is undefined behaviour.

Source

pub fn get(&self, index: usize) -> Option<T::Trit>

Fetch the trit at the given index of this trit slice, if the index is valid.

Source

pub fn set(&mut self, index: usize, trit: T::Trit)

Set the trit at the given index of this mutable trit slice, if the index is valid.

§Panics

This function will panic if the index is not less than the length of this slice.

Source

pub fn iter( &self, ) -> impl DoubleEndedIterator<Item = T::Trit> + ExactSizeIterator<Item = T::Trit> + '_

Returns an iterator over the trits in this slice.

Using this function is significantly faster than calling Trits::get in a loop and should be used where possible.

Source

pub fn subslice(&self, range: Range<usize>) -> &Self

Returns a subslice of this slice with the given range of trits.

§Panics

This function will panic if called with a range that contains indices outside this slice, or the start of the range is greater than its end.

Source

pub fn subslice_mut(&mut self, range: Range<usize>) -> &mut Self

Returns a mutable subslice of this mutable slice with the given range of trits.

§Panics

This function will panic if called with a range that contains indices outside this slice, or the start of the range is greater than its end.

Source

pub fn copy_from<U: RawEncoding<Trit = T::Trit> + ?Sized>( &mut self, trits: &Trits<U>, )

Copy the trits from a trit slice into this mutable trit slice (the encoding need not be equivalent).

§Panics

This function will panic if the length of the slices are different.

Source

pub fn fill(&mut self, trit: T::Trit)

Fill this mutable trit slice with copied of the given trit.

Source

pub fn to_buf<U: RawEncodingBuf<Slice = T>>(&self) -> TritBuf<U>

Copy the contents of this trit slice into a new TritBuf with the same encoding. This function is analogous to to_vec method implemented on ordinary slices.

Source

pub fn chunks( &self, chunk_len: usize, ) -> impl DoubleEndedIterator<Item = &Self> + ExactSizeIterator<Item = &Self> + '_

Return an iterator over distinct, non-overlapping subslices of this trit slice, each with the given chunk length. If the length of the trit slice is not a multiple of the given chunk length, the last slice provided by the iterator will be smaller to compensate.

§Panics

This function will panic if the given chunk length is 0.

Source

pub fn encode<U>(&self) -> TritBuf<U>
where U: RawEncodingBuf, U::Slice: RawEncoding<Trit = T::Trit>,

Encode the contents of this trit slice into a TritBuf with a different encoding.

Source

pub fn iter_trytes( &self, ) -> impl DoubleEndedIterator<Item = Tryte> + ExactSizeIterator<Item = Tryte> + '_

Returns an iterator over the trytes represented within this slice.

For encodings that are representation-compatible with trytes, such as T3B1, use Trits::as_trytes instead since it is faster and more capable.

Source

pub fn negate(&mut self)

Negate each trit in this buffer.

This has the effect of making the trit buffer negative when expressed in numeric form.

Trait Implementations§

Source§

impl<T: RawEncodingBuf> Borrow<Trits<<T as RawEncodingBuf>::Slice>> for TritBuf<T>

Source§

fn borrow(&self) -> &Trits<T::Slice>

Immutably borrows from an owned value. Read more
Source§

impl<T: RawEncodingBuf> BorrowMut<Trits<<T as RawEncodingBuf>::Slice>> for TritBuf<T>

Source§

fn borrow_mut(&mut self) -> &mut Trits<T::Slice>

Mutably borrows from an owned value. Read more
Source§

impl<T: Clone + RawEncodingBuf> Clone for TritBuf<T>

Source§

fn clone(&self) -> TritBuf<T>

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<T: RawEncodingBuf> Debug for TritBuf<T>

Source§

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

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

impl<T: RawEncodingBuf> Default for TritBuf<T>

Source§

fn default() -> Self

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

impl<T: RawEncodingBuf> Deref for TritBuf<T>

Source§

type Target = Trits<<T as RawEncodingBuf>::Slice>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: RawEncodingBuf> DerefMut for TritBuf<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'de, T: RawEncodingBuf> Deserialize<'de> for TritBuf<T>
where <T::Slice as RawEncoding>::Trit: Deserialize<'de>,

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: RawEncodingBuf> Display for TritBuf<T>

Source§

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

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

impl<T: RawEncodingBuf> From<i128> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Btrit>,

Source§

fn from(x: i128) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<i16> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Btrit>,

Source§

fn from(x: i16) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<i32> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Btrit>,

Source§

fn from(x: i32) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<i64> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Btrit>,

Source§

fn from(x: i64) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<i8> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Btrit>,

Source§

fn from(x: i8) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<u128> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Utrit>,

Source§

fn from(x: u128) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<u16> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Utrit>,

Source§

fn from(x: u16) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<u32> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Utrit>,

Source§

fn from(x: u32) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<u64> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Utrit>,

Source§

fn from(x: u64) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> From<u8> for TritBuf<T>
where T::Slice: RawEncoding<Trit = Utrit>,

Source§

fn from(x: u8) -> Self

Converts to this type from the input type.
Source§

impl<T: RawEncodingBuf> FromIterator<<<T as RawEncodingBuf>::Slice as RawEncoding>::Trit> for TritBuf<T>

Source§

fn from_iter<I: IntoIterator<Item = <T::Slice as RawEncoding>::Trit>>( iter: I, ) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Hash for TritBuf<T>
where T: RawEncodingBuf, T::Slice: Hash,

Source§

fn hash<H: Hasher>(&self, hasher: &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<T: RawEncodingBuf> Neg for TritBuf<T>
where T::Slice: RawEncoding<Trit = Btrit>,

Source§

type Output = TritBuf<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<T: RawEncodingBuf, U: RawEncodingBuf> PartialEq<TritBuf<U>> for TritBuf<T>
where T::Slice: RawEncoding, U::Slice: RawEncoding<Trit = <T::Slice as RawEncoding>::Trit>,

Source§

fn eq(&self, other: &TritBuf<U>) -> 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<T, U> PartialOrd<TritBuf<U>> for TritBuf<T>

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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<T: RawEncodingBuf> Serialize for TritBuf<T>
where <T::Slice as RawEncoding>::Trit: Serialize,

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: RawEncodingBuf> Eq for TritBuf<T>
where T::Slice: RawEncoding,

Auto Trait Implementations§

§

impl<T> Freeze for TritBuf<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for TritBuf<T>
where T: RefUnwindSafe,

§

impl<T> Send for TritBuf<T>
where T: Send,

§

impl<T> Sync for TritBuf<T>
where T: Sync,

§

impl<T> Unpin for TritBuf<T>
where T: Unpin,

§

impl<T> UnwindSafe for TritBuf<T>
where T: UnwindSafe,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,