Skip to main content

TypeVisitor

Trait TypeVisitor 

Source
pub trait TypeVisitor {
    type Db: Habitat;
    type Error;

    // Required method
    fn next_type<E: Describe<Self::Db>>(&mut self) -> Result<(), Self::Error>;
}
Expand description

A visitor applied once per node the graph declares.

§What a visitor may assume about a node

Describe<Self::Db>, which next_type declares — so a body reads <E as Describe<Self::Db>>::BLUEPRINT and not merely the node’s identity. A node that describes no storage in this target is E0277 where the WALK is written, which is where the graph’s field list can be fixed.

§The database is the VISITOR’s, not the walk’s

Db is an associated type rather than a parameter on the trait, because a visitor is built for one database and the walk that drives it takes no side: App::type_iter is generic over V alone, so the same emitted walk serves a SQLite create, a PostgreSQL create and a validate against either. Putting the database on the trait would make a graph’s walk name a database, which is a fact about a deployment rather than about a declaration.

§Examples

A graph written by hand, and a visitor that counts the columns its nodes declare:

use ingraph::{Column, Describe, FieldName, Habitat, RecordAddress, Table, TypeVisitor};

struct Sqlite;

impl Habitat for Sqlite {
  type Blueprint = Table;
}

struct Media;
struct Clip;

impl Describe<Sqlite> for Media {
  const BLUEPRINT: Table = Table::new(
    RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]),
    &[Column::new(FieldName::new("id"), "INTEGER", false)],
  );
}

impl Describe<Sqlite> for Clip {
  const BLUEPRINT: Table = Table::new(
    RecordAddress::new(FieldName::new("clip"), &[FieldName::new("id")]),
    &[
      Column::new(FieldName::new("id"), "INTEGER", false),
      Column::new(FieldName::new("title"), "TEXT", true),
    ],
  );
}

/// What `#[ingraph::graph]` writes for a root whose fields are `Media` and `Clip`.
struct App;

impl App {
  fn type_iter<V>(v: &mut V) -> Result<(), V::Error>
  where
    V: TypeVisitor,
    Media: Describe<V::Db>,
    Clip: Describe<V::Db>,
  {
    v.next_type::<Media>()?;
    v.next_type::<Clip>()
  }
}

#[derive(Default)]
struct Widths(Vec<usize>);

impl TypeVisitor for Widths {
  type Db = Sqlite;
  type Error = core::convert::Infallible;

  fn next_type<E: Describe<Sqlite>>(&mut self) -> Result<(), Self::Error> {
    self.0.push(E::BLUEPRINT.columns().width());
    Ok(())
  }
}

let mut widths = Widths::default();
App::type_iter(&mut widths).unwrap();
assert_eq!(widths.0, vec![1, 2]);

A node this database does not store is refused where the WALK names it, and the refusal is the bound’s own sentence:

ⓘ
use ingraph::{Describe, TypeVisitor};

struct Sqlite;
struct Cue;

fn walk<V: TypeVisitor>(v: &mut V) -> Result<(), V::Error> {
  v.next_type::<Cue>()
}

Required Associated Types§

Source

type Db: Habitat

The TARGET this visitor describes nodes for — a database, a full-text mount, a vector store, a family of buckets.

§Habitat and nothing else, and that ONE bound is what the second layer bought

The member was bound by NOTHING while every visitor here worked against a SQL dialect: what a database had to satisfy was the visitor’s own question, so it was asked where the visitor was written. The initialization layer is what changed it — a walk now hands over a BLUEPRINT, and a target that has not said what a blueprint IS has nothing to hand. So the floor is exactly the vocabulary that answers that, and the rest is still the visitor’s: a create visitor asks for a dialect beside it, a slot maker asks for a mount.

Source

type Error

This visitor’s failure.

Bound-free, per Operator::Error’s discipline: a visitor reports in its own currency, and a caller bounds it where it must render or propagate one.

Required Methods§

Source

fn next_type<E: Describe<Self::Db>>(&mut self) -> Result<(), Self::Error>

Observe one node of the graph.

§Errors

Whatever this visitor’s per-node work fails with. Returning Err ENDS the walk — the nodes after it are not visited — which is what makes a walk’s refusal name the first node it could not serve rather than a list.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<Db: AsyncSchemaDialect> TypeVisitor for Create<Db>

Available on crate feature sqlx only.
Source§

impl<Db: AsyncSchemaDialect> TypeVisitor for Validate<Db>

Available on crate feature sqlx only.
Source§

type Db = Db

Source§

type Error = !

Source§

impl<R: Habitat> TypeVisitor for MakesNothing<R>

Available on crate feature runtime only.
Source§

type Db = R

Source§

type Error = !

Source§

impl<R> TypeVisitor for TantivySlotMaker<R>
where R: AsyncResource + Habitat<Blueprint = &'static [SlotSchema]> + FulltextMount<Handle = TantivyFulltext>,

Available on crate features runtime and tantivy only.

The BLOCKING face, because opening a tantivy index is two syscalls and no await — the same measurement that puts this store on FulltextStore rather than on its awaited twin.

The Db is the RESOURCE and not the engine, which is the grain the whole layer turns on: two deployments’ stores are two resource types and therefore two habitats, so a declaration electing into one of them is E0277 at the other. An engine-keyed habitat would have collapsed them into one and made a second tantivy mount unaddressable.