Skip to main content

AnyProjector

Struct AnyProjector 

Source
pub struct AnyProjector { /* private fields */ }
Expand description

A type-erased ConversationProjector for dynamic dispatch.

Wraps any concrete projector so it can be stored in trait objects, passed across module boundaries, or held in collections alongside projectors with different output types.

§Example

use toolpath_convo::{ConversationView, ConversationProjector, Result};
use toolpath_convo::project::AnyProjector;
use std::collections::HashMap;

struct TurnCounter;
impl ConversationProjector for TurnCounter {
    type Output = usize;
    fn project(&self, view: &ConversationView) -> Result<usize> {
        Ok(view.turns.len())
    }
}

let view = ConversationView {
    id: "s1".into(),
    started_at: None,
    last_activity: None,
    turns: vec![],
    total_usage: None,
    provider_id: None,
    files_changed: vec![],
    session_ids: vec![],
    events: vec![],
};

let projector = AnyProjector::new(TurnCounter);
let count = projector.project_as::<usize>(&view).unwrap();
assert_eq!(count, 0);

Implementations§

Source§

impl AnyProjector

Source

pub fn new<P>(projector: P) -> Self
where P: ConversationProjector + Send + Sync + 'static, P::Output: 'static,

Wrap a concrete ConversationProjector for type-erased dispatch.

Source

pub fn project(&self, view: &ConversationView) -> Result<Box<dyn Any>>

Project view and return the result as Box<dyn Any>.

Use project_as when the concrete output type is known at the call site.

Source

pub fn project_as<T: 'static>(&self, view: &ConversationView) -> Result<T>

Project view and downcast the result to T.

Returns Err(ConvoError::Provider(...)) if the downcast fails, which means T does not match the projector’s actual Output type.

Auto Trait Implementations§

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> 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<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.