Skip to main content

Slicer

Trait Slicer 

Source
pub trait Slicer:
    Send
    + Sync
    + Debug {
    // Required methods
    fn id(&self) -> SlicerId;
    fn slice<'life0, 'life1, 'async_trait>(
        &'life0 self,
        raw: &'life1 Raw,
        cap: usize,
    ) -> Pin<Box<dyn Future<Output = Result<Slice, SliceError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn counter(&self) -> &dyn TokenCount { ... }
    fn minimum_cap(&self) -> usize { ... }
    fn condense<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        raw: &'life1 Raw,
        cap: usize,
        store: &'life2 dyn Store,
    ) -> Pin<Box<dyn Future<Output = Result<Condensate, SliceError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

Layer 4: turns raw output into a size-capped, typed summary.

Implement slice and id. Do not implement condense — it is what enforces the contract, and the whole design of this module is that an implementation cannot opt out of it.

Required Methods§

Source

fn id(&self) -> SlicerId

Which slicer this is, and at what version.

Recorded into every condensate, so re-slicing the same raw output later can be checked against the version that originally produced it.

Source

fn slice<'life0, 'life1, 'async_trait>( &'life0 self, raw: &'life1 Raw, cap: usize, ) -> Pin<Box<dyn Future<Output = Result<Slice, SliceError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Produce the summary. The part an implementation writes.

Must be a pure function of (raw, cap) — see the module documentation on determinism.

§Errors

SliceError::Unsuitable if this slicer does not handle the input.

Provided Methods§

Source

fn counter(&self) -> &dyn TokenCount

How this slicer counts tokens.

Defaults to Estimate. Override to inject a real tokeniser.

Source

fn minimum_cap(&self) -> usize

The least a summary from this slicer can cost.

Used to turn an impossible cap into SliceError::CapTooSmall before any work happens, so the author is told their step’s window is too small rather than handed a mysterious over-cap failure.

Source

fn condense<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, raw: &'life1 Raw, cap: usize, store: &'life2 dyn Store, ) -> Pin<Box<dyn Future<Output = Result<Condensate, SliceError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Preserve the original, slice it, and check the result fits.

In that order, and the order matters: the raw bytes reach the store before anything is dropped, so a slicer that fails part-way through still leaves the original recoverable.

§Errors

SliceError::Store if the original could not be preserved, SliceError::CapTooSmall if the cap cannot hold a summary, SliceError::OverCap if the slicer overran it, or whatever slice returned.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§