Struct bee_ternary::Trits
source · [−]#[repr(transparent)]pub struct Trits<T: RawEncoding + ?Sized = T1B1<Btrit>>(_);
Expand description
A type that represents a buffer of trits of unknown length.
This type is roughly analogous to [T]
or str
. It is an unsized type and hence is rarely
used directly. Instead, it’s more common to see it used from behind a reference (in a similar
manner to &[T]
and &str
.
Implementations
sourceimpl<T> Trits<T> where
T: RawEncoding + ?Sized,
impl<T> Trits<T> where
T: RawEncoding + ?Sized,
sourcepub unsafe fn from_raw_unchecked(raw: &[i8], num_trits: usize) -> &Self
pub unsafe fn from_raw_unchecked(raw: &[i8], num_trits: usize) -> &Self
Interpret an (core::i8
) slice as a trit slice with the given encoding without first
checking that the slice is valid in the given encoding. The num_trits
parameter is used
to specify the exact length, in trits, that the slice should be taken to have. Providing a
slice that is not valid for this encoding is undefined behaviour.
Panics
This function will panic if num_trits
is more than can be represented with the slice in
the given encoding.
Safety
This function must only be called with an i8
slice that is valid for this trit encoding
given the specified num_trits
length. Right now, this validity is not well-defined and so
it is suggested that only i8
slices created from existing trit slices or trit buffers
be used. Calling this function with an invalid i8
slice is undefined behaviour.
sourcepub unsafe fn from_raw_unchecked_mut(
raw: &mut [i8],
num_trits: usize
) -> &mut Self
pub unsafe fn from_raw_unchecked_mut(
raw: &mut [i8],
num_trits: usize
) -> &mut Self
Interpret a mutable (core::i8
) slice as a mutable trit slice with the given encoding
without first checking that the slice is valid in the given encoding. The num_trits
parameter is used to specify the exact length, in trits, that the slice should be taken to
have. Providing a slice that is not valid for this encoding is undefined behaviour.
Panics
This function will panic if num_trits
is more than can be represented with the slice in
the given encoding.
Safety
This function must only be called with an i8
slice that is valid for this trit encoding
given the specified num_trits
length. Right now, this validity is not well-defined and so
it is suggested that only i8
slices created from existing trit slices or trit buffers
be used. Calling this function with an invalid i8
slice is undefined behaviour.
sourcepub fn try_from_raw(raw: &[i8], num_trits: usize) -> Result<&Self, Error>
pub fn try_from_raw(raw: &[i8], num_trits: usize) -> Result<&Self, Error>
Interpret an (core::i8
) slice as a trit slice with the given encoding, checking to ensure
that the slice is valid in the given encoding. The num_trits
parameter is used to specify
the exact length, in trits, that the slice should be taken to have.
Panics
This function will panic if num_trits
is more than can be represented with the slice in
the given encoding.
sourcepub fn try_from_raw_mut(
raw: &mut [i8],
num_trits: usize
) -> Result<&mut Self, Error>
pub fn try_from_raw_mut(
raw: &mut [i8],
num_trits: usize
) -> Result<&mut Self, Error>
Interpret a mutable (core::i8
) slice as a mutable trit slice with the given encoding,
checking to ensure that the slice is valid in the given encoding. The num_trits
parameter
is used to specify the exact length, in trits, that the slice should be taken to have.
Panics
This function will panic if num_trits
is more than can be represented with the slice in
the given encoding.
sourcepub fn as_i8_slice(&self) -> &[i8]
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.
sourcepub unsafe fn as_i8_slice_mut(&mut self) -> &mut [i8]
pub unsafe fn as_i8_slice_mut(&mut self) -> &mut [i8]
sourcepub unsafe fn get_unchecked(&self, index: usize) -> T::Trit
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.
sourcepub unsafe fn set_unchecked(&mut self, index: usize, trit: T::Trit)
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.
sourcepub fn get(&self, index: usize) -> Option<T::Trit>
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.
sourcepub fn set(&mut self, index: usize, trit: T::Trit)
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.
sourcepub fn iter(
&self
) -> impl DoubleEndedIterator<Item = T::Trit> + ExactSizeIterator<Item = T::Trit> + '_
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.
sourcepub fn subslice(&self, range: Range<usize>) -> &Self
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.
sourcepub fn subslice_mut(&mut self, range: Range<usize>) -> &mut Self
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.
sourcepub fn copy_from<U: RawEncoding<Trit = T::Trit> + ?Sized>(
&mut self,
trits: &Trits<U>
)
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.
sourcepub fn fill(&mut self, trit: T::Trit)
pub fn fill(&mut self, trit: T::Trit)
Fill this mutable trit slice with copied of the given trit.
sourcepub fn to_buf<U: RawEncodingBuf<Slice = T>>(&self) -> TritBuf<U>
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.
sourcepub fn chunks(
&self,
chunk_len: usize
) -> impl DoubleEndedIterator<Item = &Self> + ExactSizeIterator<Item = &Self> + '_
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
.
sourcepub fn encode<U>(&self) -> TritBuf<U> where
U: RawEncodingBuf,
U::Slice: RawEncoding<Trit = T::Trit>,
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.
sourceimpl<T> Trits<T> where
T: RawEncoding<Trit = Btrit> + ?Sized,
impl<T> Trits<T> where
T: RawEncoding<Trit = Btrit> + ?Sized,
sourcepub fn iter_trytes(
&self
) -> impl DoubleEndedIterator<Item = Tryte> + ExactSizeIterator<Item = Tryte> + '_
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.
sourceimpl<T: Trit> Trits<T1B1<T>>
impl<T: Trit> Trits<T1B1<T>>
These functions are only implemented for trit slices with the T1B1
encoding because other
encodings are compressed and do not support handing out references to their internal trits.
T1B1
is an exception because its trits are strictly byte-aligned.
This fact also implies that T1B1
is the fastest encoding for general-purpose manipulation
of trits.
sourcepub fn as_raw_slice(&self) -> &[T]ⓘNotable traits for &'_ mut [u8]impl<'_> Write for &'_ mut [u8]impl<'_> Read for &'_ [u8]
pub fn as_raw_slice(&self) -> &[T]ⓘNotable traits for &'_ mut [u8]impl<'_> Write for &'_ mut [u8]impl<'_> Read for &'_ [u8]
View this trit slice as an ordinary slice of trits.
sourcepub fn as_raw_slice_mut(&mut self) -> &mut [T]ⓘNotable traits for &'_ mut [u8]impl<'_> Write for &'_ mut [u8]impl<'_> Read for &'_ [u8]
pub fn as_raw_slice_mut(&mut self) -> &mut [T]ⓘNotable traits for &'_ mut [u8]impl<'_> Write for &'_ mut [u8]impl<'_> Read for &'_ [u8]
View this mutable trit slice as an ordinary slice of mutable trits.
sourcepub fn chunks_mut(
&mut self,
chunk_len: usize
) -> impl Iterator<Item = &mut Self> + '_
pub fn chunks_mut(
&mut self,
chunk_len: usize
) -> impl Iterator<Item = &mut Self> + '_
Return an iterator over distinct, non-overlapping mutable subslices of this mutable 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
.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
Returns a mutable iterator over the trits in this slice.
Using this function is significantly faster than calling Trits::set
in a loop and
should be used where possible.
sourceimpl Trits<T3B1>
impl Trits<T3B1>
These functions are only implemented for trit slices with the T3B1
encoding because only
the T3B1
encoding has a representation compatible with a slice of Tryte
s. If you find
yourself commonly needing to convert between trits and trytes, T3B1
is the encoding to use.
Trait Implementations
sourceimpl<T: RawEncodingBuf> Borrow<Trits<<T as RawEncodingBuf>::Slice>> for TritBuf<T>
impl<T: RawEncodingBuf> Borrow<Trits<<T as RawEncodingBuf>::Slice>> for TritBuf<T>
sourceimpl<T: RawEncodingBuf> BorrowMut<Trits<<T as RawEncodingBuf>::Slice>> for TritBuf<T>
impl<T: RawEncodingBuf> BorrowMut<Trits<<T as RawEncodingBuf>::Slice>> for TritBuf<T>
sourcefn borrow_mut(&mut self) -> &mut Trits<T::Slice>
fn borrow_mut(&mut self) -> &mut Trits<T::Slice>
Mutably borrows from an owned value. Read more
sourceimpl<'a, T: RawEncoding + ?Sized> Debug for &'a Trits<T>
impl<'a, T: RawEncoding + ?Sized> Debug for &'a Trits<T>
sourceimpl<T: RawEncoding + ?Sized> Display for Trits<T>
impl<T: RawEncoding + ?Sized> Display for Trits<T>
sourceimpl<'a, T: Trit> From<&'a mut [T]> for &'a mut Trits<T1B1<T>>
impl<'a, T: Trit> From<&'a mut [T]> for &'a mut Trits<T1B1<T>>
sourcefn from(xs: &'a mut [T]) -> Self
fn from(xs: &'a mut [T]) -> Self
Converts to this type from the input type.
sourceimpl<T: RawEncoding + ?Sized> Index<RangeInclusive<usize>> for Trits<T>
impl<T: RawEncoding + ?Sized> Index<RangeInclusive<usize>> for Trits<T>
sourceimpl<T: RawEncoding + ?Sized> Index<RangeToInclusive<usize>> for Trits<T>
impl<T: RawEncoding + ?Sized> Index<RangeToInclusive<usize>> for Trits<T>
sourceimpl<T: RawEncoding + ?Sized> IndexMut<RangeInclusive<usize>> for Trits<T>
impl<T: RawEncoding + ?Sized> IndexMut<RangeInclusive<usize>> for Trits<T>
sourceimpl<T: RawEncoding + ?Sized> IndexMut<RangeToInclusive<usize>> for Trits<T>
impl<T: RawEncoding + ?Sized> IndexMut<RangeToInclusive<usize>> for Trits<T>
sourceimpl<T, U> PartialEq<Trits<U>> for Trits<T> where
T: RawEncoding + ?Sized,
U: RawEncoding<Trit = T::Trit> + ?Sized,
impl<T, U> PartialEq<Trits<U>> for Trits<T> where
T: RawEncoding + ?Sized,
U: RawEncoding<Trit = T::Trit> + ?Sized,
sourceimpl<T, U> PartialOrd<Trits<U>> for Trits<T> where
T: RawEncoding + ?Sized,
U: RawEncoding<Trit = T::Trit> + ?Sized,
T::Trit: PartialOrd,
impl<T, U> PartialOrd<Trits<U>> for Trits<T> where
T: RawEncoding + ?Sized,
U: RawEncoding<Trit = T::Trit> + ?Sized,
T::Trit: PartialOrd,
sourcefn partial_cmp(&self, other: &Trits<U>) -> Option<Ordering>
fn partial_cmp(&self, other: &Trits<U>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<T: RawEncoding + ?Sized> ToOwned for Trits<T>
impl<T: RawEncoding + ?Sized> ToOwned for Trits<T>
impl<T> Eq for Trits<T> where
T: RawEncoding + ?Sized,
Auto Trait Implementations
impl<T: ?Sized> RefUnwindSafe for Trits<T> where
T: RefUnwindSafe,
impl<T: ?Sized> Send for Trits<T> where
T: Send,
impl<T: ?Sized> Sync for Trits<T> where
T: Sync,
impl<T: ?Sized> Unpin for Trits<T> where
T: Unpin,
impl<T: ?Sized> UnwindSafe for Trits<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more