pub struct TokenBlockSequence { /* private fields */ }
Expand description
Represents a sequence of tokens, segmented into fixed-size, hashed blocks.
This structure manages a series of completed TokenBlock
s and one
PartialTokenBlock
for accumulating incoming tokens.
It provides methods for appending tokens (append
, extend
), removing tokens
(pop
, truncate
, unwind
), and accessing sequence information.
Hashing incorporates an initial SaltHash
to ensure uniqueness across different
contexts (e.g., different models, PEFTs).
Key Hashes:
BlockHash
: Hash of tokens within a single block (seeded bySaltHash
).SequenceHash
: Hash combining the previous block’sSequenceHash
and the current block’sBlockHash
(also seeded bySaltHash
).
Implementations§
Source§impl TokenBlockSequence
impl TokenBlockSequence
Sourcepub fn new(tokens: Tokens, block_size: u32, salt_hash: Option<SaltHash>) -> Self
pub fn new(tokens: Tokens, block_size: u32, salt_hash: Option<SaltHash>) -> Self
Creates a new TokenBlockSequence
from an initial set of tokens.
The tokens are split into blocks of block_size
. Any remaining tokens
form the initial current_block
.
§Arguments
tokens
- The initialTokens
for the sequence.block_size
- The fixed size for eachTokenBlock
. Must be greater than 0.salt_hash
- An optionalSaltHash
. Defaults to 0 ifNone
.
§Panics
Panics if block_size
is 0.
Sourcepub fn extend(
&mut self,
tokens: Tokens,
) -> Result<Option<Range<usize>>, TokenBlockError>
pub fn extend( &mut self, tokens: Tokens, ) -> Result<Option<Range<usize>>, TokenBlockError>
Extends the sequence with the given tokens, potentially completing multiple blocks.
This method processes all tokens from the input Tokens
object.
If adding tokens causes one or more blocks to become full, they are committed
and added to the internal list of completed blocks.
§Arguments
tokens
- TheTokens
object containing the tokens to extend the sequence with.
§Returns
Ok(Some(Range<usize>))
- The range of indices in theblocks
vector corresponding to the blocks completed during thisextend
operation.Ok(None)
- If no blocks were completed.Err(TokenBlockError)
- If an internal error occurs during commit.
Sourcepub fn append(&mut self, token: Token) -> Result<Option<usize>, TokenBlockError>
pub fn append(&mut self, token: Token) -> Result<Option<usize>, TokenBlockError>
Appends a single token to the sequence.
If adding this token completes the current partial block, the block is committed, and the index of the newly completed block is returned.
This method is equivalent to calling [extend
] with a single-token Tokens
object.
§Arguments
token
- TheToken
to append.
§Returns
Ok(Some(usize))
- The index of the block that was just completed.Ok(None)
- No block was completed by adding this token.Err(TokenBlockError)
- If an internal error occurs during processing.
Sourcepub fn truncate(&mut self, len: usize) -> Result<(), TokenBlockError>
pub fn truncate(&mut self, len: usize) -> Result<(), TokenBlockError>
Shortens the sequence, keeping the first len
tokens and removing the rest.
If len
is greater than the sequence’s current length, this has no effect.
This operation is analogous to Vec::truncate
.
It may involve removing tokens from the current partial block, removing entire
completed blocks, and adjusting the current partial block
to reflect the new end of the sequence.
§Arguments
len
- The number of tokens to keep.
§Returns
Ok(())
- If the sequence was successfully truncated.Err(TokenBlockError::InsufficientTokens)
- This error should ideally not occur iflen
is correctly checked againsttotal_tokens
, but the underlyingpop_tokens
might return it.
Sourcepub fn unwind(&mut self, count: usize) -> Result<(), TokenBlockError>
pub fn unwind(&mut self, count: usize) -> Result<(), TokenBlockError>
Removes the last count
tokens from the sequence.
This is a convenience method that calculates the required length and calls [truncate
].
§Arguments
count
- The number of tokens to remove from the end.
§Returns
Ok(())
- If the tokens were successfully removed.Err(TokenBlockError::InsufficientTokens)
- Ifcount
is greater than or equal to the total number of tokens in the sequence.
Sourcepub fn blocks(&self) -> &[TokenBlock]
pub fn blocks(&self) -> &[TokenBlock]
Returns a slice containing all the completed TokenBlock
s in the sequence.
Sourcepub fn last_complete_block(&self) -> Option<&TokenBlock>
pub fn last_complete_block(&self) -> Option<&TokenBlock>
Returns a reference to the last completed TokenBlock
in the sequence, if any.
Sourcepub fn current_block(&self) -> &PartialTokenBlock
pub fn current_block(&self) -> &PartialTokenBlock
Returns a reference to the current PartialTokenBlock
where new tokens are added.
Sourcepub fn into_parts(self) -> (Vec<TokenBlock>, PartialTokenBlock)
pub fn into_parts(self) -> (Vec<TokenBlock>, PartialTokenBlock)
Consumes the sequence and returns its parts: a Vec
of completed blocks and the final partial block.
Sourcepub fn block_size(&self) -> usize
pub fn block_size(&self) -> usize
Returns the block size used for this sequence.
Sourcepub fn total_tokens(&self) -> usize
pub fn total_tokens(&self) -> usize
Returns the total number of tokens in the sequence (sum of tokens in all completed blocks plus tokens in the current partial block).
Sourcepub fn split_tokens(
tokens: &[Token],
block_size: u32,
salt_hash: u64,
) -> (Vec<TokenBlock>, PartialTokenBlock)
pub fn split_tokens( tokens: &[Token], block_size: u32, salt_hash: u64, ) -> (Vec<TokenBlock>, PartialTokenBlock)
Splits a Tokens
object into a vector of completed blocks and a final partial block.
This is primarily used internally by TokenBlockSequence::new
but can be used externally.
§Arguments
tokens
- TheTokens
to split.block_size
- The size of each block.salt_hash
- TheSaltHash
to use for hashing.
§Returns
A tuple containing (Vec<TokenBlock>, PartialTokenBlock)
.
§Panics
Panics if block_size
is 0.
pub fn from_slice( tokens: &[Token], block_size: u32, salt_hash: Option<SaltHash>, ) -> Self
Trait Implementations§
Source§impl Debug for TokenBlockSequence
impl Debug for TokenBlockSequence
Source§impl PartialEq for TokenBlockSequence
impl PartialEq for TokenBlockSequence
impl StructuralPartialEq for TokenBlockSequence
Auto Trait Implementations§
impl Freeze for TokenBlockSequence
impl RefUnwindSafe for TokenBlockSequence
impl Send for TokenBlockSequence
impl Sync for TokenBlockSequence
impl Unpin for TokenBlockSequence
impl UnwindSafe for TokenBlockSequence
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
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>
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>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<T, U> OverflowingInto<U> for Twhere
U: OverflowingFrom<T>,
impl<T, U> OverflowingInto<U> for Twhere
U: OverflowingFrom<T>,
fn overflowing_into(self) -> (U, bool)
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);